2024-01-10 22:28:05

by Lad, Prabhakar

[permalink] [raw]
Subject: [PATCH] dmaengine: sh: rz-dmac: Avoid format-overflow warning

From: Lad Prabhakar <[email protected]>

The max channel count for RZ DMAC is 16, hence use u8 instead of unsigned
int and make the pdev_irqname string long enough to avoid the warning.

This fixes the below issue:
drivers/dma/sh/rz-dmac.c: In function ‘rz_dmac_probe’:
drivers/dma/sh/rz-dmac.c:770:34: warning: ‘%u’ directive writing between 1 and 10 bytes into a region of size 3 [-Wformat-overflow=]
770 | sprintf(pdev_irqname, "ch%u", index);
| ^~
In function ‘rz_dmac_chan_probe’,
inlined from ‘rz_dmac_probe’ at drivers/dma/sh/rz-dmac.c:910:9:
drivers/dma/sh/rz-dmac.c:770:31: note: directive argument in the range [0, 4294967294]
770 | sprintf(pdev_irqname, "ch%u", index);
| ^~~~~~
drivers/dma/sh/rz-dmac.c:770:9: note: ‘sprintf’ output between 4 and 13 bytes into a destination of size 5
770 | sprintf(pdev_irqname, "ch%u", index);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

While at it use scnprintf() instead of sprintf() to make the code
more robust.

Signed-off-by: Lad Prabhakar <[email protected]>
---
drivers/dma/sh/rz-dmac.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/dma/sh/rz-dmac.c b/drivers/dma/sh/rz-dmac.c
index fea5bda34bc2..1f1e86ba5c66 100644
--- a/drivers/dma/sh/rz-dmac.c
+++ b/drivers/dma/sh/rz-dmac.c
@@ -755,11 +755,11 @@ static struct dma_chan *rz_dmac_of_xlate(struct of_phandle_args *dma_spec,

static int rz_dmac_chan_probe(struct rz_dmac *dmac,
struct rz_dmac_chan *channel,
- unsigned int index)
+ u8 index)
{
struct platform_device *pdev = to_platform_device(dmac->dev);
struct rz_lmdesc *lmdesc;
- char pdev_irqname[5];
+ char pdev_irqname[6];
char *irqname;
int ret;

@@ -767,7 +767,7 @@ static int rz_dmac_chan_probe(struct rz_dmac *dmac,
channel->mid_rid = -EINVAL;

/* Request the channel interrupt. */
- sprintf(pdev_irqname, "ch%u", index);
+ scnprintf(pdev_irqname, sizeof(pdev_irqname), "ch%u", index);
channel->irq = platform_get_irq_byname(pdev, pdev_irqname);
if (channel->irq < 0)
return channel->irq;
@@ -845,9 +845,9 @@ static int rz_dmac_probe(struct platform_device *pdev)
struct dma_device *engine;
struct rz_dmac *dmac;
int channel_num;
- unsigned int i;
int ret;
int irq;
+ u8 i;

dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
if (!dmac)
--
2.34.1



2024-01-11 09:09:09

by Geert Uytterhoeven

[permalink] [raw]
Subject: Re: [PATCH] dmaengine: sh: rz-dmac: Avoid format-overflow warning

Hi Prabhakar,

On Wed, Jan 10, 2024 at 11:27 PM Prabhakar <[email protected]> wrote:
> From: Lad Prabhakar <[email protected]>
>
> The max channel count for RZ DMAC is 16, hence use u8 instead of unsigned
> int and make the pdev_irqname string long enough to avoid the warning.

Note that the danger lies into someone changing
RZ_DMAC_MAX_CHANNELS later...

> This fixes the below issue:
> drivers/dma/sh/rz-dmac.c: In function ‘rz_dmac_probe’:
> drivers/dma/sh/rz-dmac.c:770:34: warning: ‘%u’ directive writing between 1 and 10 bytes into a region of size 3 [-Wformat-overflow=]
> 770 | sprintf(pdev_irqname, "ch%u", index);
> | ^~
> In function ‘rz_dmac_chan_probe’,
> inlined from ‘rz_dmac_probe’ at drivers/dma/sh/rz-dmac.c:910:9:
> drivers/dma/sh/rz-dmac.c:770:31: note: directive argument in the range [0, 4294967294]
> 770 | sprintf(pdev_irqname, "ch%u", index);
> | ^~~~~~
> drivers/dma/sh/rz-dmac.c:770:9: note: ‘sprintf’ output between 4 and 13 bytes into a destination of size 5
> 770 | sprintf(pdev_irqname, "ch%u", index);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> While at it use scnprintf() instead of sprintf() to make the code
> more robust.
>
> Signed-off-by: Lad Prabhakar <[email protected]>

Reviewed-by: Geert Uytterhoeven <[email protected]>

Some nits below...

> --- a/drivers/dma/sh/rz-dmac.c
> +++ b/drivers/dma/sh/rz-dmac.c
> @@ -845,9 +845,9 @@ static int rz_dmac_probe(struct platform_device *pdev)
> struct dma_device *engine;
> struct rz_dmac *dmac;
> int channel_num;
> - unsigned int i;
> int ret;
> int irq;
> + u8 i;

Personally, I'm not much a fan of making loop counters smaller than
(unsigned) int. If you do go this way, you should change channel_num
to u8, too, just like i in rz_dmac_remove().

>
> dmac = devm_kzalloc(&pdev->dev, sizeof(*dmac), GFP_KERNEL);
> if (!dmac)

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68korg

In personal conversations with technical people, I call myself a hacker. But
when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

2024-01-19 12:51:01

by Vinod Koul

[permalink] [raw]
Subject: Re: [PATCH] dmaengine: sh: rz-dmac: Avoid format-overflow warning


On Wed, 10 Jan 2024 22:27:17 +0000, Prabhakar wrote:
> The max channel count for RZ DMAC is 16, hence use u8 instead of unsigned
> int and make the pdev_irqname string long enough to avoid the warning.
>
> This fixes the below issue:
> drivers/dma/sh/rz-dmac.c: In function ‘rz_dmac_probe’:
> drivers/dma/sh/rz-dmac.c:770:34: warning: ‘%u’ directive writing between 1 and 10 bytes into a region of size 3 [-Wformat-overflow=]
> 770 | sprintf(pdev_irqname, "ch%u", index);
> | ^~
> In function ‘rz_dmac_chan_probe’,
> inlined from ‘rz_dmac_probe’ at drivers/dma/sh/rz-dmac.c:910:9:
> drivers/dma/sh/rz-dmac.c:770:31: note: directive argument in the range [0, 4294967294]
> 770 | sprintf(pdev_irqname, "ch%u", index);
> | ^~~~~~
> drivers/dma/sh/rz-dmac.c:770:9: note: ‘sprintf’ output between 4 and 13 bytes into a destination of size 5
> 770 | sprintf(pdev_irqname, "ch%u", index);
> | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> [...]

Applied, thanks!

[1/1] dmaengine: sh: rz-dmac: Avoid format-overflow warning
commit: c4d6dcb3b6250ea546a952ad33382daf7cd32425

Best regards,
--
~Vinod