Re: [PATCH] xfs: xattr: replace strncpy and check for truncation

From: Christoph Hellwig
Date: Tue Apr 09 2024 - 09:36:56 EST


On Fri, Apr 05, 2024 at 07:45:08PM +0000, Justin Stitt wrote:
> - memcpy(offset, prefix, prefix_len);
> - offset += prefix_len;
> - strncpy(offset, (char *)name, namelen); /* real name */
> - offset += namelen;
> - *offset = '\0';
> +
> + combined_len = prefix_len + namelen;
> +
> + /* plus one byte for \0 */
> + actual_len = scnprintf(offset, combined_len + 1, "%s%s", prefix, name);
> +
> + if (actual_len < combined_len)

Shouldn't this be a != ?

That being said I think this is actually wrong - the attr names are
not NULL-terminated on disk, which is why we have the explicit
zero terminataion above.

How was this tested?