[cfe-dev] Incorrect warning with -Wformat=2

Ted Kremenek kremenek at apple.com
Mon Nov 15 13:08:36 PST 2010


This looks like a bug.  Please file a bug report so we can track its resolution.

On Nov 15, 2010, at 6:33 AM, Csaba Raduly wrote:

> Hi all,
> The following code:
> 
> enum error_type_t {
>  none, warn, err, fatal
> };
> 
> void error(error_type_t p_et, const char *fmt, ...)
>    __attribute__ ((__format__ (__printf__, 2, 3)))
>     ;
> 
> class ErrorContext {
> public:
> 
>  ErrorContext();
> 
>  ErrorContext(const char *fmt, ...)
>    __attribute__ ((__format__ (__printf__, 2, 3))); // 'this' is at 1
> 
>  ~ErrorContext();
> 
>  static void error(error_type_t p_et, const char *fmt, ...)
>    __attribute__ ((__format__ (__printf__, 2, 3)))
>      ;
> 
>  static void error_internal(const char *fmt, ...)
>    __attribute__ ((__format__ (__printf__, 1, 2), __noreturn__));
> };
> 
> int main(int argc, char **argv)
> {
>  if (argc < 2 ) {
>    ErrorContext ec("While checking arguments");
>    if (argc < 1) {
>      ec.error_internal("argc=%d is not possible", argc);
>    }
>    ec.error(fatal, "Not enough arguments for %s", argv[0]); // line 34
>    error   (fatal, "Not enough arguments for %s", argv[0]);
>  }
>  return 0;
> }
> 
> and the following commandline
> 
> clang++ -c -Wformat=2 example.cc
> 
> generates a spurious warning:
> 
> example.cc:34:14: warning: format string is not a string literal
> [-Wformat-nonliteral]
>    ec.error(fatal, "Not enough arguments for %s", argv[0]);
>             ^~~~~
> 
> The handling of the static method "error" seems to be inconsistent
> with the free function "error" (there is no warning for line 35).
> 
> $ clang++ --version
> clang version 2.9 (trunk 119134)
> Target: x86_64-unknown-linux-gnu
> Thread model: posix
> 
> -- 
> GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
> Life is complex, with real and imaginary parts.
> "Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
> "People disagree with me. I just ignore them." -- Linus Torvalds
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev





More information about the cfe-dev mailing list