Skip to contentSkip to conversation

pcmcia:Fix memory leaks in the function, sa11xx_drv_pcmcia_probe

PATCH Larry Finger 1 participant 1 message Mar 25, 2015 1 message not in the archive

lore
Share & tools
lore

Larry Finger

author NAK
#1 lore raw
In reply to a message not in the archive
Quoted 16 lines · Nicholas Krause

Fixes memory leaks in the function,sa11xx_drv_pcmcia_probe for when either clk_get returns a error value or when we cannot allocate memory with the pointer sinfo to memory required for this function to continue and return successfully. Further more this was caught by running coccinelle on the lastet kernel tree.

---
  drivers/pcmcia/sa11xx_base.c | 9 +++++++--
  1 file changed, 7 insertions(+), 2 deletions(-)
drivers/pcmcia/sa11xx_base.c +0 -0
index cf6de2c..a95ce73 100644

drivers/pcmcia/sa11xx_base.c int sa11xx_drv_pcmcia_probe(struct device *dev, struct pcmcia_low_level *ops, · line 229

Quoted 8 lines
drivers/pcmcia/sa11xx_base.c +3 -1
@@ -223,14 +223,19 @@ int sa11xx_drv_pcmcia_probe(struct device *dev, struct pcmcia_low_level *ops,
  	struct clk *clk;
 
  	clk = clk_get(dev, NULL);
-	if (IS_ERR(clk))
+	if (IS_ERR(clk)) {
+		clk_put(clk);
  		return PTR_ERR(clk);
+	}

If the clk_get() failed, then there is no need to use a clk_put(). In addition, clk contains an error code, not a valid pointer. This change would crash the system every time clk_get() failed.

drivers/pcmcia/sa11xx_base.c int sa11xx_drv_pcmcia_probe(struct device *dev, struct pcmcia_low_level *ops, · line 236

Quoted 9 lines
drivers/pcmcia/sa11xx_base.c +3 -1
  	sa11xx_drv_pcmcia_ops(ops);
 
  	sinfo = kzalloc(SKT_DEV_INFO_SIZE(nr), GFP_KERNEL);
-	if (!sinfo)
+	if (!sinfo) {
+		clk_put(clk);
+		kfree(sinfo);
  		return -ENOMEM;

If the memory allocation failed, there is nothing to free. That kfree() call is not needed; however, the clk_put() is needed here.

NACK.

Larry

Quoted 5 lines
drivers/pcmcia/sa11xx_base.c +1 -0
+	}
 
  	sinfo->nskt = nr;
  	sinfo->clk = clk;