Skip to content

Commit 9d86852

Browse files
theihorAlexei Starovoitov
authored andcommitted
selftests/bpf: Use memcpy() for bounded non-NULL-terminated copies
Replace strncpy() with memcpy() in cases where the source is non-NULL-terminated and the copy length is known. Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev> Link: https://lore.kernel.org/r/20260223190736.649171-6-ihor.solodrai@linux.dev Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 3ed0bc2 commit 9d86852

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

tools/testing/selftests/bpf/prog_tests/ctx_rewrite.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,8 +308,10 @@ static int find_field_offset(struct btf *btf, char *pattern, regmatch_t *matches
308308
return -1;
309309
}
310310

311-
strncpy(type_str, type, type_sz);
312-
strncpy(field_str, field, field_sz);
311+
memcpy(type_str, type, type_sz);
312+
type_str[type_sz] = '\0';
313+
memcpy(field_str, field, field_sz);
314+
field_str[field_sz] = '\0';
313315
btf_id = btf__find_by_name(btf, type_str);
314316
if (btf_id < 0) {
315317
PRINT_FAIL("No BTF info for type %s\n", type_str);

tools/testing/selftests/bpf/test_verifier.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1320,7 +1320,7 @@ static bool cmp_str_seq(const char *log, const char *exp)
13201320
printf("FAIL\nTestcase bug\n");
13211321
return false;
13221322
}
1323-
strncpy(needle, exp, len);
1323+
memcpy(needle, exp, len);
13241324
needle[len] = 0;
13251325
q = strstr(log, needle);
13261326
if (!q) {

0 commit comments

Comments
 (0)