linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: minyard@acm.org
To: Raphael Assenat <raph@8d.com>
Cc: linux-leds@vger.kernel.org,
	Linux Kernel <linux-kernel@vger.kernel.org>,
	Corey Minyard <cminyard@mvista.com>
Subject: [PATCH] leds-gpio: Fix error handling and memory leak
Date: Mon,  9 Mar 2015 19:43:42 -0500	[thread overview]
Message-ID: <1425948222-7925-1-git-send-email-minyard@acm.org> (raw)

From: Corey Minyard <cminyard@mvista.com>

The leds-gpio driver would not clean up properly if it failed in some
places, and it wasn't freeing its private data.

Signed-off-by: Corey Minyard <cminyard@mvista.com>
---
 drivers/leds/leds-gpio.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/drivers/leds/leds-gpio.c b/drivers/leds/leds-gpio.c
index d26af0a..32f7642 100644
--- a/drivers/leds/leds-gpio.c
+++ b/drivers/leds/leds-gpio.c
@@ -198,8 +198,10 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 		} else {
 			if (IS_ENABLED(CONFIG_OF) && !led.name && np)
 				led.name = np->name;
-			if (!led.name)
-				return ERR_PTR(-EINVAL);
+			if (!led.name) {
+				ret = -EINVAL;
+				goto err;
+			}
 		}
 		fwnode_property_read_string(child, "linux,default-trigger",
 					    &led.default_trigger);
@@ -217,19 +219,21 @@ static struct gpio_leds_priv *gpio_leds_create(struct platform_device *pdev)
 		if (fwnode_property_present(child, "retain-state-suspended"))
 			led.retain_state_suspended = 1;
 
-		ret = create_gpio_led(&led, &priv->leds[priv->num_leds++],
+		ret = create_gpio_led(&led, &priv->leds[priv->num_leds],
 				      dev, NULL);
 		if (ret < 0) {
 			fwnode_handle_put(child);
 			goto err;
 		}
+		priv->num_leds++;
 	}
 
 	return priv;
 
 err:
-	for (count = priv->num_leds - 2; count >= 0; count--)
+	for (count = priv->num_leds - 1; count >= 0; count--)
 		delete_gpio_led(&priv->leds[count]);
+	devm_kfree(dev, priv);
 	return ERR_PTR(ret);
 }
 
@@ -283,6 +287,7 @@ static int gpio_led_remove(struct platform_device *pdev)
 
 	for (i = 0; i < priv->num_leds; i++)
 		delete_gpio_led(&priv->leds[i]);
+	devm_kfree(&pdev->dev, priv);
 
 	return 0;
 }
-- 
1.8.3.1


             reply	other threads:[~2015-03-10  0:45 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-10  0:43 minyard [this message]
2015-03-26 14:55 ` [PATCH] leds-gpio: Fix error handling and memory leak Corey Minyard
2015-03-27  1:20 ` Bryan Wu
2015-03-27  3:08   ` Corey Minyard
2015-03-30 22:16     ` Bryan Wu
2015-04-01 21:03       ` Corey Minyard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1425948222-7925-1-git-send-email-minyard@acm.org \
    --to=minyard@acm.org \
    --cc=cminyard@mvista.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-leds@vger.kernel.org \
    --cc=raph@8d.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).