[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20240905052033.GA378@linuxonhyperv3.guj3yctzbm1etfxqx2vob5hsef.xx.internal.cloudapp.net>
Date: Wed, 4 Sep 2024 22:20:33 -0700
From: Saurabh Singh Sengar <ssengar@...ux.microsoft.com>
To: Dexuan Cui <decui@...rosoft.com>
Cc: Zhu Jun <zhujun2@...s.chinamobile.com>,
KY Srinivasan <kys@...rosoft.com>,
Haiyang Zhang <haiyangz@...rosoft.com>,
"wei.liu@...nel.org" <wei.liu@...nel.org>,
"linux-hyperv@...r.kernel.org" <linux-hyperv@...r.kernel.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v2] tools/hv: Add memory allocation check in
hv_fcopy_start
On Sat, Aug 31, 2024 at 12:34:43AM +0000, Dexuan Cui wrote:
> > From: Zhu Jun <zhujun2@...s.chinamobile.com>
> > Sent: Wednesday, August 28, 2024 7:45 PM
> > @@ -296,6 +296,18 @@ static int hv_fcopy_start(struct hv_start_fcopy
> > *smsg_in)
> > file_name = (char *)malloc(file_size * sizeof(char));
> > path_name = (char *)malloc(path_size * sizeof(char));
> >
> > + if (!file_name) {
> > + free(file_name);
> > + syslog(LOG_ERR, "Can't allocate file_name memory!");
> > + exit(EXIT_FAILURE);
> > + }
> > +
> > + if (!path_name) {
> > + free(path_name);
> > + syslog(LOG_ERR, "Can't allocate path_name memory!");
> > + exit(EXIT_FAILURE);
> > + }
>
> If we're calling exit() just 2 lines later, it doesn't make a lot of sense
> to call free().
>
> How about this:
>
> @@ -296,6 +296,12 @@ static int hv_fcopy_start(struct hv_start_fcopy *smsg_in)
> file_name = (char *)malloc(file_size * sizeof(char));
> path_name = (char *)malloc(path_size * sizeof(char));
>
> + if (!file_name || !path_name) {
> + free(file_name);
> + free(path_name);
> + syslog(LOG_ERR, "Can't allocate memory for file name and/or path name");
> + return HV_E_FAIL;
> + }
>
> Note: free(NULL) is valid (refer to "man 3 free").
hv_fcopy_send_data is the parent function which calls hv_fcopy_start.
Possibly a good solution is to check the return value from hv_fcopy_send_data
in fcopy_pkt_process function. Otherwise I prefer exit over returning error.
And as you rightly pointed out if we use exit, there is no sense of using free.
- Saurabh
Powered by blists - more mailing lists