Correcting "format string is not a string literal" warning

The warning flag that enables this type of warning is -Wformat-nonliteral. Since you don't want to turn that warning off completely, you can locally disable this warning using the following code:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"

...

#pragma clang diagnostic pop

So your function would look like this:

int vasprintf_wrapper(char** bufptr, const char* fmt, va_list ap)
{
    // Do stuff...
    // ...

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wformat-nonliteral"
    return vasprintf(bufptr, fmt, ap);
#pragma clang diagnostic pop
}

Indicate a parameter is a printf-style format using the __attribute__ flag. For example:

__attribute__((__format__ (__printf__, 2, 0)))
int vasprintf_wrapper(char** bufptr, const char* fmt, va_list ap)
{
  ...
}

The last parameter (0) disables checking for va_list.

From the documentation:

format (archetype, string-index, first-to-check)

The format attribute specifies that a function takes printf-, scanf-, strftime-, or strfmon-style arguments that should be type-checked against a format string.

The parameter archetype determines how the format string is interpreted.

The parameter string-index specifies which argument is the format string argument (starting from 1).

The parameter first-to-check is the number of the first argument to check against the format string. For functions where the arguments are not available to be checked (such as vprintf), specify the third parameter as zero.

See also:

  • http://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html#Common-Function-Attributes
  • http://clang.llvm.org/docs/AttributeReference.html#format

Tags:

C++

Clang