Re: [PATCH] kobject_set_name_vargs memory leak

From: Kay Sievers
Date: Sat Jun 27 2009 - 19:57:18 EST


On Sat, 2009-06-27 at 12:39 +0300, Sergey Senozhatsky wrote:

> > >> Or something along those lines, I can't remember the exact reasoning
> > >> this early in the morning.
> > >>
> > >> Kay, do you remember?

Hmm, yes, I think there was only something to work around during the
transition from the static name array, which is gone now. At least I
can't see anything we need to care about with the current code.

> Sorry, correct one.
>
> diff --git a/lib/kobject.c b/lib/kobject.c
> index b512b74..3ab224b 100644
> --- a/lib/kobject.c
> +++ b/lib/kobject.c
> @@ -215,7 +215,6 @@ static int kobject_add_internal(struct kobject *kobj)
> int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
> va_list vargs)
> {
> - const char *old_name = kobj->name;

I guess, that would leak an allocated name, when it is set several times
in a row? Something like this?

Thanks,
Kay

diff --git a/lib/kobject.c b/lib/kobject.c
index b512b74..780e89f 100644
--- a/lib/kobject.c
+++ b/lib/kobject.c
@@ -215,21 +215,22 @@ static int kobject_add_internal(struct kobject *kobj)
int kobject_set_name_vargs(struct kobject *kobj, const char *fmt,
va_list vargs)
{
- const char *old_name = kobj->name;
+ const char *name = kobj->name;
char *s;

if (kobj->name && !fmt)
return 0;

- kobj->name = kvasprintf(GFP_KERNEL, fmt, vargs);
- if (!kobj->name)
+ name = kvasprintf(GFP_KERNEL, fmt, vargs);
+ if (!name)
return -ENOMEM;

/* ewww... some of these buggers have '/' in the name ... */
- while ((s = strchr(kobj->name, '/')))
+ while ((s = strchr(name, '/')))
s[0] = '!';

- kfree(old_name);
+ kfree(kobj->name);
+ kobj->name = name;
return 0;
}



--
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/