Menu

#38 crrc_system.cpp:177:8: warning: '%s' directive output may be truncated

open
nobody
fedora (5)
5
2017-12-22
2017-12-21
No

Building crrcsim v0.9.13 for Fedora produces the following warning[1]:

g++ -DHAVE_CONFIG_H -I.    -pthread  -I/usr/include/SDL -D_GNU_SOURCE=1 -D_REENTRANT -frounding-math -DPU_USE_SDL -DCRRC_DATA_PATH="\"/usr/share/crrcsim\"" -O2 -g -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -mtune=generic -Dlinux -Wall -c -o crrcsim-aircraft.o `test -f 'src/aircraft.cpp' || echo './'`src/aircraft.cpp
src/crrc_system.cpp: In function 'char* getOSVersionString()':
src/crrc_system.cpp:177:8: warning: '%s' directive output may be truncated writing up to 64 bytes into a region of size between 62 and 126 [-Wformat-truncation=]
 char * getOSVersionString()
        ^~~~~~~~~~~~~~~~~~
In file included from /usr/include/stdio.h:862:0,
                 from /usr/include/c++/7/cstdio:42,
                 from /usr/include/c++/7/ext/string_conversions.h:43,
                 from /usr/include/c++/7/bits/basic_string.h:6347,
                 from /usr/include/c++/7/string:52,
                 from src/crrc_system.h:55,
                 from src/crrc_system.cpp:52:
/usr/include/bits/stdio2.h:65:44: note: '__builtin___snprintf_chk' output between 2 and 130 bytes into a destination of size 127
        __bos (__s), __fmt, __va_arg_pack ());
                                            ^

[1] https://kojipkgs.fedoraproject.org//work/tasks/2391/23822391/build.log

Discussion

  • Jan Reucker

    Jan Reucker - 2017-12-22

    I don't really understand this warning. I'm using snprintf to explicitly truncate the output at the end of the buffer, yet I receive a warning that output is truncated? Hmm, I tend to disable this warning.

    The other option would be to enlarge the target buffer by setting OSVERSIONSTRINGBUFFERLEN (line 91) to something like 256 or 512. Could you please try if that helps?

     
  • Damian Wrobel

    Damian Wrobel - 2017-12-22

    output between 2 and 130 bytes into a destination of size 127

    It means that content from sysname and release fields requires 130 bytes but you have only 127.

    If you don't have any plans for dynamically allocate the buffer and prefer to keep the following define untouched:

    #define OSVERSIONSTRINGBUFFERLEN (128)
    

    then please consider to use the following patch:

    $ hg di crrc_system.cpp 
    diff -r 41dc80721fb1 src/crrc_system.cpp
    --- a/src/crrc_system.cpp       Fri Dec 22 15:26:23 2017 +0100
    +++ b/src/crrc_system.cpp       Fri Dec 22 16:32:54 2017 +0100
    @@ -194,7 +194,7 @@
       if (uname(prInfo) == 0)
       {
         snprintf(cOSVersionStringBuffer, OSVERSIONSTRINGBUFFERLEN - 1,
    -            "%s %s", prInfo->sysname, prInfo->release);
    +            "%.62s %.62s", prInfo->sysname, prInfo->release);
       }
       else
       {
    
     

Log in to post a comment.