Skip to contentSkip to conversation

drm: enable -Wformat-truncation

PATCH In review Jani Nikula 4 participants 11 messages May 23, 2024 – May 24, 2024

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

cover drm: enable -Wformat-truncation

Jani Nikula (4):
drm/amdgpu: fix -Wformat-truncation warning in
amdgpu_gfx_kiq_init_ring()
drm/nouveau: fix -Wformat-truncation warning in
nouveau_backlight_init()
drm/imx: fix -Wformat-truncation warning in imx_ldb_probe()
drm: enable -Wformat-truncation across the subsystem

 drivers/gpu/drm/Makefile                    | 3 +--
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c     | 6 ++++--
 drivers/gpu/drm/imx/ipuv3/imx-ldb.c         | 6 +++++-
 drivers/gpu/drm/nouveau/nouveau_backlight.c | 9 +++++++--
 4 files changed, 17 insertions(+), 7 deletions(-)
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/4 drm/amdgpu: fix -Wformat-truncation warning in amdgpu_gfx_kiq_init_ring()

Enabling -Wformat-truncation yields the following warning:

../drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c: In function ‘amdgpu_gfx_kiq_init_ring’:
../drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c:332:61: error: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 8 [-Werror=format-truncation=]
332 | snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
| ^~
../drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c:332:50: note: directive argument in the range [0, 2147483647]
332 | snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",

      |                                                  ^~~~~~~~~~~~~~~~~
../drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c:332:9: note: ‘snprintf’ output between 12 and 41 bytes into a destination of size 16
  332 |         snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  333 |                  xcc_id, ring->me, ring->pipe, ring->queue);
      |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Silence the warning by checking the snprintf() return value.

---

Cc Alex Deucher <[email protected]>
Cc "Christian König" <[email protected]>
Cc Pan Xinhui <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +4 -2
index 1d955652f3ba..92744d0d2c10 100644
@@ -329,8 +329,10 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev, int xcc_id)
 
 	ring->eop_gpu_addr = kiq->eop_gpu_addr;
 	ring->no_scheduler = true;
-	snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
-		 xcc_id, ring->me, ring->pipe, ring->queue);
+	r = snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
+		     xcc_id, ring->me, ring->pipe, ring->queue);
+	if (r >= sizeof(ring->name))
+		dev_warn(adev->dev, "kiq ring name truncated\n");
 	r = amdgpu_ring_init(adev, ring, 1024, irq, AMDGPU_CP_KIQ_IRQ_DRIVER0,
 			     AMDGPU_RING_PRIO_DEFAULT, NULL);
 	if (r)
Signature · 1 line
2.39.2
#4 lore raw

PATCH 2/4 drm/nouveau: fix -Wformat-truncation warning in nouveau_backlight_init()

In reply to Jani Nikula

Enabling -Wformat-truncation yields the following warning:

../drivers/gpu/drm/nouveau/nouveau_backlight.c: In function ‘nouveau_backlight_init’:
../drivers/gpu/drm/nouveau/nouveau_backlight.c:56:69: error: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size 3 [-Werror=format-truncation=]
56 | snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
| ^~
In function ‘nouveau_get_backlight_name’,
inlined from ‘nouveau_backlight_init’ at ../drivers/gpu/drm/nouveau/nouveau_backlight.c:351:7:
../drivers/gpu/drm/nouveau/nouveau_backlight.c:56:56: note: directive argument in the range [1, 2147483647]
56 | snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
| ^~~~~~~~~~~~~~~~
../drivers/gpu/drm/nouveau/nouveau_backlight.c:56:17: note: ‘snprintf’ output between 14 and 23 bytes into a destination of size 15

   56 |                 snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Silence the warning by checking the snprintf() return value.

---

Cc Karol Herbst <[email protected]>
Cc Lyude Paul <[email protected]>
Cc Danilo Krummrich <[email protected]>
---
 drivers/gpu/drm/nouveau/nouveau_backlight.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/gpu/drm/nouveau/nouveau_backlight.c +7 -2
index d47442125fa1..1d77a5f280c5 100644
@@ -49,13 +49,18 @@ nouveau_get_backlight_name(char backlight_name[BL_NAME_SIZE],
 			   struct nouveau_backlight *bl)
 {
 	const int nb = ida_alloc_max(&bl_ida, 99, GFP_KERNEL);
+	int ret;
 
 	if (nb < 0)
 		return false;
 	if (nb > 0)
-		snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
+		ret = snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight%d", nb);
 	else
-		snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight");
+		ret = snprintf(backlight_name, BL_NAME_SIZE, "nv_backlight");
+
+	if (ret >= BL_NAME_SIZE)
+		return false;
+
 	bl->id = nb;
 	return true;
 }
Signature · 1 line
2.39.2
#5 lore raw

PATCH 3/4 drm/imx: fix -Wformat-truncation warning in imx_ldb_probe()

In reply to Jani Nikula

Enabling -Wformat-truncation yields the following warning:

../drivers/gpu/drm/imx/ipuv3/imx-ldb.c: In function ‘imx_ldb_probe’:
../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:57: error: ‘_sel’ directive output may be truncated writing 4 bytes into a region of size between 3 and 13 [-Werror=format-truncation=]
658 | snprintf(clkname, sizeof(clkname), "di%d_sel", i);
| ^~~~
../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:17: note: ‘snprintf’ output between 8 and 18 bytes into a destination of size 16

  658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Silence the warning by checking the snprintf() return value.

---

Cc Philipp Zabel <[email protected]>
Cc Shawn Guo <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc Pengutronix Kernel Team <[email protected]>
Cc Fabio Estevam <[email protected]>
---
 drivers/gpu/drm/imx/ipuv3/imx-ldb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/imx/ipuv3/imx-ldb.c +5 -1
index 71d70194fcbd..46f779fe60ee 100644
@@ -654,8 +654,12 @@ static int imx_ldb_probe(struct platform_device *pdev)
 	 */
 	for (i = 0; i < 4; i++) {
 		char clkname[16];
+		int len;
+
+		len = snprintf(clkname, sizeof(clkname), "di%d_sel", i);
+		if (len >= sizeof(clkname))
+			dev_err(dev, "clkname truncated\n");
 
-		snprintf(clkname, sizeof(clkname), "di%d_sel", i);
 		imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
 		if (IS_ERR(imx_ldb->clk_sel[i])) {
 			ret = PTR_ERR(imx_ldb->clk_sel[i]);
Signature · 1 line
2.39.2
#8 lore raw

PATCH 4/4 drm: enable -Wformat-truncation across the subsystem

In reply to Jani Nikula

With the -Wformat-truncation warnings fixed, finish the job started in commit a61ddb4393ad ("drm: enable (most) W=1 warnings by default across the subsystem"), and enable that warning too.

---

Gut feeling says there are more issues, and my configs just don't catch them all, but let's see what the build bots have to say. ;)

---
 drivers/gpu/drm/Makefile | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/Makefile +1 -2
index 68cc9258ffc4..644613dbedda 100644
@@ -16,8 +16,7 @@ subdir-ccflags-y += $(call cc-option, -Wunused-but-set-variable)
 subdir-ccflags-y += $(call cc-option, -Wunused-const-variable)
 subdir-ccflags-y += $(call cc-option, -Wpacked-not-aligned)
 subdir-ccflags-y += $(call cc-option, -Wformat-overflow)
-# FIXME: fix -Wformat-truncation warnings and uncomment
-#subdir-ccflags-y += $(call cc-option, -Wformat-truncation)
+subdir-ccflags-y += $(call cc-option, -Wformat-truncation)
 subdir-ccflags-y += $(call cc-option, -Wstringop-truncation)
 # The following turn off the warnings enabled by -Wextra
 ifeq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
Signature · 1 line
2.39.2
#6 lore raw

Re: [PATCH 3/4] drm/imx: fix -Wformat-truncation warning in imx_ldb_probe()

In reply to Jani Nikula
Quoted 9 lines · Jani Nikula

Enabling -Wformat-truncation yields the following warning:

../drivers/gpu/drm/imx/ipuv3/imx-ldb.c: In function ‘imx_ldb_probe’:
../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:57: error: ‘_sel’ directive output may be truncated writing 4 bytes into a region of size between 3 and 13 [-Werror=format-truncation=]
658 | snprintf(clkname, sizeof(clkname), "di%d_sel", i);
| ^~~~
../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:17: note: ‘snprintf’ output between 8 and 18 bytes into a destination of size 16

  658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If only the compiler could count to three...

Quoted 38 lines

Silence the warning by checking the snprintf() return value.

---

Cc Philipp Zabel <[email protected]>
Cc Shawn Guo <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc Pengutronix Kernel Team <[email protected]>
Cc Fabio Estevam <[email protected]>
---
 drivers/gpu/drm/imx/ipuv3/imx-ldb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/imx/ipuv3/imx-ldb.c +5 -1
index 71d70194fcbd..46f779fe60ee 100644
@@ -654,8 +654,12 @@ static int imx_ldb_probe(struct platform_device *pdev)
 	 */
 	for (i = 0; i < 4; i++) {
 		char clkname[16];
+		int len;
+
+		len = snprintf(clkname, sizeof(clkname), "di%d_sel", i);
+		if (len >= sizeof(clkname))
+			dev_err(dev, "clkname truncated\n");
 
-		snprintf(clkname, sizeof(clkname), "di%d_sel", i);
 		imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
 		if (IS_ERR(imx_ldb->clk_sel[i])) {
 			ret = PTR_ERR(imx_ldb->clk_sel[i]);
Signature · 1 line
2.39.2
Signature · 2 lines
Ville Syrjälä
Intel
#2 lore raw

Re: [PATCH 1/4] drm/amdgpu: fix -Wformat-truncation warning in amdgpu_gfx_kiq_init_ring()

In reply to Jani Nikula
Quoted 9 lines · Jani Nikula

Enabling -Wformat-truncation yields the following warning:

../drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c: In function ‘amdgpu_gfx_kiq_init_ring’:
../drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c:332:61: error: ‘%d’ directive output may be truncated writing between 1 and 10 bytes into a region of size between 0 and 8 [-Werror=format-truncation=]
332 | snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
| ^~
../drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c:332:50: note: directive argument in the range [0, 2147483647]
332 | snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",

Quoted 9 lines
      |                                                  ^~~~~~~~~~~~~~~~~
../drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c:332:9: note: ‘snprintf’ output between 12 and 41 bytes into a destination of size 16
  332 |         snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  333 |                  xcc_id, ring->me, ring->pipe, ring->queue);
      |                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Silence the warning by checking the snprintf() return value.

Already fixed with this patch:
https://patchwork.freedesktop.org/patch/594864/

Thanks,

Alex

Quoted 33 lines

---

Cc Alex Deucher <[email protected]>
Cc "Christian König" <[email protected]>
Cc Pan Xinhui <[email protected]>
---
 drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/gpu/drm/amd/amdgpu/amdgpu_gfx.c +4 -3
index 1d955652f3ba..92744d0d2c10 100644
@@ -329,8 +329,10 @@ int amdgpu_gfx_kiq_init_ring(struct amdgpu_device *adev, int xcc_id)
 
        ring->eop_gpu_addr = kiq->eop_gpu_addr;
        ring->no_scheduler = true;
-       snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
-                xcc_id, ring->me, ring->pipe, ring->queue);
+       r = snprintf(ring->name, sizeof(ring->name), "kiq_%d.%d.%d.%d",
+                    xcc_id, ring->me, ring->pipe, ring->queue);
+       if (r >= sizeof(ring->name))
+               dev_warn(adev->dev, "kiq ring name truncated\n");
        r = amdgpu_ring_init(adev, ring, 1024, irq, AMDGPU_CP_KIQ_IRQ_DRIVER0,
                             AMDGPU_RING_PRIO_DEFAULT, NULL);
        if (r)
--

2.39.2

#9 lore raw

Re: [PATCH 4/4] drm: enable -Wformat-truncation across the subsystem

In reply to Jani Nikula

Hi Jani,

Quoted 5 lines · Jani Nikula

With the -Wformat-truncation warnings fixed, finish the job started in commit a61ddb4393ad ("drm: enable (most) W=1 warnings by default across the subsystem"), and enable that warning too.

When it is enabled for all of drm then the explicit assignments here could be dropped I think:

drivers/gpu/drm/i915/Makefile:subdir-ccflags-y += $(call cc-option, -Wformat-truncation) drivers/gpu/drm/xe/Makefile:subdir-ccflags-y += $(call cc-option, -Wformat-truncation)

Just a drive-by comment, I know this patch was mostly for the bots.

Sam

Quoted 25 lines

---

Gut feeling says there are more issues, and my configs just don't catch them all, but let's see what the build bots have to say. ;)

---
 drivers/gpu/drm/Makefile | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/Makefile +1 -2
index 68cc9258ffc4..644613dbedda 100644
@@ -16,8 +16,7 @@ subdir-ccflags-y += $(call cc-option, -Wunused-but-set-variable)
 subdir-ccflags-y += $(call cc-option, -Wunused-const-variable)
 subdir-ccflags-y += $(call cc-option, -Wpacked-not-aligned)
 subdir-ccflags-y += $(call cc-option, -Wformat-overflow)
-# FIXME: fix -Wformat-truncation warnings and uncomment
-#subdir-ccflags-y += $(call cc-option, -Wformat-truncation)
+subdir-ccflags-y += $(call cc-option, -Wformat-truncation)
 subdir-ccflags-y += $(call cc-option, -Wstringop-truncation)
 # The following turn off the warnings enabled by -Wextra
 ifeq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
Signature · 1 line
2.39.2
#7 lore raw

Re: [PATCH 3/4] drm/imx: fix -Wformat-truncation warning in imx_ldb_probe()

In reply to Ville Syrjälä
Quoted 11 lines · Ville Syrjälä
Quoted 9 lines · Jani Nikula

Enabling -Wformat-truncation yields the following warning:

../drivers/gpu/drm/imx/ipuv3/imx-ldb.c: In function ‘imx_ldb_probe’:
../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:57: error: ‘_sel’ directive output may be truncated writing 4 bytes into a region of size between 3 and 13 [-Werror=format-truncation=]
658 | snprintf(clkname, sizeof(clkname), "di%d_sel", i);
| ^~~~
../drivers/gpu/drm/imx/ipuv3/imx-ldb.c:658:17: note: ‘snprintf’ output between 8 and 18 bytes into a destination of size 16

  658 |                 snprintf(clkname, sizeof(clkname), "di%d_sel", i);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Quoted 1 line

If only the compiler could count to three...

I did not try, but apparently using %hhd would hide the issue too:

	snprintf(clkname, sizeof(clkname), "di%hhd_sel", i);

BR,
Jani.

Quoted 39 lines
Quoted 39 lines

Silence the warning by checking the snprintf() return value.

---

Cc Philipp Zabel <[email protected]>
Cc Shawn Guo <[email protected]>
Cc Sascha Hauer <[email protected]>
Cc Pengutronix Kernel Team <[email protected]>
Cc Fabio Estevam <[email protected]>
---
 drivers/gpu/drm/imx/ipuv3/imx-ldb.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/gpu/drm/imx/ipuv3/imx-ldb.c +5 -1
index 71d70194fcbd..46f779fe60ee 100644
@@ -654,8 +654,12 @@ static int imx_ldb_probe(struct platform_device *pdev)
 	 */
 	for (i = 0; i < 4; i++) {
 		char clkname[16];
+		int len;
+
+		len = snprintf(clkname, sizeof(clkname), "di%d_sel", i);
+		if (len >= sizeof(clkname))
+			dev_err(dev, "clkname truncated\n");
 
-		snprintf(clkname, sizeof(clkname), "di%d_sel", i);
 		imx_ldb->clk_sel[i] = devm_clk_get(imx_ldb->dev, clkname);
 		if (IS_ERR(imx_ldb->clk_sel[i])) {
 			ret = PTR_ERR(imx_ldb->clk_sel[i]);
Signature · 1 line
2.39.2
Signature · 1 line
Jani Nikula, Intel
#10 lore raw

Re: [PATCH 4/4] drm: enable -Wformat-truncation across the subsystem

In reply to Sam Ravnborg
Quoted 9 lines · Sam Ravnborg

Hi Jani,

Quoted 5 lines · Jani Nikula

With the -Wformat-truncation warnings fixed, finish the job started in commit a61ddb4393ad ("drm: enable (most) W=1 warnings by default across the subsystem"), and enable that warning too.

Quoted 7 lines

When it is enabled for all of drm then the explicit assignments here could be dropped I think:

drivers/gpu/drm/i915/Makefile:subdir-ccflags-y += $(call cc-option, -Wformat-truncation) drivers/gpu/drm/xe/Makefile:subdir-ccflags-y += $(call cc-option, -Wformat-truncation)

Just a drive-by comment, I know this patch was mostly for the bots.

Additionally, I didn't want to create any conflicts with [1]. There's no harm in having the duplication.

BR,
Jani.

[1] https://lore.kernel.org/r/[email protected]

Quoted 28 lines

Sam

Quoted 25 lines

---

Gut feeling says there are more issues, and my configs just don't catch them all, but let's see what the build bots have to say. ;)

---
 drivers/gpu/drm/Makefile | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/gpu/drm/Makefile +1 -2
index 68cc9258ffc4..644613dbedda 100644
@@ -16,8 +16,7 @@ subdir-ccflags-y += $(call cc-option, -Wunused-but-set-variable)
 subdir-ccflags-y += $(call cc-option, -Wunused-const-variable)
 subdir-ccflags-y += $(call cc-option, -Wpacked-not-aligned)
 subdir-ccflags-y += $(call cc-option, -Wformat-overflow)
-# FIXME: fix -Wformat-truncation warnings and uncomment
-#subdir-ccflags-y += $(call cc-option, -Wformat-truncation)
+subdir-ccflags-y += $(call cc-option, -Wformat-truncation)
 subdir-ccflags-y += $(call cc-option, -Wstringop-truncation)
 # The following turn off the warnings enabled by -Wextra
 ifeq ($(findstring 2, $(KBUILD_EXTRA_WARN)),)
Signature · 1 line
2.39.2
Signature · 1 line
Jani Nikula, Intel