Skip to content

Commit 7554a7b

Browse files
keesshuahkh
authored andcommitted
kunit: executor: Simplify string allocation handling
The alloc/copy code pattern is better consolidated to single kstrdup (and kstrndup) calls instead. This gets rid of deprecated[1] strncpy() uses as well. Replace one other strncpy() use with the more idiomatic strscpy(). Link: KSPP#90 [1] Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Kees Cook <kees@kernel.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent 67c9971 commit 7554a7b

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

lib/kunit/executor.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,32 +70,26 @@ struct kunit_glob_filter {
7070
static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed,
7171
const char *filter_glob)
7272
{
73-
const int len = strlen(filter_glob);
7473
const char *period = strchr(filter_glob, '.');
7574

7675
if (!period) {
77-
parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL);
76+
parsed->suite_glob = kstrdup(filter_glob, GFP_KERNEL);
7877
if (!parsed->suite_glob)
7978
return -ENOMEM;
80-
8179
parsed->test_glob = NULL;
82-
strcpy(parsed->suite_glob, filter_glob);
8380
return 0;
8481
}
8582

86-
parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL);
83+
parsed->suite_glob = kstrndup(filter_glob, period - filter_glob, GFP_KERNEL);
8784
if (!parsed->suite_glob)
8885
return -ENOMEM;
8986

90-
parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL);
87+
parsed->test_glob = kstrdup(period + 1, GFP_KERNEL);
9188
if (!parsed->test_glob) {
9289
kfree(parsed->suite_glob);
9390
return -ENOMEM;
9491
}
9592

96-
strncpy(parsed->suite_glob, filter_glob, period - filter_glob);
97-
strncpy(parsed->test_glob, period + 1, len - (period - filter_glob));
98-
9993
return 0;
10094
}
10195

lib/kunit/executor_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ static struct kunit_suite *alloc_fake_suite(struct kunit *test,
286286

287287
/* We normally never expect to allocate suites, hence the non-const cast. */
288288
suite = kunit_kzalloc(test, sizeof(*suite), GFP_KERNEL);
289-
strncpy((char *)suite->name, suite_name, sizeof(suite->name) - 1);
289+
strscpy((char *)suite->name, suite_name, sizeof(suite->name));
290290
suite->test_cases = test_cases;
291291

292292
return suite;

0 commit comments

Comments
 (0)