[18/20] raw/ifpga/base: fix memory leaks in error handlings
Checks
Commit Message
1) in ifpga_enumerate.c:
The memory 'feature' is stored by TAILQ_INSERT_TAIL() at the end of the
function. When function returned early in error handling, 'feature' is
not released and leads to a memory leak.
2) in opae_eth_group.c and opae_i2c.c
These function return 'dev' when success and return NULL when validation
failed or some error occur. In the latter case 'dev' is not released and
leads to a memory leak.
Fixes: 56bb54ea1bdf ("raw/ifpga/base: add Intel FPGA OPAE share code")
Fixes: 12f92a513a13 ("raw/ifpga/base: fix retimer link status")
Fixes: 15d21c851028 ("raw/ifpga/base: add I2C and at24 EEPROM driver")
Signed-off-by: Weiguo Li <liwg06@foxmail.com>
---
drivers/raw/ifpga/base/ifpga_enumerate.c | 10 ++++++++--
drivers/raw/ifpga/base/opae_eth_group.c | 1 +
drivers/raw/ifpga/base/opae_i2c.c | 5 ++++-
3 files changed, 13 insertions(+), 3 deletions(-)
@@ -100,12 +100,16 @@ build_info_add_sub_feature(struct build_feature_devs_info *binfo,
(unsigned long long)feature->phys_addr, size);
if (vec_cnt) {
- if (vec_start + vec_cnt <= vec_start)
+ if (vec_start + vec_cnt <= vec_start) {
+ opae_free(feature);
return -EINVAL;
+ }
ctx = zmalloc(sizeof(*ctx) * vec_cnt);
- if (!ctx)
+ if (!ctx) {
+ opae_free(feature);
return -ENOMEM;
+ }
for (i = 0; i < vec_cnt; i++) {
ctx[i].eventfd = -1;
@@ -130,6 +134,8 @@ build_info_add_sub_feature(struct build_feature_devs_info *binfo,
TAILQ_INSERT_TAIL(&hw->port[port_id].feature_list,
feature, next);
} else {
+ opae_free(feature->ctx);
+ opae_free(feature);
return -EFAULT;
}
return ret;
@@ -297,6 +297,7 @@ struct eth_group_device *eth_group_probe(void *base)
if (eth_group_hw_init(dev)) {
dev_err(dev, "eth group hw init fail\n");
+ opae_free(dev);
return NULL;
}
@@ -479,6 +479,7 @@ struct altera_i2c_dev *altera_i2c_probe(void *base)
if (dev->i2c_param.devid != 0xEE011) {
dev_err(dev, "find a invalid i2c master\n");
+ opae_free(dev);
return NULL;
}
@@ -494,8 +495,10 @@ struct altera_i2c_dev *altera_i2c_probe(void *base)
dev->i2c_clk = dev->i2c_param.ref_clk * 1000000;
dev->xfer = altera_i2c_xfer;
- if (pthread_mutex_init(&dev->lock, NULL))
+ if (pthread_mutex_init(&dev->lock, NULL)) {
+ opae_free(dev);
return NULL;
+ }
dev->mutex = &dev->lock;
altera_i2c_hardware_init(dev);