Skip to content
Back to formatted view

Raw Message

Message-ID: <20241211162930.38eb4ba3@arachnoid>
Date: 2024-12-11T13:29:30Z
From: Ivan Krylov
Subject: [R-pkg-devel] truncating strings on purpose
In-Reply-To: <CALECgPaop1dk_Zfxrb-68sJkFfUPtBJ6FHkqMAuNTKbA+t=zPg@mail.gmail.com>

? 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