diff mbox

[U-Boot] common: cli_hush: avoid memory leak

Message ID 1448442981-14127-1-git-send-email-Peng.Fan@freescale.com
State Superseded
Headers show

Commit Message

Peng Fan Nov. 25, 2015, 9:16 a.m. UTC
Need to free memory avoid memory leak, when error.

Signed-off-by: Peng Fan <Peng.Fan@freescale.com>
Cc: Simon Glass <sjg@chromium.org>
Cc: Tom Rini <trini@konsulko.com>
---
 common/cli_hush.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

Comments

Simon Glass Nov. 26, 2015, 5:49 p.m. UTC | #1
Hi Peng,

On 25 November 2015 at 02:16, Peng Fan <Peng.Fan@freescale.com> wrote:
> Need to free memory avoid memory leak, when error.
>
> Signed-off-by: Peng Fan <Peng.Fan@freescale.com>
> Cc: Simon Glass <sjg@chromium.org>
> Cc: Tom Rini <trini@konsulko.com>
> ---
>  common/cli_hush.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/common/cli_hush.c b/common/cli_hush.c
> index f075459..ab85225 100644
> --- a/common/cli_hush.c
> +++ b/common/cli_hush.c
> @@ -2474,8 +2474,10 @@ static int done_word(o_string *dest, struct p_context *ctx)
>                 if (child->argv == NULL) return 1;
>                 child->argv_nonnull = realloc(child->argv_nonnull,
>                                         (argc+1)*sizeof(*child->argv_nonnull));
> -               if (child->argv_nonnull == NULL)
> +               if (child->argv_nonnull == NULL) {
> +                       free(str);
>                         return 1;
> +               }
>                 child->argv[argc-1]=str;
>                 child->argv_nonnull[argc-1] = dest->nonnull;
>                 child->argv[argc]=NULL;
> --
> 2.6.2
>
>

Reviewed-by: Simon Glass <sjg@chromium.org>

It looks like there is another memory leak at the 'return 1' immediately above.

Regards,
Simon
Peng Fan Nov. 27, 2015, 2:04 a.m. UTC | #2
Hi Simon,
On Thu, Nov 26, 2015 at 09:49:38AM -0800, Simon Glass wrote:
>Hi Peng,
>
>On 25 November 2015 at 02:16, Peng Fan <Peng.Fan@freescale.com> wrote:
>> Need to free memory avoid memory leak, when error.
>>
>> Signed-off-by: Peng Fan <Peng.Fan@freescale.com>
>> Cc: Simon Glass <sjg@chromium.org>
>> Cc: Tom Rini <trini@konsulko.com>
>> ---
>>  common/cli_hush.c | 4 +++-
>>  1 file changed, 3 insertions(+), 1 deletion(-)
>>
>> diff --git a/common/cli_hush.c b/common/cli_hush.c
>> index f075459..ab85225 100644
>> --- a/common/cli_hush.c
>> +++ b/common/cli_hush.c
>> @@ -2474,8 +2474,10 @@ static int done_word(o_string *dest, struct p_context *ctx)
>>                 if (child->argv == NULL) return 1;
>>                 child->argv_nonnull = realloc(child->argv_nonnull,
>>                                         (argc+1)*sizeof(*child->argv_nonnull));
>> -               if (child->argv_nonnull == NULL)
>> +               if (child->argv_nonnull == NULL) {
>> +                       free(str);
>>                         return 1;
>> +               }
>>                 child->argv[argc-1]=str;
>>                 child->argv_nonnull[argc-1] = dest->nonnull;
>>                 child->argv[argc]=NULL;
>> --
>> 2.6.2
>>
>>
>
>Reviewed-by: Simon Glass <sjg@chromium.org>
>
>It looks like there is another memory leak at the 'return 1' immediately above.

My coverity check tool does not report this (:-, but seems this is true memory
leak if child->argv is NULL. Thanks for pointing this out. I'll fix this in V2.

Thanks,
Peng.
>
>Regards,
>Simon
diff mbox

Patch

diff --git a/common/cli_hush.c b/common/cli_hush.c
index f075459..ab85225 100644
--- a/common/cli_hush.c
+++ b/common/cli_hush.c
@@ -2474,8 +2474,10 @@  static int done_word(o_string *dest, struct p_context *ctx)
 		if (child->argv == NULL) return 1;
 		child->argv_nonnull = realloc(child->argv_nonnull,
 					(argc+1)*sizeof(*child->argv_nonnull));
-		if (child->argv_nonnull == NULL)
+		if (child->argv_nonnull == NULL) {
+			free(str);
 			return 1;
+		}
 		child->argv[argc-1]=str;
 		child->argv_nonnull[argc-1] = dest->nonnull;
 		child->argv[argc]=NULL;