[PATCH 05/12] net: smc911x: Fix potential memleak() in init fail path

Marek Vasut marek.vasut at gmail.com
Sat Mar 21 17:30:46 CET 2020


On 3/17/20 7:21 AM, Masahiro Yamada wrote:
[...]
>> @@ -283,15 +283,23 @@ int smc911x_initialize(u8 dev_num, int base_addr)
>>  #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
>>         int retval;
>>         struct mii_dev *mdiodev = mdio_alloc();
>> -       if (!mdiodev)
>> +       if (!mdiodev) {
>> +               eth_unregister(dev);
>> +               free(dev);
>>                 return -ENOMEM;
>> +       }
>> +
>>         strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
>>         mdiodev->read = smc911x_miiphy_read;
>>         mdiodev->write = smc911x_miiphy_write;
>>
>>         retval = mdio_register(mdiodev);
>> -       if (retval < 0)
>> +       if (retval < 0) {
>> +               mdio_free(mdiodev);
>> +               eth_unregister(dev);
>> +               free(dev);
>>                 return retval;
> 
> 
> Using "goto <label>" is a general tip to
> simplify the failure path.

It's even better to pull the entire MII registration into a separate
function to avoid all the ifdeffery, so I'll rather do that in a
separate patch. And then it's possible to use the goto labels without it
looking ugly.

-- 
Best regards,
Marek Vasut


More information about the U-Boot mailing list