No error message when format string is passed as variable to printf functions family

Manuel López-Ibáñez lopezibanez@gmail.com
Thu May 14 08:56:00 GMT 2015


On 13/05/15 23:36, Martin Sebor wrote:
> or optimization necessary to "see" variable values. It only
> considers the values of literals and constants, as in:
>
>   const char str[] = "Last printf: %B\n";
>   printf (str, 2);   // warning
>
> It doesn't do any sort of indirection so this won't cause
> a warning either:
>
>   const char* const str = "Last printf: %B\n";
>   printf (str, 2);   // no warning

Unfortunately, it doesn't work like that 
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52952#c34)

manuel@gcc14:~$ ~/test1/221745/build/gcc/cc1 test.c -Wall
  foo
test.c: In function ‘foo’:
test.c:4:28: warning: unknown conversion type character ‘B’ in format [-Wformat=]
    const char* const strp = "Last printf: %B\n";
                             ^
test.c:7:21: warning: unknown conversion type character ‘B’ in format [-Wformat=]
    __builtin_printf (stra, 2);   // no warning
                      ^
manuel@gcc14:~$ ~/test1/221745/build/gcc/cc1plus test.c -Wall
  void foo()
test.c:5:28: warning: unknown conversion type character ‘B’ in format [-Wformat=]
    __builtin_printf (strp, 2);
                             ^
test.c:7:28: warning: unknown conversion type character ‘B’ in format [-Wformat=]
    __builtin_printf (stra, 2);   // no warning
                             ^

We only keep one location, which one is kept (the use-location or the 
definition location) is kind of arbitrary at the moment.

To be as smart as Clang, we need to keep both:

/home/manuel/test.c:5:21: warning: invalid conversion specifier 'B' 
[-Wformat-invalid-specifier]
   __builtin_printf (strp, 2);
                     ^~~~
/home/manuel/test.c:4:43: note: format string is defined here
   const char* const strp = "Last printf: %B\n";
                                          ~^
/home/manuel/test.c:7:21: warning: invalid conversion specifier 'B' 
[-Wformat-invalid-specifier]
   __builtin_printf (stra, 2);   // no warning
                     ^~~~
/home/manuel/test.c:6:38: note: format string is defined here
   const char stra[] = "Last printf: %B\n";
                                     ~^

My suggestion would be to keep the location of the definition in the object and 
add a location for each argument of the function call. An intermediate solution 
would be to keep a location for the definition (so we can give the note) and 
give the warning at the location of the printf call. This will already be an 
overall improvement.

If anyone wants to help to fix this, please let me know. I don't think it is 
hard, just time consuming, but this has been broken for decades, thus you can 
take your time. :)

Cheers,

	Manuel.



More information about the Gcc-help mailing list