strftime wrapper: "warning: format not a string literal, format string not checked"

R. Diez via gcc-help gcc-help@gcc.gnu.org
Mon Jan 29 15:53:00 GMT 2018


Hi all:

I have written the following strftime() wrapper:


__attribute__ ((format (strftime, 3, 0) ))
inline size_t strftime_e ( char * const s, const size_t max, const char * const format, const tm * const tm )
{
const size_t res = strftime( s, max, format, tm );

// This check is not watertight:
//  "Note that the return value 0 does not necessarily indicate an error.  For example, in many locales %p
//   yields an empty string.  An empty format string will likewise yield an empty string."
// But it should suffice for our purposes.
if ( 0 == res )
{
throw std::runtime_error( "Error calling strftime()." );
}

return res;
}



How can I prevent the following compilation warning on the strftime() call above?

warning: format not a string literal, format string not checked

Note that I am already trying with __attribute__ format to no avail. I am using GCC 7.3 for an embedded ARM Cortex-M4F target

Somebody else asked this same question in the past, but I saw no definitive answer:

https://gcc.gnu.org/ml/gcc-help/2009-02/msg00246.html

Please copy me on all the answers, for I am not subscribed to this list.


Thanks in advance,
  rdiez



More information about the Gcc-help mailing list