common: miiphyutil: avoid memory leak
authorPeng Fan <Peng.Fan@freescale.com>
Thu, 26 Nov 2015 02:26:59 +0000 (10:26 +0800)
committerTom Rini <trini@konsulko.com>
Sat, 5 Dec 2015 23:22:24 +0000 (18:22 -0500)
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@freescale.com>
Cc: Joe Hershberger <joe.hershberger@ni.com>
Cc: Simon Glass <sjg@chromium.org>
Cc: Bin Meng <bmeng.cn@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
common/miiphyutil.c

index 0811e098b47978579ce5d25fe5f82f5f010a3823..7e41957185072123a81468309081ec4586730867 100644 (file)
@@ -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;
        }