Skip to contentSkip to conversation

hwmon: (pmbus/core) Replace deprecated strncpy() with strscpy()

PATCH Applied Thorsten Blum 2 participants 4 messages Feb 27, 2025 – Mar 2, 2025 Hardware monitoring

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hwmon: (pmbus/core) Replace deprecated strncpy() with strscpy()

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.

Compile-tested only.

---
 drivers/hwmon/pmbus/pmbus_core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/hwmon/pmbus/pmbus_core.c +1 -2
index 787683e83db6..cdde8b03a6e9 100644
@@ -1470,8 +1470,7 @@ static int pmbus_add_label(struct pmbus_data *data,
 	snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
 	if (!index) {
 		if (phase == 0xff)
-			strncpy(label->label, lstring,
-				sizeof(label->label) - 1);
+			strscpy(label->label, lstring);
 		else
 			snprintf(label->label, sizeof(label->label), "%s.%d",
 				 lstring, phase);
Signature · 1 line
2.48.1
maintainer
#1 lore raw
Quoted 17 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.

Compile-tested only.

---
  drivers/hwmon/pmbus/pmbus_core.c | 3 +--
  1 file changed, 1 insertion(+), 2 deletions(-)
drivers/hwmon/pmbus/pmbus_core.c +0 -0
index 787683e83db6..cdde8b03a6e9 100644

drivers/hwmon/pmbus/pmbus_core.c static int pmbus_add_label(struct pmbus_data *data, · line 1473

Quoted 6 lines
drivers/hwmon/pmbus/pmbus_core.c +1 -2
@@ -1470,8 +1470,7 @@ static int pmbus_add_label(struct pmbus_data *data,
  	snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
  	if (!index) {
  		if (phase == 0xff)
-			strncpy(label->label, lstring,
-				sizeof(label->label) - 1);
+			strscpy(label->label, lstring);

What guarantees that strlen(lstring) < sizeof(label->label) ?

Guenter

Quoted 3 lines
drivers/hwmon/pmbus/pmbus_core.c +0 -0
  		else
  			snprintf(label->label, sizeof(label->label), "%s.%d",
  				 lstring, phase);
#2 lore raw
Quoted 10 lines · Guenter Roeck
Quoted 7 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.
Compile-tested only.

---

What guarantees that strlen(lstring) < sizeof(label->label) ?

Hi Guenter, I don't think it matters for this patch.

If lstring >= label, strscpy() behaves the same as strncpy() because the size argument is now one byte larger.

If lstring < label, strscpy() NUL-terminates the destination buffer, but doesn't add NUL-padding compared to strncpy(). However, this doesn't matter because label is already zero-initialized.

Thanks,
Thorsten

maintainer Applied
#3 lore raw
In reply to Thorsten Blum
Quoted 8 lines · Thorsten Blum

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead.

Compile-tested only.

Applied.

Quoted 9 lines
---
 drivers/hwmon/pmbus/pmbus_core.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/hwmon/pmbus/pmbus_core.c +0 -0
index 787683e83db6..cdde8b03a6e9 100644

drivers/hwmon/pmbus/pmbus_core.c static int pmbus_add_label(struct pmbus_data *data, · line 1473

Quoted 6 lines
drivers/hwmon/pmbus/pmbus_core.c +1 -2
@@ -1470,8 +1470,7 @@ static int pmbus_add_label(struct pmbus_data *data,
 	snprintf(label->name, sizeof(label->name), "%s%d_label", name, seq);
 	if (!index) {
 		if (phase == 0xff)
-			strncpy(label->label, lstring,
-				sizeof(label->label) - 1);
+			strscpy(label->label, lstring);

I added a note explaining that strscpy() uses sizeof(label->label) if the length of the destination buffer is not provided.

Guenter