public inbox for linux-edac@vger.kernel.org
 help / color / mirror / Atom feed
From: Ayaan Mirza Baig <ayaanmirzabaig85@gmail.com>
To: qiuxu.zhuo@intel.com
Cc: ayaanmirzabaig85@gmail.com, bp@alien8.de,
	linux-edac@vger.kernel.org, shubhrajyoti.datta@amd.com
Subject: [PATCH v3] edac: versalnet: Use kasprintf() to simplify string allocation and fix error paths.
Date: Mon, 17 Nov 2025 16:32:20 +0530	[thread overview]
Message-ID: <20251117110219.35852-2-ayaanmirzabaig85@gmail.com> (raw)
In-Reply-To: <SA1PR11MB71307704AC76D92F04FC634489C9A@SA1PR11MB7130.namprd11.prod.outlook.com>

Replace the kmalloc() + sprintf() pattern with a single call
to kasprintf(). This is cleaner, simpler, and avoids potential
buffer overflows from the fixed-size 32-byte allocation.
Handle possible NULL return from kasprintf() on allocation
failure and ensure proper cleanup on error paths.

Also free dev->init_name in the device release function
to avoid leak on normal removal.

Signed-off-by: Ayaan Mirza Baig <ayaanmirzabaig85@gmail.com>

v2:
 - Add NULL check for kasprintf() as requested by reviewer.

v3:
 - Free dev->init_name in versal_edac_release() to fix the existing leak.
---
 drivers/edac/versalnet_edac.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/edac/versalnet_edac.c b/drivers/edac/versalnet_edac.c
index 1ded4c3f0213..360d4f83ed89 100644
--- a/drivers/edac/versalnet_edac.c
+++ b/drivers/edac/versalnet_edac.c
@@ -15,6 +15,7 @@
 #include <ras/ras_event.h>
 
 #include "edac_module.h"
+#include "../../include/linux/device.h"
 
 /* Granularity of reported error in bytes */
 #define MC5_ERR_GRAIN			1
@@ -755,6 +756,7 @@ static struct rpmsg_driver amd_rpmsg_driver = {
 
 static void versal_edac_release(struct device *dev)
 {
+	kfree(dev->init_name);
 	kfree(dev);
 }
 
@@ -812,12 +814,19 @@ static int init_versalnet(struct mc_priv *priv, struct platform_device *pdev)
 
 		dev = kzalloc(sizeof(*dev), GFP_KERNEL);
 		dev->release = versal_edac_release;
-		name = kmalloc(32, GFP_KERNEL);
-		sprintf(name, "versal-net-ddrmc5-edac-%d", i);
+		name = kasprintf(GFP_KERNEL, "versal-net-ddrmc5-edac-%d", i);
+		if (!name) {
+			kfree(dev);
+			return -ENOMEM;
+		}
+
 		dev->init_name = name;
 		rc = device_register(dev);
-		if (rc)
+		if (rc) {
+			kfree(dev->init_name);
+			kfree(dev);
 			goto err_alloc;
+		}
 
 		mci->pdev = dev;
 
-- 
2.51.0


  reply	other threads:[~2025-11-17 11:03 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-15 19:56 [PATCH] edac: versalnet: Use kasprintf() to simplify string allocation Ayaan Mirza Baig
2025-11-16 12:02 ` Borislav Petkov
2025-11-16 19:58   ` [PATCH v2] " Ayaan Mirza Baig
2025-11-17  3:26     ` Zhuo, Qiuxu
2025-11-17 11:02       ` Ayaan Mirza Baig [this message]
2025-11-18 14:02         ` [PATCH v3] edac: versalnet: Use kasprintf() to simplify string allocation and fix error paths Zhuo, Qiuxu
2025-11-18 18:46           ` Ayaan Mirza Baig
2025-11-18 21:08             ` Borislav Petkov

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=20251117110219.35852-2-ayaanmirzabaig85@gmail.com \
    --to=ayaanmirzabaig85@gmail.com \
    --cc=bp@alien8.de \
    --cc=linux-edac@vger.kernel.org \
    --cc=qiuxu.zhuo@intel.com \
    --cc=shubhrajyoti.datta@amd.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