Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 7ba8221

Browse files
rfvirgilbroonie
authored andcommitted
regmap: kunit: Fix array overflow in stride() test
Force the max_register of the test regmap to be one register longer than the number of test registers, to prevent an array overflow in the test loop. The test defines num_reg_defaults = 6. With 6 registers and stride == 2 the valid register addresses would be 0, 2, 4, 6, 8, 10. However the loop checks attempting to access the odd address, so on the final register it accesses address 11, and it writes entry [11] of the read/written arrays. Originally this worked because the max_register of the regmap was hardcoded to be BLOCK_TEST_SIZE (== 12). commit 7109157 ("regmap: kunit: Run sparse cache tests at non-zero register addresses") introduced the ability to start the test address range from any address, which means adjusting the max_register. If max_register was not forced, it was calculated either from num_reg_defaults or BLOCK_TEST_SIZE. This correctly calculated that with num_reg_defaults == 6 and stride == 2 the final valid address is 10. So the read/written arrays are allocated to contain entries [0..10]. When stride attempted to access [11] it was overflowing the array. Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com> Fixes: 7109157 ("regmap: kunit: Run sparse cache tests at non-zero register addresses") Tested-by: Guenter Roeck <linux@roeck-us.net> Link: https://msgid.link/r/20240517144703.1200995-1-rf@opensource.cirrus.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 9b1fe05 commit 7ba8221

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

drivers/base/regmap/regmap-kunit.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,12 +609,19 @@ static void stride(struct kunit *test)
609609
config.reg_stride = 2;
610610
config.num_reg_defaults = BLOCK_TEST_SIZE / 2;
611611

612+
/*
613+
* Allow one extra register so that the read/written arrays
614+
* are sized big enough to include an entry for the odd
615+
* address past the final reg_default register.
616+
*/
617+
config.max_register = BLOCK_TEST_SIZE;
618+
612619
map = gen_regmap(test, &config, &data);
613620
KUNIT_ASSERT_FALSE(test, IS_ERR(map));
614621
if (IS_ERR(map))
615622
return;
616623

617-
/* Only even registers can be accessed, try both read and write */
624+
/* Only even addresses can be accessed, try both read and write */
618625
for (i = 0; i < BLOCK_TEST_SIZE; i++) {
619626
data->read[i] = false;
620627
data->written[i] = false;

0 commit comments

Comments
 (0)