selftest.c: gracefully handle NULL in assert_streq
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 19 Jul 2016 16:16:18 +0000 (16:16 +0000)
committerDavid Malcolm <dmalcolm@gcc.gnu.org>
Tue, 19 Jul 2016 16:16:18 +0000 (16:16 +0000)
gcc/ChangeLog:
* selftest.c (selftest::assert_streq): Handle NULL values of
val_actual and val_expected.

From-SVN: r238479

gcc/ChangeLog
gcc/selftest.c

index c25a1d38e08304263ad98b78307d4651f44290ef..4d4cd9b44e847d58fb590dda60c457aa54aabb64 100644 (file)
@@ -1,3 +1,8 @@
+2016-07-19  David Malcolm  <dmalcolm@redhat.com>
+
+       * selftest.c (selftest::assert_streq): Handle NULL values of
+       val_actual and val_expected.
+
 2016-07-19  Martin Jambor  <mjambor@suse.cz>
 
         PR fortran/71688
index ed6e517f00efeb8c3f0b2c98512cfc8b41fc3c4c..76a4c4149479b041acd134dd8a47fde03b9f8737 100644 (file)
@@ -60,13 +60,25 @@ selftest::fail_formatted (const location &loc, const char *fmt, ...)
   abort ();
 }
 
-/* Implementation detail of ASSERT_STREQ.  */
+/* Implementation detail of ASSERT_STREQ.
+   Compare val_expected and val_actual with strcmp.  They ought
+   to be non-NULL; fail gracefully if either are NULL.  */
 
 void
 selftest::assert_streq (const location &loc,
                        const char *desc_expected, const char *desc_actual,
                        const char *val_expected, const char *val_actual)
 {
+  /* If val_expected is NULL, the test is buggy.  Fail gracefully.  */
+  if (val_expected == NULL)
+    ::selftest::fail_formatted
+       (loc, "ASSERT_STREQ (%s, %s) expected=NULL",
+        desc_expected, desc_actual);
+  /* If val_actual is NULL, fail with a custom error message.  */
+  if (val_actual == NULL)
+    ::selftest::fail_formatted
+       (loc, "ASSERT_STREQ (%s, %s) expected=\"%s\" actual=NULL",
+        desc_expected, desc_actual, val_expected);
   if (0 == strcmp (val_expected, val_actual))
     ::selftest::pass (loc, "ASSERT_STREQ");
   else