Skip to content

Commit f82ecf7

Browse files
andy-shevbroonie
authored andcommitted
regmap: kunit: Use array_size() and sizeof(*ptr) consistently
Some of the allocations use explit sizeof(type) instead of sizeof(*ptr), which is fragile. In particular, stress_insert() allocates double of memory without obvious need for a test. Convert all allocations to use array_size() and sizeof(*ptr) to eliminate similar mistakes or wrong memory sizes. Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Link: https://lore.kernel.org/r/20240606202102.3108729-1-andriy.shevchenko@linux.intel.com Signed-off-by: Mark Brown <broonie@kernel.org>
1 parent 0ae7477 commit f82ecf7

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

drivers/base/regmap/regmap-kunit.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ static struct regmap *gen_regmap(struct kunit *test,
163163
config->max_register += (BLOCK_TEST_SIZE * config->reg_stride);
164164
}
165165

166-
size = (config->max_register + 1) * sizeof(unsigned int);
166+
size = array_size(config->max_register + 1, sizeof(*buf));
167167
buf = kmalloc(size, GFP_KERNEL);
168168
if (!buf)
169169
return ERR_PTR(-ENOMEM);
@@ -768,10 +768,9 @@ static void stress_insert(struct kunit *test)
768768
if (IS_ERR(map))
769769
return;
770770

771-
vals = kunit_kcalloc(test, sizeof(unsigned long), config.max_register,
772-
GFP_KERNEL);
771+
buf_sz = array_size(sizeof(*vals), config.max_register);
772+
vals = kunit_kmalloc(test, buf_sz, GFP_KERNEL);
773773
KUNIT_ASSERT_FALSE(test, vals == NULL);
774-
buf_sz = sizeof(unsigned long) * config.max_register;
775774

776775
get_random_bytes(vals, buf_sz);
777776

@@ -1507,15 +1506,16 @@ static struct regmap *gen_raw_regmap(struct kunit *test,
15071506
const struct regmap_test_param *param = test->param_value;
15081507
u16 *buf;
15091508
struct regmap *ret = ERR_PTR(-ENOMEM);
1510-
size_t size = (config->max_register + 1) * config->reg_bits / 8;
15111509
int i, error;
15121510
struct reg_default *defaults;
1511+
size_t size;
15131512

15141513
config->cache_type = param->cache;
15151514
config->val_format_endian = param->val_endian;
15161515
config->disable_locking = config->cache_type == REGCACHE_RBTREE ||
15171516
config->cache_type == REGCACHE_MAPLE;
15181517

1518+
size = array_size(config->max_register + 1, BITS_TO_BYTES(config->reg_bits));
15191519
buf = kmalloc(size, GFP_KERNEL);
15201520
if (!buf)
15211521
return ERR_PTR(-ENOMEM);
@@ -1615,7 +1615,7 @@ static void raw_read_defaults(struct kunit *test)
16151615
if (IS_ERR(map))
16161616
return;
16171617

1618-
val_len = sizeof(*rval) * (config.max_register + 1);
1618+
val_len = array_size(sizeof(*rval), config.max_register + 1);
16191619
rval = kunit_kmalloc(test, val_len, GFP_KERNEL);
16201620
KUNIT_ASSERT_TRUE(test, rval != NULL);
16211621
if (!rval)

0 commit comments

Comments
 (0)