Skip to contentSkip to conversation

drivers/scsi/aic7xxx/aic7xxx_osm: Use kstrdup() instead of kmalloc() and strcpy()

PATCH In review [email protected] 2 participants 2 messages Jun 6

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drivers/scsi/aic7xxx/aic7xxx_osm: Use kstrdup() instead of kmalloc() and strcpy()

From: David Laight <[email protected]>

---
This is one of a group of patches that remove potentially unbounded strcpy() calls.

They are mostly replaced by strscpy() or, when strlen() has just been called, with memcpy() (usually including the '\0').

Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.

The changes were made by getting the compiler to detect the calls and then fixing the code by hand.

Note that all the changes are only compile tested.

Some Makefiles were changed to allow files to contain strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to use sysfs_emit() or seq_printf().

All the patches are being sent individually to avoid very long cc lists. Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)

 drivers/scsi/aic7xxx/aic7xxx_osm.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)
drivers/scsi/aic7xxx/aic7xxx_osm.c +3 -5
index d93b522695eb..d6237aa5beac 100644
@@ -1101,12 +1101,10 @@ ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *templa
 	ahc_lock(ahc, &s);
 	ahc_set_unit(ahc, ahc_linux_unit++);
 	ahc_unlock(ahc, &s);
-	sprintf(buf, "scsi%d", host->host_no);
-	new_name = kmalloc(strlen(buf) + 1, GFP_ATOMIC);
-	if (new_name != NULL) {
-		strcpy(new_name, buf);
+	snprintf(buf, sizeof (buf), "scsi%d", host->host_no);
+	new_name = kstrdup(buf, GFP_ATOMIC);
+	if (new_name != NULL)
 		ahc_set_name(ahc, new_name);
-	}
 	host->unique_id = ahc->unit;
 	ahc_linux_initialize_scsi_bus(ahc);
 	ahc_intr_enable(ahc, TRUE);
Signature · 1 line
2.39.5
#1 lore raw

Le 06/06/2026 à 22:26, [email protected] a écrit :

Quoted 34 lines

From: David Laight <[email protected]>

---
This is one of a group of patches that remove potentially unbounded strcpy() calls.

They are mostly replaced by strscpy() or, when strlen() has just been called, with memcpy() (usually including the '\0').

Calls with copy string literals into arrays are left unchanged. They are safe and easily detected as such.

The changes were made by getting the compiler to detect the calls and then fixing the code by hand.

Note that all the changes are only compile tested.

Some Makefiles were changed to allow files to contain strcpy(). As well as 'difficult to fix' files, this included 'show' functions as they really need to use sysfs_emit() or seq_printf().

All the patches are being sent individually to avoid very long cc lists. Apologies for the terse commit messages and likely unexpected tags. (There are about 100 patches in total.)

  drivers/scsi/aic7xxx/aic7xxx_osm.c | 8 +++-----
  1 file changed, 3 insertions(+), 5 deletions(-)
drivers/scsi/aic7xxx/aic7xxx_osm.c +0 -0
index d93b522695eb..d6237aa5beac 100644

drivers/scsi/aic7xxx/aic7xxx_osm.c ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *templa · line 1105

Quoted 9 lines
drivers/scsi/aic7xxx/aic7xxx_osm.c +2 -4
@@ -1101,12 +1101,10 @@ ahc_linux_register_host(struct ahc_softc *ahc, struct scsi_host_template *templa
  	ahc_lock(ahc, &s);
  	ahc_set_unit(ahc, ahc_linux_unit++);
  	ahc_unlock(ahc, &s);
-	sprintf(buf, "scsi%d", host->host_no);
-	new_name = kmalloc(strlen(buf) + 1, GFP_ATOMIC);
-	if (new_name != NULL) {
-		strcpy(new_name, buf);
+	snprintf(buf, sizeof (buf), "scsi%d", host->host_no);
+	new_name = kstrdup(buf, GFP_ATOMIC);

I think that kasprintf() would simplify code and do the same.

CJ

Quoted 6 lines
drivers/scsi/aic7xxx/aic7xxx_osm.c +1 -1
+	if (new_name != NULL)
  		ahc_set_name(ahc, new_name);
-	}
  	host->unique_id = ahc->unit;
  	ahc_linux_initialize_scsi_bus(ahc);
  	ahc_intr_enable(ahc, TRUE);