https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90036
--- Comment #3 from Dmitry G. Dyachenko <dimhen at gmail dot com> ---
I see smth may be similar starts at r265648 / PR87041
265647 NO warnings
265648 warnings
270581 warnings
$ cat x.cpp
#include <cstdio>
#include <vector>
extern void extf(char*);
extern unsigned U1;
extern unsigned U2;
void foo()
{
if(U1 == 0)
return;
if(U2 == 0)
return;
std::vector<char> N1(U1);
extf(&N1[0]);
std::vector<char> N2(U2);
extf(&N2[0]);
printf("%s = %s\n", &N1[0], &N2[0]); // warning
}
void foo_i(unsigned i)
{
if(i == 0)
return;
if(U2 == 0)
return;
std::vector<char> N1(i);
extf(&N1[0]);
std::vector<char> N2(U2);
extf(&N2[0]);
printf("%s = %s\n", &N1[0], &N2[0]); // warning
}
void foo_j(unsigned j)
{
if(U1 == 0)
return;
if(j == 0)
return;
std::vector<char> N1(U1);
extf(&N1[0]);
std::vector<char> N2(j);
extf(&N2[0]);
printf("%s = %s\n", &N1[0], &N2[0]); // NO warning
}
void foo_2(unsigned i, unsigned j)
{
if(i == 0)
return;
if(j == 0)
return;
std::vector<char> N1(i);
extf(&N1[0]);
std::vector<char> N2(j);
extf(&N2[0]);
printf("%s = %s\n", &N1[0], &N2[0]); // NO warning
}
$ g++ -O2 -Wall -Werror -c x.cpp
x.cpp: In function ‘void foo()’:
x.cpp:22:11: error: ‘%s’ directive argument is null [-Werror=format-overflow=]
22 | printf("%s = %s\n", &N1[0], &N2[0]); // warning
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
x.cpp: In function ‘void foo_i(unsigned int)’:
x.cpp:38:11: error: ‘%s’ directive argument is null [-Werror=format-overflow=]
38 | printf("%s = %s\n", &N1[0], &N2[0]); // warning
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~