Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

leds: max77650: Add of_node_put() before return

Each iteration of for_each_child_of_node puts the previous node, but in
the case of a return from the middle of the loop, there is no put, thus
causing a memory leak.
Hence create a new label, err_node_put, which puts the previous node and
returns variable rv. Modify the mid-loop return statements to instead
store the return value in rv and jump to err_node_put.
Issue found with Coccinelle.

Signed-off-by: Nishka Dasgupta <nishkadg.linux@gmail.com>
Reviewed-by: Dan Murphy <dmurphy@ti.com>
Acked-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
Signed-off-by: Jacek Anaszewski <jacek.anaszewski@gmail.com>

authored by

Nishka Dasgupta and committed by
Jacek Anaszewski
730f693d 533016c8

+14 -7
+14 -7
drivers/leds/leds-max77650.c
··· 91 91 92 92 for_each_child_of_node(of_node, child) { 93 93 rv = of_property_read_u32(child, "reg", &reg); 94 - if (rv || reg >= MAX77650_LED_NUM_LEDS) 95 - return -EINVAL; 94 + if (rv || reg >= MAX77650_LED_NUM_LEDS) { 95 + rv = -EINVAL; 96 + goto err_node_put; 97 + } 96 98 97 99 led = &leds[reg]; 98 100 led->map = map; ··· 109 107 } else { 110 108 led->cdev.name = devm_kasprintf(dev, GFP_KERNEL, 111 109 "max77650:%s", label); 112 - if (!led->cdev.name) 113 - return -ENOMEM; 110 + if (!led->cdev.name) { 111 + rv = -ENOMEM; 112 + goto err_node_put; 113 + } 114 114 } 115 115 116 116 of_property_read_string(child, "linux,default-trigger", ··· 120 116 121 117 rv = devm_of_led_classdev_register(dev, child, &led->cdev); 122 118 if (rv) 123 - return rv; 119 + goto err_node_put; 124 120 125 121 rv = regmap_write(map, led->regA, MAX77650_LED_A_DEFAULT); 126 122 if (rv) 127 - return rv; 123 + goto err_node_put; 128 124 129 125 rv = regmap_write(map, led->regB, MAX77650_LED_B_DEFAULT); 130 126 if (rv) 131 - return rv; 127 + goto err_node_put; 132 128 } 133 129 134 130 return regmap_write(map, 135 131 MAX77650_REG_CNFG_LED_TOP, 136 132 MAX77650_LED_TOP_DEFAULT); 133 + err_node_put: 134 + of_node_put(child); 135 + return rv; 137 136 } 138 137 139 138 static struct platform_driver max77650_led_driver = {