https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113414
Bug ID: 113414
Summary: Incorrent checking for printf format: does not
distinguish whether the variable is signed or unsigned
Product: gcc
Version: 11.4.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: tongxiangzheng.route at gmail dot com
Target Milestone: ---
When checking the printf() function, gcc does not distinguish whether the
variable is signed or unsigned.
for example:
$ gcc test.cpp
#include<stdio.h>
int main(){
int n;
printf("%u",n);
}
The code should be warned, but it did not.
example 2:
$ gcc test.cpp
#include<stdio.h>
int main(){
unsigned long long int n;
printf("%d",n);
}
The code be warned, but it's wrong:
test.cpp: In function ‘int main()’:
test.cpp:4:18: warning: format ‘%d’ expects argument of type ‘int’, but
argument 2 has type ‘long long unsigned int’ [-Wformat=]
4 | printf("%d",n);
| ~^ ~
| | |
| | long long unsigned int
| int
| %lld
gcc suggests using %lld, but in reality, %llu should be used.