Skip to contentSkip to conversation

scsi: pm8001: avoid -Wrestrict warning

PATCH Applied Arnd Bergmann 3 participants 3 messages Mar 23, 2021 – Mar 30, 2021

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

PATCH 1/1 scsi: pm8001: avoid -Wrestrict warning

From: Arnd Bergmann <[email protected]>

On some configurations, gcc warns about overlapping source and destination arguments to snprintf:

drivers/scsi/pm8001/pm8001_init.c: In function 'pm8001_request_msix':
drivers/scsi/pm8001/pm8001_init.c:977:3: error: 'snprintf' argument 4 may overlap destination object 'pm8001_ha' [-Werror=restrict]

  977 |   snprintf(drvname, len, "%s-%d", pm8001_ha->name, i);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/pm8001/pm8001_init.c:962:56: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
  962 | static u32 pm8001_request_msix(struct pm8001_hba_info *pm8001_ha)
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~

I first assumed this was a gcc bug, as that should not happen, but a reduced test case makes it clear that this happens when the loop counter is not bounded by the array size.

Help the compiler out by adding an explicit limit here to make the code slightly more robust and avoid the warning.

---
 drivers/scsi/pm8001/pm8001_init.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/pm8001/pm8001_init.c +5 -1
index bd626ef876da..a268c647b987 100644
@@ -963,6 +963,7 @@ static u32 pm8001_request_msix(struct pm8001_hba_info *pm8001_ha)
 {
 	u32 i = 0, j = 0;
 	int flag = 0, rc = 0;
+	int nr_irqs = pm8001_ha->number_of_intr;
 
 	if (pm8001_ha->chip_id != chip_8001)
 		flag &= ~IRQF_SHARED;
@@ -971,7 +972,10 @@ static u32 pm8001_request_msix(struct pm8001_hba_info *pm8001_ha)
 		   "pci_enable_msix request number of intr %d\n",
 		   pm8001_ha->number_of_intr);
 
-	for (i = 0; i < pm8001_ha->number_of_intr; i++) {
+	if (nr_irqs > ARRAY_SIZE(pm8001_ha->intr_drvname))
+		nr_irqs = ARRAY_SIZE(pm8001_ha->intr_drvname);
+
+	for (i = 0; i < nr_irqs; i++) {
 		snprintf(pm8001_ha->intr_drvname[i],
 			sizeof(pm8001_ha->intr_drvname[0]),
 			"%s-%d", pm8001_ha->name, i);
Signature · 1 line
2.29.2
Acked-by on v1 1/1
#1 lore raw
Quoted 18 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

On some configurations, gcc warns about overlapping source and destination arguments to snprintf:

drivers/scsi/pm8001/pm8001_init.c: In function 'pm8001_request_msix':
drivers/scsi/pm8001/pm8001_init.c:977:3: error: 'snprintf' argument 4 may overlap destination object 'pm8001_ha' [-Werror=restrict]

  977 |   snprintf(drvname, len, "%s-%d", pm8001_ha->name, i);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/pm8001/pm8001_init.c:962:56: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
  962 | static u32 pm8001_request_msix(struct pm8001_hba_info *pm8001_ha)
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~

I first assumed this was a gcc bug, as that should not happen, but a reduced test case makes it clear that this happens when the loop counter is not bounded by the array size.

Quoted 5 lines

Help the compiler out by adding an explicit limit here to make the code slightly more robust and avoid the warning.

Quoted 31 lines
---
 drivers/scsi/pm8001/pm8001_init.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/scsi/pm8001/pm8001_init.c +5 -2
index bd626ef876da..a268c647b987 100644
@@ -963,6 +963,7 @@ static u32 pm8001_request_msix(struct pm8001_hba_info *pm8001_ha)
 {
        u32 i = 0, j = 0;
        int flag = 0, rc = 0;
+       int nr_irqs = pm8001_ha->number_of_intr;
 
        if (pm8001_ha->chip_id != chip_8001)
                flag &= ~IRQF_SHARED;
@@ -971,7 +972,10 @@ static u32 pm8001_request_msix(struct pm8001_hba_info *pm8001_ha)
                   "pci_enable_msix request number of intr %d\n",
                   pm8001_ha->number_of_intr);
 
-       for (i = 0; i < pm8001_ha->number_of_intr; i++) {
+       if (nr_irqs > ARRAY_SIZE(pm8001_ha->intr_drvname))
+               nr_irqs = ARRAY_SIZE(pm8001_ha->intr_drvname);
+
+       for (i = 0; i < nr_irqs; i++) {
                snprintf(pm8001_ha->intr_drvname[i],
                        sizeof(pm8001_ha->intr_drvname[0]),
                        "%s-%d", pm8001_ha->name, i);
--

2.29.2

#2 lore raw
In reply to Arnd Bergmann
Quoted 5 lines · Arnd Bergmann

On some configurations, gcc warns about overlapping source and destination arguments to snprintf:

drivers/scsi/pm8001/pm8001_init.c: In function 'pm8001_request_msix':
drivers/scsi/pm8001/pm8001_init.c:977:3: error: 'snprintf' argument 4 may overlap destination object 'pm8001_ha' [-Werror=restrict]

Quoted 7 lines
  977 |   snprintf(drvname, len, "%s-%d", pm8001_ha->name, i);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
drivers/scsi/pm8001/pm8001_init.c:962:56: note: destination object referenced by 'restrict'-qualified argument 1 was declared here
  962 | static u32 pm8001_request_msix(struct pm8001_hba_info *pm8001_ha)
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~

[...]

Applied to 5.13/scsi-queue, thanks!

[1/1] scsi: pm8001: avoid -Wrestrict warning

      https://git.kernel.org/mkp/scsi/c/c2255ece2be2
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering