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

leds: ns2: 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, that puts
the previous node before returning the required value. Edit the mid-loop
return sites to instead go to this new label.
Issue found with Coccinelle.

Signed-off-by: Nishka Dasgupta <nishkadg.linux@gmail.com>
Signed-off-by: Jacek Anaszewski <jacek.anaszewski@gmail.com>

authored by

Nishka Dasgupta and committed by
Jacek Anaszewski
79937a4b 730f693d

+14 -7
+14 -7
drivers/leds/leds-ns2.c
··· 245 245 struct device_node *np = dev->of_node; 246 246 struct device_node *child; 247 247 struct ns2_led *led, *leds; 248 - int num_leds = 0; 248 + int ret, num_leds = 0; 249 249 250 250 num_leds = of_get_child_count(np); 251 251 if (!num_leds) ··· 259 259 led = leds; 260 260 for_each_child_of_node(np, child) { 261 261 const char *string; 262 - int ret, i, num_modes; 262 + int i, num_modes; 263 263 struct ns2_led_modval *modval; 264 264 265 265 ret = of_get_named_gpio(child, "cmd-gpio", 0); 266 266 if (ret < 0) 267 - return ret; 267 + goto err_node_put; 268 268 led->cmd = ret; 269 269 ret = of_get_named_gpio(child, "slow-gpio", 0); 270 270 if (ret < 0) 271 - return ret; 271 + goto err_node_put; 272 272 led->slow = ret; 273 273 ret = of_property_read_string(child, "label", &string); 274 274 led->name = (ret == 0) ? string : child->name; ··· 281 281 if (ret < 0 || ret % 3) { 282 282 dev_err(dev, 283 283 "Missing or malformed modes-map property\n"); 284 - return -EINVAL; 284 + ret = -EINVAL; 285 + goto err_node_put; 285 286 } 286 287 287 288 num_modes = ret / 3; ··· 290 289 num_modes, 291 290 sizeof(struct ns2_led_modval), 292 291 GFP_KERNEL); 293 - if (!modval) 294 - return -ENOMEM; 292 + if (!modval) { 293 + ret = -ENOMEM; 294 + goto err_node_put; 295 + } 295 296 296 297 for (i = 0; i < num_modes; i++) { 297 298 of_property_read_u32_index(child, ··· 317 314 pdata->num_leds = num_leds; 318 315 319 316 return 0; 317 + 318 + err_node_put: 319 + of_node_put(child); 320 + return ret; 320 321 } 321 322 322 323 static const struct of_device_id of_ns2_leds_match[] = {