mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jesper Juhl <jesper.juhl@gmail.com>
To: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>
Cc: Thomas Hellstrom <thomas@tungstengraphics.com>,
	Alan Hourihane <alanh@tungstengraphics.com>,
	Andrew Morton <akpm@linux-foundation.org>,
	Jesper Juhl <jesper.juhl@gmail.com>
Subject: [PATCH] Fix several memory leaks in cr_backlight_probe()
Date: Sat, 21 Jul 2007 00:56:01 +0200	[thread overview]
Message-ID: <200707210056.02139.jesper.juhl@gmail.com> (raw)

Hi,

After fixing the too small memory allocation in cr_backlight_probe()
from drivers/video/backlight/cr_bllcd.c 
(commit e3bbb3f05339de438faf54124f25c92e6fe4ac2e) I noticed that the 
Coverity checker also thought there were a few memory leaks in there. 
I took a closer look and confirmed that there were indeed several 
leaks.

At the start of the function we allocate storage for a 
'struct cr_panel' and store the pointer in a variable named 'crp'. 

Then we call pci_get_device() and pci_read_config_byte() and if 
either of them fail we return without freeing the memory allocated 
for the 'struct cr_panel'. These two leaks are easy to fix since we 
don't even use 'crp' for anything up to this point, so I simply 
moved the allocation further down in the function so it only happens 
just before we actually need it.

A bit further down we call backlight_device_register() and store the 
result in 'crp->cr_backlight_device'. In case of error we return 
'crp->cr_backlight_device' from the function, thus leaking 'crp' 
itself. The same thing happens with the call to lcd_device_register().
To fix these two leaks I declare two new pointers to hold the return 
values, so that in case of error we can return the pointer (as before) 
but without leaking 'crp'.

Please consider merging.

Note: Due to lack of hardware the patch has only been compile tested.


Signed-off-by: Jesper Juhl <jesper.juhl@gmail.com>
---

 drivers/video/backlight/cr_bllcd.c |   30 +++++++++++++++---------------
 1 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/drivers/video/backlight/cr_bllcd.c b/drivers/video/backlight/cr_bllcd.c
index 1b3f658..d526907 100644
--- a/drivers/video/backlight/cr_bllcd.c
+++ b/drivers/video/backlight/cr_bllcd.c
@@ -171,13 +171,11 @@ static struct lcd_ops cr_lcd_ops = {
 
 static int cr_backlight_probe(struct platform_device *pdev)
 {
+	struct backlight_device *bdp;
+	struct lcd_device *ldp;
 	struct cr_panel *crp;
 	u8 dev_en;
 
-	crp = kzalloc(sizeof(*crp), GFP_KERNEL);
-	if (crp == NULL)
-		return -ENOMEM;
-
 	lpc_dev = pci_get_device(PCI_VENDOR_ID_INTEL,
 					CRVML_DEVICE_LPC, NULL);
 	if (!lpc_dev) {
@@ -193,27 +191,29 @@ static int cr_backlight_probe(struct platform_device *pdev)
 		return -ENODEV;
 	}
 
-	crp->cr_backlight_device = backlight_device_register("cr-backlight",
-							     &pdev->dev, NULL,
-							     &cr_backlight_ops);
-	if (IS_ERR(crp->cr_backlight_device)) {
+	bdp = backlight_device_register("cr-backlight",
+					&pdev->dev, NULL,
+					&cr_backlight_ops);
+	if (IS_ERR(bdp)) {
 		pci_dev_put(lpc_dev);
-		return PTR_ERR(crp->cr_backlight_device);
+		return PTR_ERR(bdp);
 	}
 
-	crp->cr_lcd_device = lcd_device_register("cr-lcd",
-							&pdev->dev,
-							&cr_lcd_ops);
-
-	if (IS_ERR(crp->cr_lcd_device)) {
+	ldp = lcd_device_register("cr-lcd", &pdev->dev, &cr_lcd_ops);
+	if (IS_ERR(ldp)) {
 		pci_dev_put(lpc_dev);
-		return PTR_ERR(crp->cr_backlight_device);
+		return PTR_ERR(bdp);
 	}
 
 	pci_read_config_dword(lpc_dev, CRVML_REG_GPIOBAR,
 			      &gpio_bar);
 	gpio_bar &= ~0x3F;
 
+	crp = kzalloc(sizeof(*crp), GFP_KERNEL);
+	if (crp == NULL)
+		return -ENOMEM;
+	crp->cr_backlight_device = bdp;
+	crp->cr_lcd_device = ldp;
 	crp->cr_backlight_device->props.power = FB_BLANK_UNBLANK;
 	crp->cr_backlight_device->props.brightness = 0;
 	crp->cr_backlight_device->props.max_brightness = 0;



                 reply	other threads:[~2007-07-20 22:57 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=200707210056.02139.jesper.juhl@gmail.com \
    --to=jesper.juhl@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=alanh@tungstengraphics.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=thomas@tungstengraphics.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

all inboxes | Powered by JetHome®