Re: strncpy clarify result may not be null terminated

Alejandro Colomar
2023-11-09T14:01:26Z

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

On Thu, Nov 09, 2023 at 08:51:34PM +0800, Xi Ruoyao wrote:
> On Thu, 2023-11-09 at 12:38 +0100, Alejandro Colomar wrote:
> > If you are consistent in checking the return value of strlcpy(3) and
> > reporting an error, it's the best standard alternative nowadays.
> > snprintf(3), except for using int instead of size_t, has an equivalent
> > API, and is in C99, in case that means something.
>
> Yes, you can always create your own wrapper instead of demanding a
> standard function which must be implemented by every libc.
>
> > If you would want to write something based on Michael Kerrisk's article,
> > you could do this:
>
> > ssize_t
> > strxcpy(char *restrict dst, char *restrict src, size_t dsize)
> > {
> > if (strlen(src) < dsize)
> > return -1;
> >
> > strcpy(dst, src);
> > }
>
> I'd like to add __attribute__ ((warn_unused_result)) for this wrapper as
> well.

Indeed. Thanks!

>
> --
> Xi Ruoyao <xry111@xry111.site>
> School of Aerospace Science and Technology, Xidian University

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