[PATCH v2 4/7] regmap: use krealloc_array() in regmap_register_patch()
From: Peng Fan (OSS)
Date: Fri Oct 02 2026 - 09:28:34 EST
From: Peng Fan <peng.fan@xxxxxxx>
Replace krealloc() with krealloc_array() in regmap_register_patch() to
gain the built-in overflow check on the element-count * element-size
multiplication, and switch to the early-return error style to reduce
nesting.
Assisted-by: LLM
Signed-off-by: Peng Fan <peng.fan@xxxxxxx>
---
drivers/base/regmap/regmap.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)
diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c
index 0b40e1e7817c..b9830afa6d8e 100644
--- a/drivers/base/regmap/regmap.c
+++ b/drivers/base/regmap/regmap.c
@@ -3379,16 +3379,13 @@ int regmap_register_patch(struct regmap *map, const struct reg_sequence *regs,
num_regs))
return 0;
- p = krealloc(map->patch,
- sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
- GFP_KERNEL);
- if (p) {
- memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
- map->patch = p;
- map->patch_regs += num_regs;
- } else {
+ p = krealloc_array(map->patch, map->patch_regs + num_regs, sizeof(*p), GFP_KERNEL);
+ if (!p)
return -ENOMEM;
- }
+
+ memcpy(p + map->patch_regs, regs, num_regs * sizeof(*regs));
+ map->patch = p;
+ map->patch_regs += num_regs;
scoped_guard(regmap, map) {
bypass = map->cache_bypass;
--
2.51.0