public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* PATCH: Fix PR 32953
@ 2007-08-01  4:02 Dan Hipschman
  2007-08-11 19:09 ` Joseph S. Myers
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Hipschman @ 2007-08-01  4:02 UTC (permalink / raw)
  To: gcc-patches


GCC gives the wrong warning with -Wformat for unterminated format strings
of length 1:

	/* bar.c */
#include <stdio.h>
const char s[1] = "s";
int
main (void)
{
  printf (s);
  return 0;
}

$ gcc-svn -std=gnu99 -Wformat=2 bar.c
bar.c: In function 'main':
bar.c:6: warning: zero-length printf format string

Which is wrong since the string is not zero-length, it's unterminated.
This patch fixes that, with testcase included.  I bootstrapped and ran
the tests for gcc and g++.  OK to commit?


gcc/
2007-07-31  Dan Hipschman  <dsh@google.com>

	PR 32953
	* c-format.c (check_format_arg): Move check for zero-length
	format strings below the check for unterminated strings.

testsuite/
2007-07-31  Dan Hipschman  <dsh@google.com>

	PR 32953
	* gcc.dg/format/array-1.c: Add an additional test for
	unterminated format strings of length 1.

Index: gcc/testsuite/gcc.dg/format/array-1.c
===================================================================
--- gcc/testsuite/gcc.dg/format/array-1.c	(revision 127105)
+++ gcc/testsuite/gcc.dg/format/array-1.c	(working copy)
@@ -7,7 +7,8 @@
 
 const char a1[] = "foo";
 const char a2[] = "foo%d";
-const char b[3] = "foo";
+const char b1[3] = "foo";
+const char b2[1] = "1";
 static const char c1[] = "foo";
 static const char c2[] = "foo%d";
 char d[] = "foo";
@@ -23,7 +24,8 @@ foo (int i, long l)
   printf (a1);
   printf (a2, i);
   printf (a2, l); /* { dg-warning "format" "wrong type with array" } */
-  printf (b); /* { dg-warning "unterminated" "unterminated array" } */
+  printf (b1); /* { dg-warning "unterminated" "unterminated array" } */
+  printf (b2); /* { dg-warning "unterminated" "unterminated array" } */
   printf (c1);
   printf (c2, i);
   printf (c2, l); /* { dg-warning "format" "wrong type with array" } */
Index: gcc/c-format.c
===================================================================
--- gcc/c-format.c	(revision 127105)
+++ gcc/c-format.c	(working copy)
@@ -1391,21 +1391,16 @@ check_format_arg (void *ctx, tree format
       format_chars += offset;
       format_length -= offset;
     }
-  if (format_length < 1)
+  if (format_length < 1 || format_chars[--format_length] != 0)
     {
       res->number_unterminated++;
       return;
     }
-  if (format_length == 1)
+  if (format_length == 0)
     {
       res->number_empty++;
       return;
     }
-  if (format_chars[--format_length] != 0)
-    {
-      res->number_unterminated++;
-      return;
-    }
 
   /* Skip to first argument to check.  */
   while (arg_num + 1 < info->first_arg_num)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PATCH: Fix PR 32953
  2007-08-01  4:02 PATCH: Fix PR 32953 Dan Hipschman
@ 2007-08-11 19:09 ` Joseph S. Myers
  2007-08-13 21:59   ` Dan Hipschman
  0 siblings, 1 reply; 3+ messages in thread
From: Joseph S. Myers @ 2007-08-11 19:09 UTC (permalink / raw)
  To: Dan Hipschman; +Cc: gcc-patches

On Tue, 31 Jul 2007, Dan Hipschman wrote:

> gcc/
> 2007-07-31  Dan Hipschman  <dsh@google.com>
> 
> 	PR 32953
> 	* c-format.c (check_format_arg): Move check for zero-length
> 	format strings below the check for unterminated strings.
> 
> testsuite/
> 2007-07-31  Dan Hipschman  <dsh@google.com>
> 
> 	PR 32953
> 	* gcc.dg/format/array-1.c: Add an additional test for
> 	unterminated format strings of length 1.

OK.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: PATCH: Fix PR 32953
  2007-08-11 19:09 ` Joseph S. Myers
@ 2007-08-13 21:59   ` Dan Hipschman
  0 siblings, 0 replies; 3+ messages in thread
From: Dan Hipschman @ 2007-08-13 21:59 UTC (permalink / raw)
  To: gcc-patches

On Sat, Aug 11, 2007 at 07:09:23PM +0000, Joseph S. Myers wrote:
> On Tue, 31 Jul 2007, Dan Hipschman wrote:
> 
> > gcc/
> > 2007-07-31  Dan Hipschman  <dsh@google.com>
> > 
> > 	PR 32953
> > 	* c-format.c (check_format_arg): Move check for zero-length
> > 	format strings below the check for unterminated strings.
> > 
> > testsuite/
> > 2007-07-31  Dan Hipschman  <dsh@google.com>
> > 
> > 	PR 32953
> > 	* gcc.dg/format/array-1.c: Add an additional test for
> > 	unterminated format strings of length 1.
> 
> OK.

Thanks.  Committed.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-08-13 21:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-08-01  4:02 PATCH: Fix PR 32953 Dan Hipschman
2007-08-11 19:09 ` Joseph S. Myers
2007-08-13 21:59   ` Dan Hipschman

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).