Re: strncpy clarify result may not be null terminated

Alejandro Colomar
2023-11-20T23:39:23Z

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

Hi Jonny,

On Mon, Nov 20, 2023 at 11:08:58PM +0000, Jonny Grant wrote:
> > 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.
>
> Fair enough.

To be fair with Martin and GCC, the uses of strncpy(3) that I consider
correct are so trivial that those warnings are unnecessary, since one
should always use sizeof(dst) in the call, which can be done by a
wrapper macro

#define STRNCPY(dst, src) strncpy(dst, src, nitems(dst))

which is precisely what I did in shadow-utils. With this, the chances
of getting the size wrong are 0, so I'd just turn off those warnings.

Since strncpy(3) should always be used for writing to a fixed-size
array, it's likely to be an actual array, of which you can take the
size with nitems(). At least in shadow-utils, all calls have been
replaced by that macro. I'm curious if all uses are similarly trivial
in tar(1).

So if this warning helps those who misuse strncpy(3) to at least misuse
it safely, then it's a partially-good thing.

Cheers,
Alex

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