linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: linfeilong <linfeilong@huawei.com>
To: Andrew Morton <akpm@linux-foundation.org>
Cc: "linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: 答复: [PATCH] scripts: fix memleak error in read_file
Date: Wed, 23 Oct 2019 02:56:04 +0000	[thread overview]
Message-ID: <9e0807659574488490391feff79870e6@huawei.com> (raw)
In-Reply-To: <20191022151305.c4af5c45ee7c605b4b12ae32@linux-foundation.org>

On Tue, 22 Oct 2019 11:47:59 +0000 linfeilong <linfeilong@huawei.com> wrote:
>
>> An error is found by the static code analysis tool: "memleak"
>> Fix this by add free before return.
>> 
>> Signed-off-by: Feilong Lin <linfeilong@huawei.com>
>> ---
>>  scripts/insert-sys-cert.c | 1 +
>>  1 file changed, 1 insertion(+)
>> 
>> diff --git a/scripts/insert-sys-cert.c b/scripts/insert-sys-cert.c 
>> index 8902836..22d99a8 100644
>> --- a/scripts/insert-sys-cert.c
>> +++ b/scripts/insert-sys-cert.c
>> @@ -250,6 +250,7 @@ static char *read_file(char *file_name, int *size)
>>  	}
>>  	if (read(fd, buf, *size) != *size) {
>>  		perror("File read failed");
>> +			free(buf);
>>  		close(fd);
>>  		return NULL;
>>  	}
>
>A few lines later we do
>
>	return buf;
>
>so the patch adds a use-after-free error. 
>
>We could do a free(cert) down in main() or we could just do nothing -
>read_file() is only called a single time.

Thanks, but there is no use-after-free as we do free just before return NULL.
And I think do free in error scenes makes the code look better.

      reply	other threads:[~2019-10-23  2:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-22 11:47 [PATCH] scripts: fix memleak error in read_file linfeilong
2019-10-22 22:13 ` Andrew Morton
2019-10-23  2:56   ` linfeilong [this message]

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=9e0807659574488490391feff79870e6@huawei.com \
    --to=linfeilong@huawei.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    /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;
as well as URLs for NNTP newsgroup(s).