Format string wrapper and -Wformat-nonliteral

Tobias Mueller 4tmuelle+gcc@informatik.uni-hamburg.de
Sat Feb 28 16:42:00 GMT 2009


Hi folks,

I basically have a wrapper around strftime() and compilation with 
-Werror=format-nonliteral fails when the wrapper wants to call strftime, 
because "format not a string literal, format string not checked".

I think I want to make gcc
  * know that the wrapper is not responsible for the format string and
    thus the call to strftime is allowed
  * pass the responsibility up to the callers and thus check whether they
    call the wrapper with "good" strings.


And I don't know how to do that. Do you have any advices?

The code in question looks like this:
 > muelli@bigbox /tmp $ cat mystrftime.c
#include<time.h>
#include<stdio.h>

#define SIZE 256

size_t my_strftime(char *s, size_t max, const char *fmt,
                    const struct tm *tm)
{
     size_t ret;
     ret = strftime(s, max, fmt, tm);
     return ret;
}

int
main ()
{
     char s[SIZE];
     time_t curtime;
     struct tm* loctime;

     curtime = time(NULL);
     loctime = localtime (&curtime);
     my_strftime(s, SIZE, "Hello %A", loctime);
     printf("%s", s);
     return 0;
}
> muelli@bigbox /tmp $ gcc -Wformat  -Wformat-nonliteral -Werror=format-nonliteral -o mystrftime{,.c}
> mystrftime.c: In function ‘my_strftime’:
> mystrftime.c:9: error: format not a string literal, format string not checked
> muelli@bigbox /tmp $ 



Thanks in advance,
   Tobi



More information about the Gcc-help mailing list