kunit: executor: Simplify string allocation handling
PATCH 1/1 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().
---
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: [email protected]
Cc: [email protected]
--- lib/kunit/executor.c | 12 +++--------- lib/kunit/executor_test.c | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-)
lib/kunit/executor.c +3 -9
@@ -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 ; }
lib/kunit/executor_test.c +1 -1
@@ -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); suite->test_cases = test_cases;return suite;
Signature · 1 line
2.34.1
Quoted 7 lines · Kees Cook
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 otherstrncpy()use with the more idiomaticstrscpy().Signed-off-by Kees Cook <[email protected]>
Quoted 7 lines
---
Cc: Brendan Higgins <[email protected]>
Cc: David Gow <[email protected]>
Cc: Rae Moar <[email protected]>
Cc: [email protected]
Cc: [email protected]
---
Looks good apart from the strscpy() change, which is broken by the (char *) cast. Using the 3-argument version worked here.
With the strscpy() fixed, this is:
Cheers,
-- David
Quoted 49 lines
lib/kunit/executor.c | 12 +++--------- lib/kunit/executor_test.c | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-)lib/kunit/executor.c +3
-9@@ -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 ; }lib/kunit/executor_test.c +0
-0
lib/kunit/executor_test.c static struct kunit_suite *alloc_fake_suite(struct kunit *test, · line 289
Quoted 5 lines
lib/kunit/executor_test.c +1
-1@@ -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);
This is broken: we still need to pass the length of suite->name. The (char *) cast, which is necessary to remove the 'cosnt' qualifier, stops the strscpy() macro from treating suite->name as an array.
Quoted 7 lines
lib/kunit/executor_test.c +0
-1suite->test_cases = test_cases;return suite; --2.34.1
Quoted 7 lines · David Gow
Quoted 6 lines · Kees Cook
lib/kunit/executor_test.c +0
-0@@ -286,7 +286,7 @@ static struct kunit_suite *alloc_fake_suite(struct kunit *test,
Quoted 8 lines
lib/kunit/executor_test.c static struct kunit_suite *alloc_fake_suite(struct kunit *test, · line 289
Quoted 4 lines
lib/kunit/executor_test.c +1
-1/* 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);This is broken: we still need to pass the length of
suite->name. The (char *) cast, which is necessary to remove the 'cosnt' qualifier, stops thestrscpy()macro from treatingsuite->nameas an array.
Ah! Thanks for catching that. I do build tests with "allmodconfig", and I saw the #include for executor_test.c, but didn't notice it was for _builtin_ only...
Signature · 1 line
Kees Cook