Skip to contentSkip to conversation

staging: vc04_services: changen strncpy() to strscpy_pad()

PATCH In review Arnd Bergmann 2 participants 2 messages Mar 13, 2024

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

PATCH 1/1 staging: vc04_services: changen strncpy() to strscpy_pad()

From: Arnd Bergmann <[email protected]>

gcc-14 warns about this strncpy() that results in a non-terminated string for an overflow:

In file included from include/linux/string.h:369,
from drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c:20:
In function 'strncpy',
inlined from 'create_component' at drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c:940:2:
include/linux/fortify-string.h:108:33: error: '__builtin_strncpy' specified bound 128 equals destination size [-Werror=stringop-truncation]

Change it to strscpy_pad(), which produces a properly terminated and zero-padded string.

---
 drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c +2 -2
index 258aa0e37f55..6ca5797aeae5 100644
@@ -937,8 +937,8 @@ static int create_component(struct vchiq_mmal_instance *instance,
 	/* build component create message */
 	m.h.type = MMAL_MSG_TYPE_COMPONENT_CREATE;
 	m.u.component_create.client_component = component->client_component;
-	strncpy(m.u.component_create.name, name,
-		sizeof(m.u.component_create.name));
+	strscpy_pad(m.u.component_create.name, name,
+		    sizeof(m.u.component_create.name));
 
 	ret = send_synchronous_mmal_msg(instance, &m,
 					sizeof(m.u.component_create),
Signature · 1 line
2.39.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 11 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

gcc-14 warns about this strncpy() that results in a non-terminated string for an overflow:

In file included from include/linux/string.h:369,
from drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c:20:
In function 'strncpy',
inlined from 'create_component' at drivers/staging/vc04_services/vchiq-mmal/mmal-vchiq.c:940:2:
include/linux/fortify-string.h:108:33: error: '__builtin_strncpy' specified bound 128 equals destination size [-Werror=stringop-truncation]

Quoted 5 lines

Change it to strscpy_pad(), which produces a properly terminated and zero-padded string.

---

Thanks, Arnd!

(Greg is on vacation so I'm playing Greg for two weeks).

In real life, 128 is way larger than necessary so it's not an issue, which is presumably why you left off the Fixes tag. Anyway, it's always good to replace another strncpy().

I see another bug here where m.u.component_create.pid is never initialized. I'll send a "m.u.component_create.pid = 0;" fix for that based on top of your patch.

regards,
dan carpenter