This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch to warn for wide string literals with narrow formats


This patch adds an explicit warning if a wide string literal is used
as the format with a narrow string format function.  (GCC doesn't yet
support checking formats to the wide string functions such as
wprintf.)  Previously such formats might give warnings for embedded
NULs, but otherwise wrongly be treated as string literals.

Bootstrapped with no regressions on i686-pc-linux-gnu.  OK to commit?

gcc/ChangeLog:
2000-09-23  Joseph S. Myers  <jsm28@cam.ac.uk>

	* c-common.c (check_format_info): Warn for a wide character string
	used as a non-wide format argument.

gcc/testsuite/ChangeLog:
2000-09-23  Joseph S. Myers  <jsm28@cam.ac.uk>

	* gcc.dg/c90-printf-1.c: Add test for wide string format.

--- c-common.c.orig	Thu Sep 21 06:56:27 2000
+++ c-common.c	Fri Sep 22 23:57:07 2000
@@ -2040,6 +2040,11 @@
 	status_warning (status, "format not a string literal, argument types not checked");
       return;
     }
+  if (TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (format_tree))) != char_type_node)
+    {
+      status_warning (status, "format is a wide character string");
+      return;
+    }
   format_chars = TREE_STRING_POINTER (format_tree);
   format_length = TREE_STRING_LENGTH (format_tree);
   if (format_length <= 1)
--- testsuite/gcc.dg/c90-printf-1.c.orig	Fri Aug 25 21:55:00 2000
+++ testsuite/gcc.dg/c90-printf-1.c	Fri Sep 22 23:52:35 2000
@@ -243,5 +243,6 @@
   printf ("%"); /* { dg-warning "trailing" "trailing % warning" } */
   printf ("%++d", i); /* { dg-warning "repeated" "repeated flag warning" } */
   printf ("%n", cn); /* { dg-warning "constant" "%n with const" } */
+  printf ((const char *)L"foo"); /* { dg-warning "wide" "wide string" } */
   /* Can we test for the warning for unterminated string formats?  */
 }

-- 
Joseph S. Myers
jsm28@cam.ac.uk


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]