[PATCH v2] utrace: fix memory leak

Rui Salvaterra rsalvaterra at gmail.com
Fri Jun 24 00:31:11 PDT 2022


Hi, Junnan,

On Fri, 17 Jun 2022 at 08:20, <junnanx.xu at gmail.com> wrote:
>
> From: Junnan Xu <junnanx.xu at gmail.com>
>
> Fixes following memory leak:
>
>   14 bytes in 1 blocks are definitely lost in loss record 1 of 5
>      at 0x4079514: malloc (vg_replace_malloc.c:309)
>      by 0x4049A04: vasprintf (vasprintf.c:13)
>      by 0x4046354: asprintf (asprintf.c:10)
>      by 0x80491A9: main (in /root/utrace)
>
>   134 bytes in 1 blocks are definitely lost in loss record 3 of 5
>      at 0x4079514: malloc (vg_replace_malloc.c:309)
>      by 0x4049A04: vasprintf (vasprintf.c:13)
>      by 0x4046354: asprintf (asprintf.c:10)
>      by 0x8049208: main (in /root/utrace)
>
> Signed-off-by: Junnan Xu <junnanx.xu at gmail.com>
> ---
>  trace/trace.c | 4 ++++
>  1 file changed, 4 insertions(+)
>
> diff --git a/trace/trace.c b/trace/trace.c
> index d895798..6fd321f 100644
> --- a/trace/trace.c
> +++ b/trace/trace.c
> @@ -381,6 +381,10 @@ int main(int argc, char **argv, char **envp)
>                 ULOG_ERR("failed to exec %s: %m\n", _argv[0]);
>
>                 free(_argv);
> +               if (_envp[0])
> +                       free(_envp[0]);
> +               if (newenv == 2 && _envp[1])
> +                       free(_envp[1]);
>                 free(_envp);
>                 return ret;
>         }
> --
> 2.21.0.windows.1

I'm not a C expert, but I see those array positions being allocated
with asprintf(), so they definitely have to be manually deallocated
before the array itself.

Reviewed-by: Rui Salvaterra <rsalvaterra at gmail.com>

I'll merge during the weekend if nobody beats me to it.

Cheers,
Rui



More information about the openwrt-devel mailing list