[Bug c/79082] -Wformat-truncation inconsistent behaviour
msebor at gcc dot gnu.org
gcc-bugzilla@gcc.gnu.org
Mon Jan 30 16:00:00 GMT 2017
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79082
--- Comment #12 from Martin Sebor <msebor at gcc dot gnu.org> ---
(In reply to Sylvestre Ledru from comment #11)
> Other testcase:
> ---
> #include <stdio.h>
> #include <stdint.h>
> int main() {
> char* buf = new char[10];
> uint8_t data = 255;
> printf("%.2x\n", data); // just print data
> snprintf(&buf[0], 2, "%.2x", data);
> return 0;
> }
> ---
>
> foo.c:7:13: note: format output 3 bytes into a destination of size 2
> snprintf(&buf[0], 2, "%.2x", data);
> ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
This is warning is valid. The function is called with a buffer size of 2 bytes
but the total output for 255 in hex ("ff") and the terminating nul would be 3
bytes. The output is truncated after the first 'f' which is usually a bug.
More information about the Gcc-bugs
mailing list