Staging: android: timed_gpio: Fix resource leak in timed_gpio_probe error paths

If gpio_request fails, we need to free all allocated resources.
Current code uses wrong array index to access gpio_data array.
So current code actually frees gpio_data[i].gpio by j times.

This patch moves the error handling code to err_out and thus improves
readability.

Signed-off-by: Axel Lin <axel.lin@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
This commit is contained in:
Axel Lin 2012-03-21 09:24:57 +08:00 коммит произвёл Greg Kroah-Hartman
Родитель 099f5d01a6
Коммит 5d92f71e68
1 изменённых файлов: 15 добавлений и 12 удалений

Просмотреть файл

@ -85,7 +85,7 @@ static int timed_gpio_probe(struct platform_device *pdev)
struct timed_gpio_platform_data *pdata = pdev->dev.platform_data; struct timed_gpio_platform_data *pdata = pdev->dev.platform_data;
struct timed_gpio *cur_gpio; struct timed_gpio *cur_gpio;
struct timed_gpio_data *gpio_data, *gpio_dat; struct timed_gpio_data *gpio_data, *gpio_dat;
int i, j, ret = 0; int i, ret;
if (!pdata) if (!pdata)
return -EBUSY; return -EBUSY;
@ -108,18 +108,12 @@ static int timed_gpio_probe(struct platform_device *pdev)
gpio_dat->dev.get_time = gpio_get_time; gpio_dat->dev.get_time = gpio_get_time;
gpio_dat->dev.enable = gpio_enable; gpio_dat->dev.enable = gpio_enable;
ret = gpio_request(cur_gpio->gpio, cur_gpio->name); ret = gpio_request(cur_gpio->gpio, cur_gpio->name);
if (ret >= 0) {
ret = timed_output_dev_register(&gpio_dat->dev);
if (ret < 0) if (ret < 0)
gpio_free(cur_gpio->gpio); goto err_out;
} ret = timed_output_dev_register(&gpio_dat->dev);
if (ret < 0) { if (ret < 0) {
for (j = 0; j < i; j++) { gpio_free(cur_gpio->gpio);
timed_output_dev_unregister(&gpio_data[i].dev); goto err_out;
gpio_free(gpio_data[i].gpio);
}
kfree(gpio_data);
return ret;
} }
gpio_dat->gpio = cur_gpio->gpio; gpio_dat->gpio = cur_gpio->gpio;
@ -131,6 +125,15 @@ static int timed_gpio_probe(struct platform_device *pdev)
platform_set_drvdata(pdev, gpio_data); platform_set_drvdata(pdev, gpio_data);
return 0; return 0;
err_out:
while (--i >= 0) {
timed_output_dev_unregister(&gpio_data[i].dev);
gpio_free(gpio_data[i].gpio);
}
kfree(gpio_data);
return ret;
} }
static int timed_gpio_remove(struct platform_device *pdev) static int timed_gpio_remove(struct platform_device *pdev)