Skip to contentSkip to conversation

staging: vme_user: replace strcpy with strscpy

PATCH Superseded Calvince Otieno 3 participants 3 messages Oct 18, 2023

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: vme_user: replace strcpy with strscpy

Checkpatch suggests using strscpy() instead of strncpy().

The advantages of strscpy() are that it always adds a NUL terminator and prevents read overflows if the source string is not properly terminated. One potential disadvantage is that it doesn't zero pad the string like strncpy() does.

In this code, strscpy() and strncpy() are equivalent and do not affect runtime behavior. strscpy() simply copies the known string value of the variable driver_name into the fake_bridge->name variable, which also has a fixed size.

While using strscpy() does not address any bugs, it is considered a better practice and aligns with checkpatch recommendations.

---
 drivers/staging/vme_user/vme_fake.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/vme_user/vme_fake.c +2 -1
index 0e02c194298d..09b05861017a 100644
@@ -67,6 +67,7 @@ struct fake_driver {
 	unsigned long long lm_base;
 	u32 lm_aspace;
 	u32 lm_cycle;
+
 	void (*lm_callback[4])(void *);
 	void *lm_data[4];
 	struct tasklet_struct int_tasklet;
@@ -1091,7 +1092,7 @@ static int __init fake_init(void)
 	tasklet_init(&fake_device->int_tasklet, fake_VIRQ_tasklet,
 		     (unsigned long)fake_bridge);
 
-	strcpy(fake_bridge->name, driver_name);
+	strscpy(fake_bridge->name, driver_name, sizeof(fake_bridge->name))
 
 	/* Add master windows to list */
 	INIT_LIST_HEAD(&fake_bridge->master_resources);
Signature · 1 line
2.34.1
#1 lore raw
Quoted 25 lines · Calvince Otieno

Checkpatch suggests using strscpy() instead of strncpy().

The advantages of strscpy() are that it always adds a NUL terminator and prevents read overflows if the source string is not properly terminated. One potential disadvantage is that it doesn't zero pad the string like strncpy() does.

In this code, strscpy() and strncpy() are equivalent and do not affect runtime behavior. strscpy() simply copies the known string value of the variable driver_name into the fake_bridge->name variable, which also has a fixed size.

While using strscpy() does not address any bugs, it is considered a better practice and aligns with checkpatch recommendations.

---
 drivers/staging/vme_user/vme_fake.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/staging/vme_user/vme_fake.c +0 -0
index 0e02c194298d..09b05861017a 100644

drivers/staging/vme_user/vme_fake.c struct fake_driver { · line 72

Quoted 6 lines
drivers/staging/vme_user/vme_fake.c +1 -0
@@ -67,6 +67,7 @@ struct fake_driver {
 	unsigned long long lm_base;
 	u32 lm_aspace;
 	u32 lm_cycle;
+
 	void (*lm_callback[4])(void *);
 	void *lm_data[4];

Why did you make this extra line change?

drivers/staging/vme_user/vme_fake.c static int __init fake_init(void) · line 1095

Quoted 7 lines
drivers/staging/vme_user/vme_fake.c +1 -1
 	struct tasklet_struct int_tasklet;
@@ -1091,7 +1092,7 @@ static int __init fake_init(void)
 	tasklet_init(&fake_device->int_tasklet, fake_VIRQ_tasklet,
 		     (unsigned long)fake_bridge);
 
-	strcpy(fake_bridge->name, driver_name);
+	strscpy(fake_bridge->name, driver_name, sizeof(fake_bridge->name))

Are you sure this change is identical? You need to document how you have proved that.

thanks,

greg k-h

#2 lore raw
In reply to Calvince Otieno
Quoted 6 lines · Calvince Otieno

Checkpatch suggests using strscpy() instead of strncpy().

The advantages of strscpy() are that it always adds a NUL terminator and prevents read overflows if the source string is not properly terminated. One potential disadvantage is that it doesn't zero pad the string like strncpy() does.

You're not replacing strncpy(), you're replacing strcpy(). There is never a downside to replacing strcpy() with strspy() beyond that the secure function is probably slightly slower.

Quoted 8 lines

In this code, strscpy() and strncpy() are equivalent and do not affect runtime behavior. strscpy() simply copies the known string value of the variable driver_name into the fake_bridge->name variable, which also has a fixed size.

While using strscpy() does not address any bugs, it is considered a better practice and aligns with checkpatch recommendations.

This analysis does not say where driver_name is set, or how big it is, or what the size of the fake_bridge->name buffer is. I would like to see that sort of analysis in the commit message.

regards,
dan carpenter