All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] scsi: pm8001: avoid -Wrestrict warning
@ 2021-03-23 12:54 Arnd Bergmann
  2021-03-24 10:56 ` Jinpu Wang
  2021-03-30  3:54 ` Martin K. Petersen
  0 siblings, 2 replies; 3+ messages in thread
From: Arnd Bergmann @ 2021-03-23 12:54 UTC (permalink / raw)
  To: Jack Wang, James E.J. Bottomley, Martin K. Petersen
  Cc: Arnd Bergmann, Viswas G, Ruksar Devadi, Joe Perches,
	Vaibhav Gupta, Christophe JAILLET, Lee Jones, linux-scsi,
	linux-kernel

From: Arnd Bergmann <arnd@arndb.de>

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.

Link: https://godbolt.org/z/6T1qPM
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
 drivers/scsi/pm8001/pm8001_init.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/drivers/scsi/pm8001/pm8001_init.c b/drivers/scsi/pm8001/pm8001_init.c
index bd626ef876da..a268c647b987 100644
--- a/drivers/scsi/pm8001/pm8001_init.c
+++ b/drivers/scsi/pm8001/pm8001_init.c
@@ -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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] scsi: pm8001: avoid -Wrestrict warning
  2021-03-23 12:54 [PATCH] scsi: pm8001: avoid -Wrestrict warning Arnd Bergmann
@ 2021-03-24 10:56 ` Jinpu Wang
  2021-03-30  3:54 ` Martin K. Petersen
  1 sibling, 0 replies; 3+ messages in thread
From: Jinpu Wang @ 2021-03-24 10:56 UTC (permalink / raw)
  To: Arnd Bergmann
  Cc: Jack Wang, James E.J. Bottomley, Martin K. Petersen,
	Arnd Bergmann, Viswas G, Ruksar Devadi, Joe Perches,
	Vaibhav Gupta, Christophe JAILLET, Lee Jones,
	Linux SCSI Mailinglist, open list

On Tue, Mar 23, 2021 at 1:55 PM Arnd Bergmann <arnd@kernel.org> wrote:
>
> From: Arnd Bergmann <arnd@arndb.de>
>
> 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.
>
> Link: https://godbolt.org/z/6T1qPM
> Signed-off-by: Arnd Bergmann <arnd@arndb.de>
Acked-by: Jack Wang <jinpu.wang@ionos.com>
> ---
>  drivers/scsi/pm8001/pm8001_init.c | 6 +++++-
>  1 file changed, 5 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/scsi/pm8001/pm8001_init.c b/drivers/scsi/pm8001/pm8001_init.c
> index bd626ef876da..a268c647b987 100644
> --- a/drivers/scsi/pm8001/pm8001_init.c
> +++ b/drivers/scsi/pm8001/pm8001_init.c
> @@ -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
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] scsi: pm8001: avoid -Wrestrict warning
  2021-03-23 12:54 [PATCH] scsi: pm8001: avoid -Wrestrict warning Arnd Bergmann
  2021-03-24 10:56 ` Jinpu Wang
@ 2021-03-30  3:54 ` Martin K. Petersen
  1 sibling, 0 replies; 3+ messages in thread
From: Martin K. Petersen @ 2021-03-30  3:54 UTC (permalink / raw)
  To: James E.J. Bottomley, Arnd Bergmann, Jack Wang
  Cc: Martin K . Petersen, Joe Perches, Viswas G, Lee Jones,
	Vaibhav Gupta, linux-kernel, Arnd Bergmann, linux-scsi,
	Ruksar Devadi, Christophe JAILLET

On Tue, 23 Mar 2021 13:54:23 +0100, Arnd Bergmann wrote:

> 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)
>       |                                ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
> 
> [...]

Applied to 5.13/scsi-queue, thanks!

[1/1] scsi: pm8001: avoid -Wrestrict warning
      https://git.kernel.org/mkp/scsi/c/c2255ece2be2

-- 
Martin K. Petersen	Oracle Linux Engineering

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-03-30  3:56 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-23 12:54 [PATCH] scsi: pm8001: avoid -Wrestrict warning Arnd Bergmann
2021-03-24 10:56 ` Jinpu Wang
2021-03-30  3:54 ` Martin K. Petersen

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.