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

Jani Nikula jani.nikula at intel.com
Thu May 23 15:51:07 UTC 2024


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.

Signed-off-by: Jani Nikula <jani.nikula at intel.com>

---

Cc: Karol Herbst <kherbst at redhat.com>
Cc: Lyude Paul <lyude at redhat.com>
Cc: Danilo Krummrich <dakr at redhat.com>
Cc: dri-devel at lists.freedesktop.org
Cc: nouveau at lists.freedesktop.org
---
 drivers/gpu/drm/nouveau/nouveau_backlight.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/gpu/drm/nouveau/nouveau_backlight.c b/drivers/gpu/drm/nouveau/nouveau_backlight.c
index d47442125fa1..1d77a5f280c5 100644
--- a/drivers/gpu/drm/nouveau/nouveau_backlight.c
+++ b/drivers/gpu/drm/nouveau/nouveau_backlight.c
@@ -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;
 }
-- 
2.39.2



More information about the dri-devel mailing list