Skip to contentSkip to conversation

btrfs: Replace deprecated strncpy() with strscpy_pad()

PATCH Superseded Thorsten Blum 2 participants 3 messages Feb 25, 2025 Btrfs file system

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 btrfs: Replace deprecated strncpy() with strscpy_pad()

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and don't zero-initialize the param array.

Compile-tested only.

---
 fs/btrfs/sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/sysfs.c +2 -2
index 53b846d99ece..b941fb37776d 100644
@@ -1330,13 +1330,13 @@ MODULE_PARM_DESC(read_policy,
 
 int btrfs_read_policy_to_enum(const char *str, s64 *value_ret)
 {
-	char param[32] = { 0 };
+	char param[32];
 	char __maybe_unused *value_str;
 
 	if (!str || strlen(str) == 0)
 		return 0;
 
-	strncpy(param, str, sizeof(param) - 1);
+	strscpy_pad(param, str);
 
 #ifdef CONFIG_BTRFS_EXPERIMENTAL
 	/* Separate value from input in policy:value format. */
Signature · 1 line
2.48.1
#1 lore raw
Quoted 19 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and don't zero-initialize the param array.

Compile-tested only.

---
 fs/btrfs/sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/btrfs/sysfs.c +0 -0
index 53b846d99ece..b941fb37776d 100644
@@ -1330,13 +1330,13 @@ MODULE_PARM_DESC(read_policy,
 
 int btrfs_read_policy_to_enum(const char *str, s64 *value_ret)

fs/btrfs/sysfs.c MODULE_PARM_DESC(read_policy, · line 1339

Quoted 10 lines
fs/btrfs/sysfs.c +2 -2
 {
-	char param[32] = { 0 };
+	char param[32];
 	char __maybe_unused *value_str;
 
 	if (!str || strlen(str) == 0)
 		return 0;
 
-	strncpy(param, str, sizeof(param) - 1);
+	strscpy_pad(param, str);

I don't think we need the padding, so strscpy is probably ok. We copy input string to a temporary modifiable buffer and then match it against a table, so the NUL termination is ok.

#2 lore raw
Quoted 6 lines · David Sterba
Quoted 5 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers. Use strscpy_pad() instead and don't zero-initialize the param array.

Compile-tested only.

Quoted 8 lines
Quoted 4 lines

---

I don't think we need the padding, so strscpy is probably ok. We copy input string to a temporary modifiable buffer and then match it against a table, so the NUL termination is ok.

Ok thanks, I'll submit a v2 later today.