Re: [PATCH] eeprom: fix memory leak on buf when failed allocation of csraddr_str

From: Serge Semin
Date: Thu Jan 26 2017 - 07:37:27 EST


On Tue, Jan 24, 2017 at 02:38:38PM +0000, Colin King <colin.king@xxxxxxxxxxxxx> wrote:
> From: Colin Ian King <colin.king@xxxxxxxxxxxxx>
>
> The error return path When csraddr_str fails to free buf, causing a
> memory leak. Fix this by returning via the free_buf label that
> performs the necessary cleanup.
>
> Signed-off-by: Colin Ian King <colin.king@xxxxxxxxxxxxx>
> ---
> drivers/misc/eeprom/idt_89hpesx.c | 6 ++++--
> 1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/misc/eeprom/idt_89hpesx.c b/drivers/misc/eeprom/idt_89hpesx.c
> index 25d47d0..56bc5c2 100644
> --- a/drivers/misc/eeprom/idt_89hpesx.c
> +++ b/drivers/misc/eeprom/idt_89hpesx.c
> @@ -965,8 +965,10 @@ static ssize_t idt_dbgfs_csr_write(struct file *filep, const char __user *ubuf,
> csraddr_len = colon_ch - buf;
> csraddr_str =
> kmalloc(sizeof(char)*(csraddr_len + 1), GFP_KERNEL);
> - if (csraddr_str == NULL)
> - return -ENOMEM;
> + if (csraddr_str == NULL) {
> + ret = -ENOMEM;
> + goto free_buf;
> + }
> /* Copy the register address to the substring buffer */
> strncpy(csraddr_str, buf, csraddr_len);
> csraddr_str[csraddr_len] = '\0';
> --
> 2.10.2
>

Indeed. Thanks for the patch.

Acked-by: Serge Semin <fancer.lancer@xxxxxxxxx>