Commit c0777d22 authored by Peter Huewe's avatar Peter Huewe Committed by Greg Kroah-Hartman

staging: altera-stapl: Fix memory leak of altera_init()

In case kzalloc() fails the second or third time
we should free the previous allocated resources.
In order to keep one return point and to keep the cleanup code to one
place, some reordering was necessary.

Also while at it, removed the *sizeof(char) - to quote Linus:
"" Also removed the silly "* sizeof(u8)".  If that isn't 1, we have way
   deeper problems than a simple multiplication can fix. """
Reported-by: default avatarAndre Bartke <andre.bartke@gmail.com>
Signed-off-by: default avatarPeter Huewe <peterhuewe@gmx.de>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 8ded351a
...@@ -2430,16 +2430,23 @@ int altera_init(struct altera_config *config, const struct firmware *fw) ...@@ -2430,16 +2430,23 @@ int altera_init(struct altera_config *config, const struct firmware *fw)
int index = 0; int index = 0;
s32 offset = 0L; s32 offset = 0L;
s32 error_address = 0L; s32 error_address = 0L;
int retval = 0;
key = kzalloc(33 * sizeof(char), GFP_KERNEL); key = kzalloc(33, GFP_KERNEL);
if (!key) if (!key) {
return -ENOMEM; retval = -ENOMEM;
value = kzalloc(257 * sizeof(char), GFP_KERNEL); goto out;
if (!value) }
return -ENOMEM; value = kzalloc(257, GFP_KERNEL);
if (!value) {
retval = -ENOMEM;
goto free_key;
}
astate = kzalloc(sizeof(struct altera_state), GFP_KERNEL); astate = kzalloc(sizeof(struct altera_state), GFP_KERNEL);
if (!astate) if (!astate) {
return -ENOMEM; retval = -ENOMEM;
goto free_value;
}
astate->config = config; astate->config = config;
if (!astate->config->jtag_io) { if (!astate->config->jtag_io) {
...@@ -2518,10 +2525,12 @@ int altera_init(struct altera_config *config, const struct firmware *fw) ...@@ -2518,10 +2525,12 @@ int altera_init(struct altera_config *config, const struct firmware *fw)
} else if (exec_result) } else if (exec_result)
printk(KERN_ERR "%s: error %d\n", __func__, exec_result); printk(KERN_ERR "%s: error %d\n", __func__, exec_result);
kfree(key);
kfree(value);
kfree(astate); kfree(astate);
free_value:
return 0; kfree(value);
free_key:
kfree(key);
out:
return retval;
} }
EXPORT_SYMBOL(altera_init); EXPORT_SYMBOL(altera_init);
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment