Skip to content

Commit 77a1326

Browse files
JustinStittrostedt
authored andcommitted
tracing: Replace multiple deprecated strncpy with memcpy
strncpy() is deprecated for use on NUL-terminated destination strings [1] and as such we should prefer more robust and less ambiguous string interfaces. String copy operations involving manual pointer offset and length calculations followed by explicit NUL-byte assignments are best changed to either strscpy or memcpy. strscpy is not a drop-in replacement as @len would need a one subtracted from it to avoid truncating the source string. To not sabotage readability of the current code, use memcpy (retaining the manual NUL assignment) as this unambiguously describes the desired behavior. Link: https://www.kernel.org/doc/html/latest/process/deprecated.html#strncpy-on-nul-terminated-strings [1] Link: KSPP#90 [2] Cc: Masami Hiramatsu <[email protected]> Cc: Mathieu Desnoyers <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/20241014-strncpy-kernel-trace-trace_events_filter-c-v2-1-d821e81e371e@google.com Reviewed-by: Kees Cook <[email protected]> Signed-off-by: Justin Stitt <[email protected]> Signed-off-by: Steven Rostedt (Google) <[email protected]>
1 parent 2c33155 commit 77a1326

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

kernel/trace/trace_events_filter.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ static int parse_pred(const char *str, void *data,
16161616
goto err_free;
16171617
}
16181618

1619-
strncpy(num_buf, str + s, len);
1619+
memcpy(num_buf, str + s, len);
16201620
num_buf[len] = 0;
16211621

16221622
ret = kstrtoul(num_buf, 0, &ip);
@@ -1694,7 +1694,7 @@ static int parse_pred(const char *str, void *data,
16941694
if (!pred->regex)
16951695
goto err_mem;
16961696
pred->regex->len = len;
1697-
strncpy(pred->regex->pattern, str + s, len);
1697+
memcpy(pred->regex->pattern, str + s, len);
16981698
pred->regex->pattern[len] = 0;
16991699

17001700
} else if (!strncmp(str + i, "CPUS", 4)) {
@@ -1859,7 +1859,7 @@ static int parse_pred(const char *str, void *data,
18591859
if (!pred->regex)
18601860
goto err_mem;
18611861
pred->regex->len = len;
1862-
strncpy(pred->regex->pattern, str + s, len);
1862+
memcpy(pred->regex->pattern, str + s, len);
18631863
pred->regex->pattern[len] = 0;
18641864

18651865
filter_build_regex(pred);
@@ -1919,7 +1919,7 @@ static int parse_pred(const char *str, void *data,
19191919
goto err_free;
19201920
}
19211921

1922-
strncpy(num_buf, str + s, len);
1922+
memcpy(num_buf, str + s, len);
19231923
num_buf[len] = 0;
19241924

19251925
/* Make sure it is a value */

0 commit comments

Comments
 (0)