Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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: https://github.com/KSPP/linux/issues/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>

authored by

Kees Cook and committed by
Shuah Khan
7554a7b9 67c9971c

+4 -10
+3 -9
lib/kunit/executor.c
··· 70 70 static int kunit_parse_glob_filter(struct kunit_glob_filter *parsed, 71 71 const char *filter_glob) 72 72 { 73 - const int len = strlen(filter_glob); 74 73 const char *period = strchr(filter_glob, '.'); 75 74 76 75 if (!period) { 77 - parsed->suite_glob = kzalloc(len + 1, GFP_KERNEL); 76 + parsed->suite_glob = kstrdup(filter_glob, GFP_KERNEL); 78 77 if (!parsed->suite_glob) 79 78 return -ENOMEM; 80 - 81 79 parsed->test_glob = NULL; 82 - strcpy(parsed->suite_glob, filter_glob); 83 80 return 0; 84 81 } 85 82 86 - parsed->suite_glob = kzalloc(period - filter_glob + 1, GFP_KERNEL); 83 + parsed->suite_glob = kstrndup(filter_glob, period - filter_glob, GFP_KERNEL); 87 84 if (!parsed->suite_glob) 88 85 return -ENOMEM; 89 86 90 - parsed->test_glob = kzalloc(len - (period - filter_glob) + 1, GFP_KERNEL); 87 + parsed->test_glob = kstrdup(period + 1, GFP_KERNEL); 91 88 if (!parsed->test_glob) { 92 89 kfree(parsed->suite_glob); 93 90 return -ENOMEM; 94 91 } 95 - 96 - strncpy(parsed->suite_glob, filter_glob, period - filter_glob); 97 - strncpy(parsed->test_glob, period + 1, len - (period - filter_glob)); 98 92 99 93 return 0; 100 94 }
+1 -1
lib/kunit/executor_test.c
··· 286 286 287 287 /* We normally never expect to allocate suites, hence the non-const cast. */ 288 288 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)); 290 290 suite->test_cases = test_cases; 291 291 292 292 return suite;