Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: "Andrew Burgess" <aburgess@broadcom.com>
To: "gdb-patches@sourceware.org" <gdb-patches@sourceware.org>
Subject: [PATCH] Don't call strchr with the NULL character.
Date: Thu, 11 Jul 2013 16:15:00 -0000	[thread overview]
Message-ID: <51DEDA0E.8020809@broadcom.com> (raw)

In the printf code we call strchr without guarding against the
case where the second parameter is NULL.

My local manpage for strchr doesn't say what happens in this case,
but this file:  src/libiberty/strchr.c
suggests the results are undefined, and indeed, the answer I see is
not NULL (which is what I might have hoped for).

Patch below adds check for NULL character, and some tests which are
currently failing for me, but as it's string overflow, the results 
are going to be undefined.

OK to apply?

Thanks,
Andrew

gdb/ChangeLog

	* common/format.c (parse_format_string): Add checks for NULL
	character before calling strchr.


gdb/testsuite/ChangeLog

	* gdb.base/printcmds.exp (test_printf): Add tests for format
	strings with missing format specifier.


diff --git a/gdb/common/format.c b/gdb/common/format.c
index 5803818..1bdd253 100644
--- a/gdb/common/format.c
+++ b/gdb/common/format.c
@@ -156,7 +156,7 @@ parse_format_string (const char **arg)
 
 	/* The first part of a format specifier is a set of flag
 	   characters.  */
-	while (strchr ("0-+ #", *f))
+	while (*f != '\0' && strchr ("0-+ #", *f))
 	  {
 	    if (*f == '#')
 	      seen_hash = 1;
@@ -170,7 +170,7 @@ parse_format_string (const char **arg)
 	  }
 
 	/* The next part of a format specifier is a width.  */
-	while (strchr ("0123456789", *f))
+	while (*f != '\0' && strchr ("0123456789", *f))
 	  f++;
 
 	/* The next part of a format specifier is a precision.  */
@@ -178,7 +178,7 @@ parse_format_string (const char **arg)
 	  {
 	    seen_prec = 1;
 	    f++;
-	    while (strchr ("0123456789", *f))
+	    while (*f != '\0' && strchr ("0123456789", *f))
 	      f++;
 	  }
 
diff --git a/gdb/testsuite/gdb.base/printcmds.exp b/gdb/testsuite/gdb.base/printcmds.exp
index 4883fd5..4f88382 100644
--- a/gdb/testsuite/gdb.base/printcmds.exp
+++ b/gdb/testsuite/gdb.base/printcmds.exp
@@ -728,6 +728,12 @@ proc test_printf {} {
     # Regression test for "%% at end of format string.
     # See http://sourceware.org/bugzilla/show_bug.cgi?id=11345
     gdb_test "printf \"%%%d%%\\n\", 5" "%5%"
+
+    # Some tests for missing format specifier after '%'.
+    gdb_test "printf \"%\", 0" "Incomplete format specifier at end of format string"
+    gdb_test "printf \"%.234\", 0" "Incomplete format specifier at end of format string"
+    gdb_test "printf \"%-\", 0" "Incomplete format specifier at end of format string"
+    gdb_test "printf \"%-23\", 0" "Incomplete format specifier at end of format string"
 }
 
 #Test printing DFP values with printf


             reply	other threads:[~2013-07-11 16:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-11 16:15 Andrew Burgess [this message]
2013-07-11 16:24 ` Andreas Schwab
2013-07-11 16:48 ` Paul_Koning
2013-07-12  9:18 ` Andrew Burgess
2013-07-16 17:36   ` Tom Tromey
2013-07-16 21:16     ` Andrew Burgess

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=51DEDA0E.8020809@broadcom.com \
    --to=aburgess@broadcom.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox