GCC 12.1 Released
GCC 12.1 Released
Posted May 9, 2022 7:45 UTC (Mon) by wtarreau (subscriber, #51152)In reply to: GCC 12.1 Released by wtarreau
Parent article: GCC 12.1 Released
... and that started already with a new awesome warning, it didn't take long! Note, this one is implified, it instead complains at plenty of places where controls were already in place.
$ cat thankyougcc12.c
#include <sys/param.h>
#include <stdio.h>
#include <string.h>
char dir[MAXPATHLEN];
char file[MAXPATHLEN];
char fullpath[MAXPATHLEN];
/* returns -1 in case of error */
int makefullpath()
{
if ((strlen(dir) + 1 + strlen(file) + 1) > sizeof(fullpath))
return -1;
snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, file);
return 0;
}
$ x86_64-linux-gcc -O2 -Wall-c thankyougcc12.c
thankyougcc12.c: In function 'makefullpath':
thankyougcc12.c:15:50: warning: '%s' directive output may be truncated writing up to 4094 bytes into a region of size between 1 and 4095 [-Wformat-truncation=]
15 | snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, file);
| ^~ ~~~~
thankyougcc12.c:15:9: note: 'snprintf' output between 2 and 8190 bytes into a destination of size 4096
15 | snprintf(fullpath, sizeof(fullpath), "%s/%s", dir, file);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sure... I just performed the length check before calling snprintf() and it believes I'm trying to stuff the sum of these in this string. So I have two options, either I conclude that I can remove all my now useless length checks (since gcc12 doesn't trust them, so possibly it optimised them away, not checked) or I'll simply disable that warning that became stupid.
And it's really the control fro the previous check that is wrong, because if I lower the limit on the sump of strlen() in the first check to sizeof/2, it accepts to pass! So it looks like they've implemented a string length test for snprintf() that didn't consider that two strings could be concatenated by a single call (yes we can do that!). It would be nice if they only enabled warnings after they tested that they actually work on real code.
It's sad that each and every new version forces you to disable useful warnings that once used to be valid and became useless over time, it does render the code less secure by letting stupid bugs slip through. Because of this, in the long term I'll probably end up writing my own function and stop calling it snprintf() directly so that it stops being smart. Too bad if I introduce new bugs in this action.
What would be needed would be a diagnostic mode where you ask for suggestions or "are you sure" only as a developer, but not stuff like this that prove the compiler didn't understand the code but will cause build breakage at users', and it completely discourages programmers from putting error checks in their code since regardless of what was done, the compiler complains anyway.
Ah, GNU Complainers Collection, I really love you :-(
