driver core: fix small mem leak in driver_add_kobj()

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 <jesper.juhl@gmail.com>
Cc: Greg KH <greg@kroah.com>
Cc: Kay Sievers <kay.sievers@vrfy.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by Jesper Juhl and committed by Linus Torvalds d478376c 4cdc1d1f

+4 -1
+4 -1
drivers/base/driver.c
··· 133 133 { 134 134 va_list args; 135 135 char *name; 136 + int ret; 136 137 137 138 va_start(args, fmt); 138 139 name = kvasprintf(GFP_KERNEL, fmt, args); ··· 142 141 if (!name) 143 142 return -ENOMEM; 144 143 145 - return kobject_add(kobj, &drv->p->kobj, "%s", name); 144 + ret = kobject_add(kobj, &drv->p->kobj, "%s", name); 145 + kfree(name); 146 + return ret; 146 147 } 147 148 EXPORT_SYMBOL_GPL(driver_add_kobj); 148 149