Skip to contentSkip to conversation

pinctrl: stm32: check devm_kasprintf() returned value

PATCH Superseded Ma Ke 2 participants 2 messages Sep 6, 2024

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ma Ke

author
#0 lore raw

PATCH v2 1/1 pinctrl: stm32: check devm_kasprintf() returned value

devm_kasprintf() can return a NULL pointer on failure but this returned value is not checked. Fix this lack and check the returned value.

Found by code review.

Fixes 32c170ff15b0 ("pinctrl: stm32: set default gpio line names using pin names")

---
Changes in v2:
- modified the patch according to suggestions, added braces;
- modified the typo.

---
 drivers/pinctrl/stm32/pinctrl-stm32.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
drivers/pinctrl/stm32/pinctrl-stm32.c +9 -2
index a8673739871d..f23b081f31b3 100644
@@ -1374,10 +1374,16 @@ static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode
 
 	for (i = 0; i < npins; i++) {
 		stm32_pin = stm32_pctrl_get_desc_pin_from_gpio(pctl, bank, i);
-		if (stm32_pin && stm32_pin->pin.name)
+		if (stm32_pin && stm32_pin->pin.name) {
 			names[i] = devm_kasprintf(dev, GFP_KERNEL, "%s", stm32_pin->pin.name);
-		else
+			if (!names[i]) {
+				err = -ENOMEM;
+				goto err_clk;
+			}
+		}
+
+		else {
 			names[i] = NULL;
+		}
 	}
 

bank->gpio_chip.names = (const char * const *)names;

Signature · 1 line
2.25.1
#1 lore raw
Quoted 8 lines · Ma Ke

devm_kasprintf() can return a NULL pointer on failure but this returned value is not checked. Fix this lack and check the returned value.

Found by code review.

Fixes 32c170ff15b0 ("pinctrl: stm32: set default gpio line names using pin names")

[...]

Quoted 9 lines
drivers/pinctrl/stm32/pinctrl-stm32.c +0 -1
index a8673739871d..f23b081f31b3 100644
@@ -1374,10 +1374,16 @@ static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode
 
 	for (i = 0; i < npins; i++) {
 		stm32_pin = stm32_pctrl_get_desc_pin_from_gpio(pctl, bank, i);
-		if (stm32_pin && stm32_pin->pin.name)

drivers/pinctrl/stm32/pinctrl-stm32.c static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode · line 1385

Quoted 10 lines
drivers/pinctrl/stm32/pinctrl-stm32.c +8 -1
+		if (stm32_pin && stm32_pin->pin.name) {
 			names[i] = devm_kasprintf(dev, GFP_KERNEL, "%s", stm32_pin->pin.name);
-		else
+			if (!names[i]) {
+				err = -ENOMEM;
+				goto err_clk;
+			}
+		}
+
+		else {

No, that should be:

		} else {

See Documentation/process/coding-style.rst, section 3.

drivers/pinctrl/stm32/pinctrl-stm32.c static int stm32_gpiolib_register_bank(struct stm32_pinctrl *pctl, struct fwnode · line 1388

Quoted 3 lines
drivers/pinctrl/stm32/pinctrl-stm32.c +1 -0
 			names[i] = NULL;
+		}
 	}

[...]

MBR, Sergey