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.

firmware: cs_dsp: test_control_parse: null-terminate test strings

The char pointers in 'struct cs_dsp_mock_coeff_def' are expected to
point to C strings. They need to be terminated by a null byte.
However the code does not allocate that trailing null byte and only
works if by chance the allocation is followed by such a null byte.

Refactor the repeated string allocation logic into a new helper which
makes sure the terminating null is always present.
It also makes the code more readable.

Signed-off-by: Thomas Weißschuh <thomas.weissschuh@linutronix.de>
Fixes: 83baecd92e7c ("firmware: cs_dsp: Add KUnit testing of control parsing")
Cc: stable@vger.kernel.org
Reviewed-by: Richard Fitzgerald <rf@opensource.cirrus.com>
Tested-by: Richard Fitzgerald <rf@opensource.cirrus.com>
Link: https://patch.msgid.link/20250211-cs_dsp-kunit-strings-v1-1-d9bc2035d154@linutronix.de
Signed-off-by: Mark Brown <broonie@kernel.org>

authored by

Thomas Weißschuh and committed by
Mark Brown
42ae6e25 2e2f89b1

+19 -32
+19 -32
drivers/firmware/cirrus/test/cs_dsp_test_control_parse.c
··· 73 73 .length_bytes = 4, 74 74 }; 75 75 76 + static char *cs_dsp_ctl_alloc_test_string(struct kunit *test, char c, size_t len) 77 + { 78 + char *str; 79 + 80 + str = kunit_kmalloc(test, len + 1, GFP_KERNEL); 81 + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, str); 82 + memset(str, c, len); 83 + str[len] = '\0'; 84 + 85 + return str; 86 + } 87 + 76 88 /* Algorithm info block without controls should load */ 77 89 static void cs_dsp_ctl_parse_no_coeffs(struct kunit *test) 78 90 { ··· 172 160 struct cs_dsp_mock_coeff_def def = mock_coeff_template; 173 161 struct cs_dsp_coeff_ctl *ctl; 174 162 struct firmware *wmfw; 175 - char *name; 176 163 177 - name = kunit_kzalloc(test, 256, GFP_KERNEL); 178 - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, name); 179 - memset(name, 'A', 255); 180 - def.fullname = name; 164 + def.fullname = cs_dsp_ctl_alloc_test_string(test, 'A', 255); 181 165 182 166 cs_dsp_mock_wmfw_start_alg_info_block(local->wmfw_builder, 183 167 cs_dsp_ctl_parse_test_algs[0].id, ··· 260 252 struct cs_dsp_test_local *local = priv->local; 261 253 struct cs_dsp_mock_coeff_def def = mock_coeff_template; 262 254 struct cs_dsp_coeff_ctl *ctl; 263 - char *name; 264 255 struct firmware *wmfw; 265 256 266 - name = kunit_kmalloc(test, 255, GFP_KERNEL); 267 - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, name); 268 - memset(name, 'A', 255); 269 - 270 - def.shortname = name; 257 + def.shortname = cs_dsp_ctl_alloc_test_string(test, 'A', 255); 271 258 272 259 cs_dsp_mock_wmfw_start_alg_info_block(local->wmfw_builder, 273 260 cs_dsp_ctl_parse_test_algs[0].id, ··· 276 273 ctl = list_first_entry_or_null(&priv->dsp->ctl_list, struct cs_dsp_coeff_ctl, list); 277 274 KUNIT_ASSERT_NOT_NULL(test, ctl); 278 275 KUNIT_EXPECT_EQ(test, ctl->subname_len, 255); 279 - KUNIT_EXPECT_MEMEQ(test, ctl->subname, name, ctl->subname_len); 276 + KUNIT_EXPECT_MEMEQ(test, ctl->subname, def.shortname, ctl->subname_len); 280 277 KUNIT_EXPECT_EQ(test, ctl->flags, def.flags); 281 278 KUNIT_EXPECT_EQ(test, ctl->type, def.type); 282 279 KUNIT_EXPECT_EQ(test, ctl->len, def.length_bytes); ··· 326 323 struct cs_dsp_mock_coeff_def def = mock_coeff_template; 327 324 struct cs_dsp_coeff_ctl *ctl; 328 325 struct firmware *wmfw; 329 - char *fullname; 330 326 331 - fullname = kunit_kmalloc(test, 255, GFP_KERNEL); 332 - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fullname); 333 - memset(fullname, 'A', 255); 334 - def.fullname = fullname; 327 + def.fullname = cs_dsp_ctl_alloc_test_string(test, 'A', 255); 335 328 336 329 cs_dsp_mock_wmfw_start_alg_info_block(local->wmfw_builder, 337 330 cs_dsp_ctl_parse_test_algs[0].id, ··· 391 392 struct cs_dsp_mock_coeff_def def = mock_coeff_template; 392 393 struct cs_dsp_coeff_ctl *ctl; 393 394 struct firmware *wmfw; 394 - char *description; 395 395 396 - description = kunit_kmalloc(test, 65535, GFP_KERNEL); 397 - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, description); 398 - memset(description, 'A', 65535); 399 - def.description = description; 396 + def.description = cs_dsp_ctl_alloc_test_string(test, 'A', 65535); 400 397 401 398 cs_dsp_mock_wmfw_start_alg_info_block(local->wmfw_builder, 402 399 cs_dsp_ctl_parse_test_algs[0].id, ··· 424 429 struct cs_dsp_mock_coeff_def def = mock_coeff_template; 425 430 struct cs_dsp_coeff_ctl *ctl; 426 431 struct firmware *wmfw; 427 - char *fullname, *description; 428 432 429 - fullname = kunit_kmalloc(test, 255, GFP_KERNEL); 430 - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, fullname); 431 - memset(fullname, 'A', 255); 432 - def.fullname = fullname; 433 - 434 - description = kunit_kmalloc(test, 65535, GFP_KERNEL); 435 - KUNIT_ASSERT_NOT_ERR_OR_NULL(test, description); 436 - memset(description, 'A', 65535); 437 - def.description = description; 433 + def.fullname = cs_dsp_ctl_alloc_test_string(test, 'A', 255); 434 + def.description = cs_dsp_ctl_alloc_test_string(test, 'A', 65535); 438 435 439 436 cs_dsp_mock_wmfw_start_alg_info_block(local->wmfw_builder, 440 437 cs_dsp_ctl_parse_test_algs[0].id,