[PATCH v3] auxdisplay: hd44780: Fix a potential memory leak in hd44780.c

Haoxiang Li posted 1 patch 11 months, 2 weeks ago
drivers/auxdisplay/hd44780.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH v3] auxdisplay: hd44780: Fix a potential memory leak in hd44780.c
Posted by Haoxiang Li 11 months, 2 weeks ago
At the 'fail2' label in hd44780_probe(), the 'lcd' variable is
freed via kfree(), but this does not actually release the memory
allocated by charlcd_alloc(), as that memory is a container for lcd.
As a result, a memory leak occurs. Replace kfree() with charlcd_free()
to fix a potential memory leak.
Same replacement is done in hd44780_remove().

Fixes: 718e05ed92ec ("auxdisplay: Introduce hd44780_common.[ch]")
Cc: stable@vger.kernel.org
Signed-off-by: Haoxiang Li <haoxiang_li2024@163.com>
---
Changes in v3:
- modify the patch description.
Thanks for the review! I think Fixes-tag should be added because
the previous version causes a memory leak. I modified the patch
description to illustrate it. Thanks again!
Changes in v2:
- Merge the two patches into one.
- Modify the patch description.
Sorry Geert, I didn't see your reply until after I sent the
second patch. I've merged the two patches into one, hoping
to make your work a bit easier! Thanks a lot!
---
 drivers/auxdisplay/hd44780.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/auxdisplay/hd44780.c b/drivers/auxdisplay/hd44780.c
index 0526f0d90a79..9d0ae9c02e9b 100644
--- a/drivers/auxdisplay/hd44780.c
+++ b/drivers/auxdisplay/hd44780.c
@@ -313,7 +313,7 @@ static int hd44780_probe(struct platform_device *pdev)
 fail3:
 	kfree(hd);
 fail2:
-	kfree(lcd);
+	charlcd_free(lcd);
 fail1:
 	kfree(hdc);
 	return ret;
@@ -328,7 +328,7 @@ static void hd44780_remove(struct platform_device *pdev)
 	kfree(hdc->hd44780);
 	kfree(lcd->drvdata);
 
-	kfree(lcd);
+	charlcd_free(lcd);
 }
 
 static const struct of_device_id hd44780_of_match[] = {
-- 
2.25.1
Re: [PATCH v3] auxdisplay: hd44780: Fix a potential memory leak in hd44780.c
Posted by Andy Shevchenko 11 months, 2 weeks ago
On Wed, Feb 26, 2025 at 06:12:13PM +0800, Haoxiang Li wrote:
> At the 'fail2' label in hd44780_probe(), the 'lcd' variable is
> freed via kfree(), but this does not actually release the memory
> allocated by charlcd_alloc(), as that memory is a container for lcd.
> As a result, a memory leak occurs. Replace kfree() with charlcd_free()
> to fix a potential memory leak.
> Same replacement is done in hd44780_remove().

The v2 was already applied.

-- 
With Best Regards,
Andy Shevchenko