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

Configure Feed

Select the types of activity you want to include in your feed.

kunit: debugfs: Handle errors from alloc_string_stream()

In kunit_debugfs_create_suite() give up and skip creating the debugfs
file if any of the alloc_string_stream() calls return an error or NULL.
Only put a value in the log pointer of kunit_suite and kunit_test if it
is a valid pointer to a log.

This prevents the potential invalid dereference reported by smatch:

lib/kunit/debugfs.c:115 kunit_debugfs_create_suite() error: 'suite->log'
dereferencing possible ERR_PTR()
lib/kunit/debugfs.c:119 kunit_debugfs_create_suite() error: 'test_case->log'
dereferencing possible ERR_PTR()

Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
Reported-by: Dan Carpenter <dan.carpenter@linaro.org>
Fixes: 05e2006ce493 ("kunit: Use string_stream for test log")
Reviewed-by: Rae Moar <rmoar@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

authored by

Richard Fitzgerald and committed by
Shuah Khan
1557e89d 34dfd5bb

+25 -5
+25 -5
lib/kunit/debugfs.c
··· 111 111 void kunit_debugfs_create_suite(struct kunit_suite *suite) 112 112 { 113 113 struct kunit_case *test_case; 114 + struct string_stream *stream; 114 115 115 - /* Allocate logs before creating debugfs representation. */ 116 - suite->log = alloc_string_stream(GFP_KERNEL); 117 - string_stream_set_append_newlines(suite->log, true); 116 + /* 117 + * Allocate logs before creating debugfs representation. 118 + * The suite->log and test_case->log pointer are expected to be NULL 119 + * if there isn't a log, so only set it if the log stream was created 120 + * successfully. 121 + */ 122 + stream = alloc_string_stream(GFP_KERNEL); 123 + if (IS_ERR_OR_NULL(stream)) 124 + return; 125 + 126 + string_stream_set_append_newlines(stream, true); 127 + suite->log = stream; 118 128 119 129 kunit_suite_for_each_test_case(suite, test_case) { 120 - test_case->log = alloc_string_stream(GFP_KERNEL); 121 - string_stream_set_append_newlines(test_case->log, true); 130 + stream = alloc_string_stream(GFP_KERNEL); 131 + if (IS_ERR_OR_NULL(stream)) 132 + goto err; 133 + 134 + string_stream_set_append_newlines(stream, true); 135 + test_case->log = stream; 122 136 } 123 137 124 138 suite->debugfs = debugfs_create_dir(suite->name, debugfs_rootdir); ··· 140 126 debugfs_create_file(KUNIT_DEBUGFS_RESULTS, S_IFREG | 0444, 141 127 suite->debugfs, 142 128 suite, &debugfs_results_fops); 129 + return; 130 + 131 + err: 132 + string_stream_destroy(suite->log); 133 + kunit_suite_for_each_test_case(suite, test_case) 134 + string_stream_destroy(test_case->log); 143 135 } 144 136 145 137 void kunit_debugfs_destroy_suite(struct kunit_suite *suite)