ELFkickers
ELFkickers copied to clipboard
elfls.c: Compilation warning about format-overflow (false positive)
Gcc 13 warns:
elfls.c:587:25: warning: ‘%d’ directive writing between 1 and 10
bytes into a region of size 7 [-Wformat-overflow=]
sprintf(sizefmt, "%%%dlX", i);
^~
note: directive argument in the range [6, 2147483647]
Gcc apparently doesn't see i
's upper bound. But I see it: Each loop that increments it can max iterate 16 times.
Actually, Gcc is doubly wrong: If i
was unconstrained, INT_MIN = -2147483648 – 11 bytes – would be the actual size constraint. Of course, C compilers aren't supposed to know that integers can overflow. An unsigned type would eliminate this concern.
Funnily, changing it to an unsigned type, even size_t, silences the warning without increasing the buffer. Clang 17 is happy in any case.
Tip: I think you could avoid these dynamically formatted format strings by using dynamic field widths:
snprintf(buf, sizeof(buf), "%*lX", width, value);