Skip to contentSkip to conversation

tracing/synthetic: Replace deprecated strncpy() with strscpy()

PATCH In review Thorsten Blum 2 participants 2 messages Mar 10, 2025 – Mar 11, 2025 Tracing

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tracing/synthetic: Replace deprecated strncpy() with strscpy()

strncpy() is deprecated for NUL-terminated destination buffers; use strscpy() instead and remove the manual NUL-termination.

The destination buffer 'buf' is used only with kstrtouint() and must be NUL-terminated, but not NUL-padded. The source string 'start' is also guaranteed to be NUL-terminated and meets the __must_be_cstr() requirement of strscpy().

No functional changes intended.

---
 kernel/trace/trace_events_synth.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
kernel/trace/trace_events_synth.c +1 -2
index e3f7d09e5512..4b51f74897b4 100644
@@ -207,8 +207,7 @@ static int synth_field_string_size(char *type)
 	if (len == 0)
 		return 0; /* variable-length string */
 
-	strncpy(buf, start, len);
-	buf[len] = '\0';
+	strscpy(buf, start, len + 1);
 
 	err = kstrtouint(buf, 0, &size);
 	if (err)
Signature · 1 line
2.48.1
maintainer
#1 lore raw
Quoted 5 lines · On Mon, 10 Mar 2025 21:03:58 +0100 Thorsten Blum <[email protected]>…
kernel/trace/trace_events_synth.c +0 -0
index e3f7d09e5512..4b51f74897b4 100644

kernel/trace/trace_events_synth.c static int synth_field_string_size(char *type)

Quoted 10 lines
kernel/trace/trace_events_synth.c +1 -3
@@ -207,8 +207,7 @@ static int synth_field_string_size(char *type)
 	if (len == 0)
 		return 0; /* variable-length string */
 
-	strncpy(buf, start, len);
-	buf[len] = '\0';
+	strscpy(buf, start, len + 1);
 
 	err = kstrtouint(buf, 0, &size);
 	if (err)
-

Looking at the code, it calculates the start and end by scanning the string. I think it's better to just use memcpy() instead. It's not like it's copying a random string passed in.

-- Steve