OiO.lk Blog C++ How to force gcc to do printf checks on non-literal format strings?
C++

How to force gcc to do printf checks on non-literal format strings?


const char* const non_literal_string = "Hello %d";

void my_print()
{
    /* 
     * I would like GCC to throw a warning when compiling this line because the
     * argument is not of int type.
     */
    printf(non_literal_string, "World!");
    
    //Like it does here
    printf("Hello %d", "World!");
}

Above I have declared non_literal_string as const char* const so neither the pointer nor the pointed string can change, so there are the conditions for which GCC can check that the supplied arguments have types appropriate to the specified format string.

With clang compiler these checks are performed even on non-literal format strings, this is the output if I try to compile this code with clang:

<source>:11:32: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
   11 |     printf(non_literal_string, "World!");
      |            ~~~~~~~~~~~~~~~~~~  ^~~~~~~~
<source>:3:47: note: format string is defined here
    3 | const char* const non_literal_string = "Hello %d";
      |                                               ^~
      |                                               %s
<source>:14:24: warning: format specifies type 'int' but the argument has type 'char *' [-Wformat]
   14 |     printf("Hello %d", "World!");
      |                   ~~   ^~~~~~~~
      |                   %s



You need to sign in to view this answers

Exit mobile version