https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108518
Bug ID: 108518
Summary: Format-overflow warning using `*.s` directive with
null but zero-length string
Product: gcc
Version: 13.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: aaron at aarongraham dot com
Target Milestone: ---
https://godbolt.org/z/YGra91Woa
#include <stdio.h>
int main() {
// This causes a format-overflow warning, but it
// should not warn if size() is 0
printf("%.*s\n", 0, (char*)0);
}
The warning is:
<source>: In function 'int main()':
<source>:6:13: warning: '%.*s' directive argument is null [-Wformat-overflow=]
6 | printf("%.*s\n", 0, (char*)0);
| ^~~~
I see this commonly when using std::string_view with printf. In cases where it
knows that you're passing a default-constructed string_view it produces this
warning.
It should not produce this warning if the length being printed is 0.