Re: Master and -Wformat-overflow warning

Jeffrey Walton <[email protected]>
Newsgroups gmane.comp.debugging.valgrind
Message-ID <CAH8yC8k1GXEF=we4G-g_i1KRKGRD58aifirGoo3fqArn1_h1iw@mail.gmail.com>
On Thu, May 2, 2019 at 12:02 PM Mark Wielaard <[email protected]> wrote:
>
> On Tue, 2019-04-30 at 20:43 -0400, Jeffrey Walton wrote:
> > It looks like GCC has one squawk:
> >
> > vgdb.c: In function ‘standalone_send_commands’:
> > vgdb.c:1008:21: warning: ‘%02x’ directive writing between 2 and 8
> > bytes into a r
> > egion of size 3 [-Wformat-overflow=]
> >        sprintf(hex, "%02x", cksum);
> >                      ^~~~
> > vgdb.c:1008:20: note: directive argument in the range [0, 2147483647]
> >        sprintf(hex, "%02x", cksum);
> >                     ^~~~~~
>
> But cksum is an unsigned char, so value is be between [0, 255]. Which
> is max 2 hex chars.
>
> Could you retry with GCC8 or GCC9?
> And file a bug against GCC otherwise?

If Valgrind is interested in working around it without increasing the
buffer size, then the format string "%.2x" should do the trick.

$ cat test.c
#include
int main(int argc, char* argv[])
{
    char buf[3];
    sprintf(buf, "%.2x", (unsigned char)argc);
    printf("%s\n", buf);
    return 0;
}

And:

$ gcc -Wall -Wformat-overflow=2 test.c -o test.exe
$ ./test.exe a b c d
05
$ ./test.exe a b c d e f g h i
0a
$ ./test.exe a b c d e f g h i j k l m n o p q r s t u v w x y z
1b


_______________________________________________
Valgrind-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/valgrind-users
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.