[U-Boot] [PATCH] common: miiphyutil: avoid memory leak

Bin Meng bmeng.cn at gmail.com
Thu Nov 26 03:33:09 CET 2015


On Thu, Nov 26, 2015 at 10:26 AM, Peng Fan <Peng.Fan at freescale.com> wrote:
> The following code will alloc memory for new_dev and ldev:
> "
> new_dev = mdio_alloc();
> ldev = malloc(sizeof(*ldev));
> "
> Either new_dev or ldev is NULL, directly return, but this may leak memory.
> So before return, using free(ldev) and mdio_free(new_dev) to avoid
> leaking memory, also free can handle NULL pointer.
>
> Signed-off-by: Peng Fan <Peng.Fan at freescale.com>
> Cc: Joe Hershberger <joe.hershberger at ni.com>
> Cc: Simon Glass <sjg at chromium.org>
> Cc: Bin Meng <bmeng.cn at gmail.com>
> ---
>  common/miiphyutil.c | 2 ++
>  1 file changed, 2 insertions(+)
>
> diff --git a/common/miiphyutil.c b/common/miiphyutil.c
> index 0811e09..7e41957 100644
> --- a/common/miiphyutil.c
> +++ b/common/miiphyutil.c
> @@ -114,6 +114,8 @@ void miiphy_register(const char *name,
>         if (new_dev == NULL || ldev == NULL) {
>                 printf("miiphy_register: cannot allocate memory for '%s'\n",
>                         name);
> +               free(ldev);
> +               mdio_free(new_dev);
>                 return;
>         }
>
> --

Reviewed-by: Bin Meng <bmeng.cn at gmail.com>


More information about the U-Boot mailing list