* [PATCH] scripts: fix memleak error in read_file
@ 2019-10-22 11:47 linfeilong
2019-10-22 22:13 ` Andrew Morton
0 siblings, 1 reply; 3+ messages in thread
From: linfeilong @ 2019-10-22 11:47 UTC (permalink / raw)
To: akpm, linux-kernel
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;
}
--
1.8.3.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] scripts: fix memleak error in read_file
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
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Morton @ 2019-10-22 22:13 UTC (permalink / raw)
To: linfeilong; +Cc: linux-kernel
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
* 答复: [PATCH] scripts: fix memleak error in read_file
2019-10-22 22:13 ` Andrew Morton
@ 2019-10-23 2:56 ` linfeilong
0 siblings, 0 replies; 3+ messages in thread
From: linfeilong @ 2019-10-23 2:56 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-kernel
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.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-10-23 2:56 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.