public inbox for linux-edac@vger.kernel.org
 help / color / mirror / Atom feed
From: Ayaan Mirza Baig <ayaanmirzabaig85@gmail.com>
To: "Zhuo, Qiuxu" <qiuxu.zhuo@intel.com>
Cc: "bp@alien8.de" <bp@alien8.de>,
	"linux-edac@vger.kernel.org" <linux-edac@vger.kernel.org>,
	"shubhrajyoti.datta@amd.com" <shubhrajyoti.datta@amd.com>
Subject: Re: [PATCH v3] edac: versalnet: Use kasprintf() to simplify string allocation and fix error paths.
Date: Wed, 19 Nov 2025 00:16:22 +0530	[thread overview]
Message-ID: <aRy-_vTTBL3jXbOq@ideapad> (raw)
In-Reply-To: <CY8PR11MB7134D2BE6FD3B07447A8425B89D6A@CY8PR11MB7134.namprd11.prod.outlook.com>

On Tue, Nov 18, 2025 at 02:02:01PM +0000, Zhuo, Qiuxu wrote:
> > From: Ayaan Mirza Baig <ayaanmirzabaig85@gmail.com>
> > Sent: Monday, November 17, 2025 7:02 PM
> > To: Zhuo, Qiuxu <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.
> > 
> > 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;
> 
> On this failure, I think it should "goto err_alloc;" to free the allocated mci instances 
> instead of directly returning -ENOMEM. 

Okay, thanks. I did overlook this, my bad. I apologize for these mistakes I keep
making again and again. I'll make sure there are no more errors in v4.

> 
> > +		}
> > +
> >  		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-18 18:46 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       ` [PATCH v3] edac: versalnet: Use kasprintf() to simplify string allocation and fix error paths Ayaan Mirza Baig
2025-11-18 14:02         ` Zhuo, Qiuxu
2025-11-18 18:46           ` Ayaan Mirza Baig [this message]
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=aRy-_vTTBL3jXbOq@ideapad \
    --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