Skip to contentSkip to conversation

nvmem: brcm_nvram: check for allocation failure

PATCH In review Dan Carpenter 2 participants 2 messages May 20, 2022

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 nvmem: brcm_nvram: check for allocation failure

Check for if the kcalloc() fails.

Fixes 6e977eaa8280 ("nvmem: brcm_nvram: parse NVRAM content into NVMEM cells")

---
v3: Update fixes tag
v2: I don't think anything changed in v2? Added tags?

 drivers/nvmem/brcm_nvram.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/nvmem/brcm_nvram.c +2 -0
index 450b927691c3..48bb8c62cbbf 100644
@@ -97,6 +97,8 @@ static int brcm_nvram_parse(struct brcm_nvram *priv)
 	len = le32_to_cpu(header.len);
 
 	data = kcalloc(1, len, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;
 	memcpy_fromio(data, priv->base, len);
 	data[len - 1] = '\0';
 
Signature · 1 line
2.21.0
#1 lore raw
Quoted 10 lines · Dan Carpenter

Check for if the kcalloc() fails.

Fixes 6e977eaa8280 ("nvmem: brcm_nvram: parse NVRAM content into NVMEM cells")

---
v3: Update fixes tag
v2: I don't think anything changed in v2? Added tags?

[]

drivers/nvmem/brcm_nvram.c

Quoted 1 line
drivers/nvmem/brcm_nvram.c +0 -0

[]

drivers/nvmem/brcm_nvram.c static int brcm_nvram_parse(struct brcm_nvram *priv) · line 101

Quoted 6 lines
drivers/nvmem/brcm_nvram.c +2 -0
@@ -97,6 +97,8 @@ static int brcm_nvram_parse(struct brcm_nvram *priv)
 	len = le32_to_cpu(header.len);
 
 	data = kcalloc(1, len, GFP_KERNEL);
+	if (!data)
+		return -ENOMEM;

trivia:

Not sure the kcalloc(1. ..) is useful.

It might be simpler using kzalloc, though given the memcpy_fromio below a kcalloc/kzalloc seems dubious and kmalloc could be used.

Quoted 3 lines
drivers/nvmem/brcm_nvram.c +0 -0
 	memcpy_fromio(data, priv->base, len);
 	data[len - 1] = '\0';