Re: strncpy clarify result may not be null terminated

Alejandro Colomar
2023-11-20T15:08:55Z

[-- Attachment #1: Type: text/plain, Size: 1133 bytes --]

Hi Jonny,

On Mon, Nov 20, 2023 at 11:56:40AM +0000, Jonny Grant wrote:
> BTW, GCC has a useful warning for truncation that may help code bases that use strncpy, you've probably seen this and the article, just sharing for completeness.

It's actually the opposite. GCC's warnings about strncpy(3) are
nefarious, as it warns in valid uses of strncpy(3) for writing a
null-padded character sequence (the use for which strncpy(3) was
designed), recommending the bogus use as a function for copying
truncated strings.

>
> warning: ‘__builtin_strncpy’ output truncated before terminating nul copying XYZ bytes from a string of the same length [-Wstringop-truncation]
>
>
> Martin's article from 2019
> https://developers.redhat.com/blog/2018/05/24/detecting-string-truncation-with-gcc-8#forming_truncated_strings_with_snprintf

I discussed with Martin about this, IIRC, and he told me they had to
decide which use of strncpy(3) to support, with the side effect that
other uses would be warned about, and they chose the one that I think is
bogus.

Cheers,
Alex

--
<https://www.alejandro-colomar.es/>