Bug 82206 - -Wformat-nonliteral does not warn when passing a non literal to vprintf
Summary: -Wformat-nonliteral does not warn when passing a non literal to vprintf
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2017-09-13 21:32 UTC by Simon Marchi
Modified: 2020-06-27 18:35 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments
Reproducer (187 bytes, text/x-csrc)
2017-09-13 21:32 UTC, Simon Marchi
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Marchi 2017-09-13 21:32:26 UTC
Created attachment 42165 [details]
Reproducer

I have extracted this small snippet (see attached file) from the microblaze-tdep.c source file in GDB.  clang gives this warning when passing a non literal to vprintf:

$ clang-4.0 test.c -Wformat -Wformat-nonliteral
test.c:10:12: warning: format string is not a string literal [-Wformat-nonliteral]
  vprintf (fmt, args);
           ^~~
1 warning generated.


gcc 7.2 with the same arguments does not emit any warning.  Would it make sense for gcc to also emit it?
Comment 1 Simon Marchi 2020-06-27 18:35:54 UTC
Stumbled on this again in the GDB code.  Here's another reproducer, probably the same cause:

---
#include <string>
#include <stdarg.h>

void add_line (const std::string &str);
void string_vappendf (std::string &str, const char *fmt, va_list args) __attribute__((format(printf, 2, 0)));

void print(const char *fmt, ...)
{
  std::string tmp;

  va_list ap;
  va_start (ap, fmt);
  string_vappendf (tmp, fmt, ap);
  va_end (ap);

  add_line (tmp);
}
---

$ g++-9 test.cpp -c -Wall -Wextra -Wformat-nonliteral
* no warning *
$ clang++-9 test.cpp -c -Wall -Wextra -Wformat-nonliteral
test.cpp:13:25: warning: format string is not a string literal [-Wformat-nonliteral]
  string_vappendf (tmp, fmt, ap);
                        ^~~
1 warning generated.