diff mbox

selftest.c: gracefully handle NULL in assert_streq

Message ID 1468933479-31901-1-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm July 19, 2016, 1:04 p.m. UTC
If a NULL is passed in as the expected or actual value for an
ASSERT_STREQ, the call to strcmp within selftest::assert_streq
can segfault, leading to a failure of -fself-test without
indicating which test failed.

Handle this more gracefully by checking for NULL, so that
information on the failing test is printed to stderr if this
occurs.

Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.
I also manually tested the various kinds of failure of
ASSERT_STR_EQ, and verified that each branch prints a sane
failure message before aborting.

OK for trunk?

gcc/ChangeLog:
	* selftest.c (selftest::assert_streq): Handle NULL values of
	val_actual and val_expected.
---
 gcc/selftest.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

Comments

Jeff Law July 19, 2016, 4:05 p.m. UTC | #1
On 07/19/2016 07:04 AM, David Malcolm wrote:
> If a NULL is passed in as the expected or actual value for an
> ASSERT_STREQ, the call to strcmp within selftest::assert_streq
> can segfault, leading to a failure of -fself-test without
> indicating which test failed.
>
> Handle this more gracefully by checking for NULL, so that
> information on the failing test is printed to stderr if this
> occurs.
>
> Successfully bootstrapped&regrtested on x86_64-pc-linux-gnu.
> I also manually tested the various kinds of failure of
> ASSERT_STR_EQ, and verified that each branch prints a sane
> failure message before aborting.
>
> OK for trunk?
>
> gcc/ChangeLog:
> 	* selftest.c (selftest::assert_streq): Handle NULL values of
> 	val_actual and val_expected.
OK.
jeff
diff mbox

Patch

diff --git a/gcc/selftest.c b/gcc/selftest.c
index ed6e517..76a4c41 100644
--- a/gcc/selftest.c
+++ b/gcc/selftest.c
@@ -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