Welcome to the Builder Academy

Question snprintf buffer length warnings on compile

More
03 Jun 2025 20:35 #10750 by Salty
I'm using tbaMUD 2018.  When doing a full compile there's lots of buffer length errors related to snprintf().  In the later versions of tbaMUD have these errors been fixed?

If so, is there a diff I can grab?

I'm using:
Code:
Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-linux-gnu/13/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu 13.3.0-6ubuntu2~24.04' --with-bugurl=file:///usr/share/doc/gcc-13/README.Bugs --enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-13 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-13-fG75Ri/gcc-13-13.3.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-13-fG75Ri/gcc-13-13.3.0/debian/tmp-gcn/usr --enable-offload-defaulted --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2 Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04)

Would gcc 14.3 or 15.1 be an advisable upgrade?

Thanks.

Please Log in or Create an account to join the conversation.

More
04 Jun 2025 19:54 #10752 by thomas
Check out the newest version of tba with "git clone github.com/tbamud/tbamud.git "
Find any changes since 2018-release with "git diff 544afc7..HEAD"

Mind you - this gives you _everything_, so it might not patch cleanly.

Please Log in or Create an account to join the conversation.

More
09 Jun 2025 18:50 #10755 by Salty
Thomas,

I've gone through the truncation errors line by line and they're not fixed in the latest version on github.  I'm loathe to increase MAX_INPUT_LENGTH arbitrarily just to remove the errors and it also seems non-trivial to include -Wno-format-truncation to the makefile.

Also, there's an overflow error in improved-edit.c that puzzles me and before I change the size of buf3 I want to get your opinion on this:

Here's the error on compile:
Code:
improved-edit.c: In function ‘parse_edit_action’: improved-edit.c:371:27: warning: ‘: ’ directive writing 2 bytes into a region of size between 0 and 6 [-Wformat-overflow=]   371 |         sprintf(buf3, "%4d: ", (i - 1));       |                           ^~ In file included from /usr/include/stdio.h:980,                  from sysdep.h:69,                  from improved-edit.c:7: In function ‘sprintf’,     inlined from ‘parse_edit_action’ at improved-edit.c:371:9: /usr/include/x86_64-linux-gnu/bits/stdio2.h:30:10: note: ‘__builtin___sprintf_chk’ output between 7 and 13 bytes into a destination of size 10    30 |   return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1,       |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    31 |                                   __glibc_objsize (__s), __fmt,       |                                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~    32 |                                   __va_arg_pack ());       |                                   ~~~~~~~~~~~~~~~~~

And here's the code from unmodified file from 2018:
Code:
    while (s && i <= line_high)       if ((s = strchr(s, '\n')) != NULL) {         i++;         total_len++;         s++;         temp = *s;         *s = '\0';         char buf3[10];         sprintf(buf3, "%4d: ", (i - 1));         strncat(buf, buf3, sizeof(buf) - strlen(buf) - 1);         strncat(buf, t, sizeof(buf) - strlen(buf) - 1);         *s = temp;         t = s;       }
And the code from 2025:
Code:
    while (s && i <= line_high)       if ((s = strchr(s, '\n')) != NULL) {         i++;         total_len++;         s++;         temp = *s;         *s = '\0';         char buf3[9];         sprintf(buf3, "%4d: ", (i - 1));         strncat(buf, buf3, sizeof(buf) - strlen(buf) - 1);         strncat(buf, t, sizeof(buf) - strlen(buf) - 1);         *s = temp;         t = s;       }

Am I tilting at windmills or is there a reasonable resolution to these errors?

Thanks.

Please Log in or Create an account to join the conversation.

More
09 Jun 2025 19:59 #10756 by thomas
Ah, yes, somewhere "i" is declared as an int, so the %4d pattern could in theory become MAX_INT, 2147483647, meaning that the string would overflow. This doesn't happen, so the warning is just a warning. If you want it to go away, alter the size of buf3:
Code:
-char buf3[9]; +char buf3[20];

Please Log in or Create an account to join the conversation.

Time to create page: 0.227 seconds