Re: [PATCH 1/3] uml: drivers/net_user.c memory leak fix

From: Richard Weinberger
Date: Fri Jul 08 2011 - 06:47:25 EST


Am Freitag 08 Juli 2011, 12:30:56 schrieb Vitaliy Ivanov:
> On Thu, Jul 7, 2011 at 8:27 PM, Richard Weinberger <richard@xxxxxx> wrote:
> > Am Donnerstag 07 Juli 2011, 18:36:02 schrieb Vitaliy Ivanov:
> >> >From 9b9f36f46aa708c3245f5ded83f96421966b2edf Mon Sep 17 00:00:00 2001
> >>
> >> From: Vitaliy Ivanov <vitalivanov@xxxxxxxxx>
> >> Date: Thu, 7 Jul 2011 19:23:13 +0300
> >> Subject: [PATCH 1/3] uml: drivers/net_user.c memory leak fix
> >>
> >> Perform memory cleanup on exit.
> >> On receiving invalid 'pid' we still should clean 'output' variable.
> >>
> >> Signed-off-by: Vitaliy Ivanov <vitalivanov@xxxxxxxxx>
> >> ---
> >> arch/um/drivers/net_user.c | 5 +++--
> >> 1 files changed, 3 insertions(+), 2 deletions(-)
> >>
> >> diff --git a/arch/um/drivers/net_user.c b/arch/um/drivers/net_user.c
> >> index 9415dd9..989b653 100644
> >> --- a/arch/um/drivers/net_user.c
> >> +++ b/arch/um/drivers/net_user.c
> >> @@ -228,10 +228,11 @@ static void change(char *dev, char *what, unsigned
> >> char *addr, "buffer\n");
> >>
> >> pid = change_tramp(argv, output, output_len);
> >> - if (pid < 0) return;
> >>
> >> if (output != NULL) {
> >> - printk("%s", output);
> >> + if (pid >= 0) {
> >> + printk("%s", output);
> >> + }
> >> kfree(output);
> >> }
> >> }
> >
> > This control logic is a bit strange.
> > When change_tramp() fails we should not printk() the output variable.
> >
> > if (pid < 0){
> > free(output);
> > return;
> > }
> >
> > Would be much cleaner.
>
> I just didn't want to clone this free-return stuff. So, what you
> proposing is like this:
> ------------
> ...
> output = uml_kmalloc(output_len, UM_GFP_KERNEL);
> if (output == NULL)
> printk(UM_KERN_ERR "change : failed to allocate output "
> "buffer\n");
>
> pid = change_tramp(argv, output, output_len);
> if (pid < 0) {
> free(output); <---------- I'm not sure
> but 'output' can be NULL here.
> return;
> }
>
> if (output != NULL) {
> printk("%s", output);
> kfree(output);
> }
> }
> ------------
>
> I was trying to print 'output' only in case change_tramp is
> successful. That's the old logic. And at the same time perform free
> only in case output is not NULL.

Why?
Freeing a NULL pointer is perfectly fine.

Thanks,
//richard
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/