[R-pkg-devel] truncating strings on purpose
Hi Ivan, List,
Thanks for the suggestion, and for the pointer on duplicating the error.
The good news is that I am able to trigger the warning locally now. The bad
news is that following the gcc docs on avoid the warning did not seem to
work:
gcc -std=gnu99 -fsanitize=undefined -fno-omit-frame-pointer -std=gnu11
-I"/usr/local/lib/R/include" -DNDEBUG -I/usr/local/include
-fsanitize=address
-O2 -Wstringop-truncation -fPIC -g -O2 -c input2.c -o input2.o
input2.c: In function ?inperrmsg?:
input2.c:863:5: warning: ?strncpy? output may be truncated copying 225
bytes from a string of length 255 [-Wstringop-truncation]
863 | strncpy(trunc_tok, tok, sizeof trunc_tok - 1);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I could also try memcpy, but i don't so much like the idea of going away
from string functions for string handling.
Other suggestions are also welcome.
Brad
On Wed, Dec 11, 2024 at 1:29?PM Ivan Krylov <ikrylov at disroot.org> wrote:
? Wed, 11 Dec 2024 12:12:11 +0000 Brad Eck <bradleyjeck at gmail.com> ?????:
Found the following significant warnings:
input2.c:863:5: warning: ?__builtin_strncpy? output may be
truncated copying 225 bytes from a string of length 255
[-Wstringop-truncation]
Where the truncation warnings appear, I am truncating on purpose using `strncpy` to avoid overflows later. I believe this works fine from an application point of view but the warnings remain.
GCC documentation suggests setting the length argument to _one less_ than sizeof destination buffer in addition to terminating the string: https://gcc.gnu.org/onlinedocs/gcc-14.2.0/gcc/Warning-Options.html#index-Wstringop-truncation strncpy(trunc_s1, s1, sizeof trunc_s1 - 1); trunc_s1[sizeof trunc_s1 - 1] = '\0'; -- Best regards, Ivan