]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
kunit: executor: Simplify string allocation handling
authorKees Cook <kees@kernel.org>
Thu, 11 Jul 2024 16:54:32 +0000 (09:54 -0700)
committerShuah Khan <skhan@linuxfoundation.org>
Fri, 12 Jul 2024 16:11:48 +0000 (10:11 -0600)
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: https://github.com/KSPP/linux/issues/90
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Kees Cook <kees@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
lib/kunit/executor.c
lib/kunit/executor_test.c

index 70b9a43cd2571620f8f7d3d12dd72c736e3741ab..34b7b6833df3d581e500fa29c55abcc1340326f7 100644 (file)
@@ -70,32 +70,26 @@ struct kunit_glob_filter {
 static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed,
                                    const char *filter_glob)
 {
-       const int len = strlen(filter_glob);
        const char *period = strchr(filter_glob, '.');
 
        if (!period) {
-               parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL);
+               parsed->suite_glob = kstrdup(filter_glob, GFP_KERNEL);
                if (!parsed->suite_glob)
                        return -ENOMEM;
-
                parsed->test_glob = NULL;
-               strcpy(parsed->suite_glob, filter_glob);
                return 0;
        }
 
-       parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL);
+       parsed->suite_glob = kstrndup(filter_glob, period - filter_glob, GFP_KERNEL);
        if (!parsed->suite_glob)
                return -ENOMEM;
 
-       parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL);
+       parsed->test_glob = kstrdup(period + 1, GFP_KERNEL);
        if (!parsed->test_glob) {
                kfree(parsed->suite_glob);
                return -ENOMEM;
        }
 
-       strncpy(parsed->suite_glob, filter_glob, period - filter_glob);
-       strncpy(parsed->test_glob, period + 1, len - (period - filter_glob));
-
        return 0;
 }
 
index 3f7f967e3688ee0de7e0e883c858bee2c2e9f683..f0090c2729cd8afc378fa2807822b48b698f06cf 100644 (file)
@@ -286,7 +286,7 @@ static struct kunit_suite *alloc_fake_suite(struct kunit *test,
 
        /* We normally never expect to allocate suites, hence the non-const cast. */
        suite = kunit_kzalloc(test, sizeof(*suite), GFP_KERNEL);
-       strncpy((char *)suite->name, suite_name, sizeof(suite->name) - 1);
+       strscpy((char *)suite->name, suite_name, sizeof(suite->name));
        suite->test_cases = test_cases;
 
        return suite;