[PATCH bpf v1 5/7] selftests/bpf: Use strscpy_cat() in the test_loader

Ihor Solodrai posted 7 patches 1 month, 1 week ago
[PATCH bpf v1 5/7] selftests/bpf: Use strscpy_cat() in the test_loader
Posted by Ihor Solodrai 1 month, 1 week ago
Replace a pair of strcpy() calls that construct the unpriv test name
with a single strscpy_cat() call. This simplifies the code and adds
bounds checking.

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
 tools/testing/selftests/bpf/test_loader.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/tools/testing/selftests/bpf/test_loader.c b/tools/testing/selftests/bpf/test_loader.c
index 338c035c3688..af4319566fea 100644
--- a/tools/testing/selftests/bpf/test_loader.c
+++ b/tools/testing/selftests/bpf/test_loader.c
@@ -673,18 +673,18 @@ static int parse_test_spec(struct test_loader *tester,
 
 	if (spec->mode_mask & UNPRIV) {
 		int descr_len = strlen(description);
-		const char *suffix = " @unpriv";
+		static const char suffix[] = " @unpriv";
+		int name_len = descr_len + sizeof(suffix) + 1;
 		char *name;
 
-		name = malloc(descr_len + strlen(suffix) + 1);
+		name = malloc(name_len);
 		if (!name) {
 			PRINT_FAIL("failed to allocate memory for unpriv.name\n");
 			err = -ENOMEM;
 			goto cleanup;
 		}
 
-		strcpy(name, description);
-		strcpy(&name[descr_len], suffix);
+		strscpy_cat(name, name_len, description, suffix);
 		spec->unpriv.name = name;
 	}
 
-- 
2.53.0