2015-06-29 06:34:41

by Maninder Singh

[permalink] [raw]
Subject: [PATCH 1/1] ACPI/resource.c: Fixing memory leak for reg

static analysis gives memory leak error for reg:-
[drivers/acpi/resource.c:663]: (error) Memory leak: reg

Thus to prevent this allocate memory for reg after
request_range error checking.

Signed-off-by: Maninder Singh <[email protected]>
Reviewed-by: Akhilesh Kumar <[email protected]>
---
drivers/acpi/resource.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/acpi/resource.c b/drivers/acpi/resource.c
index fcb7807..f942b70 100644
--- a/drivers/acpi/resource.c
+++ b/drivers/acpi/resource.c
@@ -655,14 +655,14 @@ static int add_region_before(u64 start, u64 end, u8 space_id,
struct reserved_region *reg;
int error;

- reg = kmalloc(sizeof(*reg), GFP_KERNEL);
- if (!reg)
- return -ENOMEM;
-
error = request_range(start, end, space_id, flags, desc);
if (error)
return error;

+ reg = kmalloc(sizeof(*reg), GFP_KERNEL);
+ if (!reg)
+ return -ENOMEM;
+
reg->start = start;
reg->end = end;
list_add_tail(&reg->node, head);
--
1.7.9.5


2015-06-29 07:13:52

by Jiang Liu

[permalink] [raw]
Subject: Re: [PATCH 1/1] ACPI/resource.c: Fixing memory leak for reg

On 2015/6/29 14:34, Maninder Singh wrote:
> static analysis gives memory leak error for reg:-
> [drivers/acpi/resource.c:663]: (error) Memory leak: reg
>
> Thus to prevent this allocate memory for reg after
> request_range error checking.
Hi Maninder,
Seems it would be better to release the allocated memory
instead of reorder the code in case of failure. Otherwise the
resource region reserved by request_range() will get lost and
causes inconsistent view.
Thanks!
Gerry

>
> Signed-off-by: Maninder Singh <[email protected]>
> Reviewed-by: Akhilesh Kumar <[email protected]>
> ---
> drivers/acpi/resource.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/acpi/resource.c b/drivers/acpi/resource.c
> index fcb7807..f942b70 100644
> --- a/drivers/acpi/resource.c
> +++ b/drivers/acpi/resource.c
> @@ -655,14 +655,14 @@ static int add_region_before(u64 start, u64 end, u8 space_id,
> struct reserved_region *reg;
> int error;
>
> - reg = kmalloc(sizeof(*reg), GFP_KERNEL);
> - if (!reg)
> - return -ENOMEM;
> -
> error = request_range(start, end, space_id, flags, desc);
> if (error)
> return error;
>
> + reg = kmalloc(sizeof(*reg), GFP_KERNEL);
> + if (!reg)
> + return -ENOMEM;
> +
> reg->start = start;
> reg->end = end;
> list_add_tail(&reg->node, head);
>