Skip to contentSkip to conversation

xfs: Replace deprecated strncpy() with strscpy()

PATCH In review Thorsten Blum 2 participants 3 messages Mar 5, 2025 XFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 xfs: Replace deprecated strncpy() with strscpy()

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy() instead and remove the manual NUL-termination.

No functional changes intended.

---
 fs/xfs/xfs_xattr.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/xfs/xfs_xattr.c +1 -3
index 0f641a9091ec..9f9a866ab803 100644
@@ -243,9 +243,7 @@ __xfs_xattr_put_listent(
 	offset = context->buffer + context->count;
 	memcpy(offset, prefix, prefix_len);
 	offset += prefix_len;
-	strncpy(offset, (char *)name, namelen);			/* real name */
-	offset += namelen;
-	*offset = '\0';
+	strscpy(offset, (char *)name, namelen + 1);		/* real name */
 
 compute_size:
 	context->count += prefix_len + namelen + 1;
Signature · 1 line
2.48.1
#1 lore raw
Quoted 17 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy() instead and remove the manual NUL-termination.

No functional changes intended.

---
 fs/xfs/xfs_xattr.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
fs/xfs/xfs_xattr.c +0 -0
index 0f641a9091ec..9f9a866ab803 100644

fs/xfs/xfs_xattr.c __xfs_xattr_put_listent( · line 246

Quoted 7 lines
fs/xfs/xfs_xattr.c +1 -3
@@ -243,9 +243,7 @@ __xfs_xattr_put_listent(
 	offset = context->buffer + context->count;
 	memcpy(offset, prefix, prefix_len);
 	offset += prefix_len;
-	strncpy(offset, (char *)name, namelen);			/* real name */
-	offset += namelen;
-	*offset = '\0';
+	strscpy(offset, (char *)name, namelen + 1);		/* real name */

Please read the list archives before you post:
https://lore.kernel.org/linux-xfs/20230211050641.GA2118932@ceph-admin/

If anything, memcpy() would be appropriate here.

--D

Quoted 6 lines
fs/xfs/xfs_xattr.c +0 -0
 compute_size:
 	context->count += prefix_len + namelen + 1;
Signature · 1 line
2.48.1
#2 lore raw
Quoted 6 lines · Darrick J. Wong
Quoted 5 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy() instead and remove the manual NUL-termination.

No functional changes intended.

Quoted 7 lines
Quoted 4 lines

---

Please read the list archives before you post:
https://lore.kernel.org/linux-xfs/20230211050641.GA2118932@ceph-admin/

My bad, I found [1] and saw that you reviewed it, but missed Li's reply.

[1] https://lore.kernel.org/linux-kernel/Y9sKXhxvf0DDusih@magnolia/
[2] https://lore.kernel.org/linux-xfs/20230211050641.GA2118932@ceph-admin/