Skip to content

Commit 9ad467a

Browse files
Gax-ctiwai
authored andcommitted
ALSA: core: Fix possible NULL dereference caused by kunit_kzalloc()
kunit_kzalloc() may return a NULL pointer, dereferencing it without NULL check may lead to NULL dereference. Add NULL checks for all the kunit_kzalloc() in sound_kunit.c Fixes: 3e39acf ("ALSA: core: Add sound core KUnit test") Signed-off-by: Zichen Xie <zichenxie0106@gmail.com> Link: https://patch.msgid.link/20241126192448.12645-1-zichenxie0106@gmail.com Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent db2eee6 commit 9ad467a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

sound/core/sound_kunit.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ static void test_format_fill_silence(struct kunit *test)
172172
u32 i, j;
173173

174174
buffer = kunit_kzalloc(test, SILENCE_BUFFER_SIZE, GFP_KERNEL);
175+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, buffer);
175176

176177
for (i = 0; i < ARRAY_SIZE(buf_samples); i++) {
177178
for (j = 0; j < ARRAY_SIZE(valid_fmt); j++)
@@ -208,8 +209,12 @@ static void test_playback_avail(struct kunit *test)
208209
struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL);
209210
u32 i;
210211

212+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r);
213+
211214
r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL);
212215
r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL);
216+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->status);
217+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->control);
213218

214219
for (i = 0; i < ARRAY_SIZE(p_avail_data); i++) {
215220
r->buffer_size = p_avail_data[i].buffer_size;
@@ -232,8 +237,12 @@ static void test_capture_avail(struct kunit *test)
232237
struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL);
233238
u32 i;
234239

240+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r);
241+
235242
r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL);
236243
r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL);
244+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->status);
245+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, r->control);
237246

238247
for (i = 0; i < ARRAY_SIZE(c_avail_data); i++) {
239248
r->buffer_size = c_avail_data[i].buffer_size;
@@ -247,6 +256,7 @@ static void test_capture_avail(struct kunit *test)
247256
static void test_card_set_id(struct kunit *test)
248257
{
249258
struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL);
259+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, card);
250260

251261
snd_card_set_id(card, VALID_NAME);
252262
KUNIT_EXPECT_STREQ(test, card->id, VALID_NAME);
@@ -280,6 +290,7 @@ static void test_pcm_format_name(struct kunit *test)
280290
static void test_card_add_component(struct kunit *test)
281291
{
282292
struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL);
293+
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, card);
283294

284295
snd_component_add(card, TEST_FIRST_COMPONENT);
285296
KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT);

0 commit comments

Comments
 (0)