Re: strncpy clarify result may not be null terminated

Xi Ruoyao
2023-11-09T12:51:41Z

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.

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