Skip to content

Commit

Permalink
driver core: fix small mem leak in driver_add_kobj()
Browse files Browse the repository at this point in the history
The Coverity checker spotted that we leak the storage allocated to 'name' in
int driver_add_kobj().  The leak looks legit to me - this is the code :

int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
                    const char *fmt, ...)
{
        va_list args;
        char *name;
        int ret;

        va_start(args, fmt);
        name = kvasprintf(GFP_KERNEL, fmt, args);
        ^^^^^^^^ This dynamically allocates space...

        va_end(args);

        if (!name)
                return -ENOMEM;

        return kobject_add(kobj, &drv->p->kobj, "%s", name);
	^^^^^^^^ This neglects to free the space allocated
}

Inside kobject_add() a copy of 'name' will be made and used.  As far as I can
see, Coverity is correct in flagging this as a leak, but I'd like some
configmation before the patch is applied.

This should fix it.

Signed-off-by: Jesper Juhl <[email protected]>
Cc: Greg KH <[email protected]>
Cc: Kay Sievers <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Jesper Juhl authored and torvalds committed Mar 28, 2008
1 parent 4cdc1d1 commit d478376
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion drivers/base/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
{
va_list args;
char *name;
int ret;

va_start(args, fmt);
name = kvasprintf(GFP_KERNEL, fmt, args);
Expand All @@ -141,7 +142,9 @@ int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
if (!name)
return -ENOMEM;

return kobject_add(kobj, &drv->p->kobj, "%s", name);
ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
kfree(name);
return ret;
}
EXPORT_SYMBOL_GPL(driver_add_kobj);

Expand Down

0 comments on commit d478376

Please sign in to comment.