Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

sysfs: avoid kmem_cache_free(NULL)

kmem_cache_free() with NULL is not allowed. But it may happen
if out of memory error is triggered in sysfs_new_dirent().
This patch fixes that error handling.

Signed-off-by: Akinobu Mita <akinobu.mita@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

authored by

Akinobu Mita and committed by
Greg Kroah-Hartman
01da2425 3f8df781

+7 -6
+7 -6
fs/sysfs/dir.c
··· 361 361 struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type) 362 362 { 363 363 char *dup_name = NULL; 364 - struct sysfs_dirent *sd = NULL; 364 + struct sysfs_dirent *sd; 365 365 366 366 if (type & SYSFS_COPY_NAME) { 367 367 name = dup_name = kstrdup(name, GFP_KERNEL); 368 368 if (!name) 369 - goto err_out; 369 + return NULL; 370 370 } 371 371 372 372 sd = kmem_cache_zalloc(sysfs_dir_cachep, GFP_KERNEL); 373 373 if (!sd) 374 - goto err_out; 374 + goto err_out1; 375 375 376 376 if (sysfs_alloc_ino(&sd->s_ino)) 377 - goto err_out; 377 + goto err_out2; 378 378 379 379 atomic_set(&sd->s_count, 1); 380 380 atomic_set(&sd->s_active, 0); ··· 386 386 387 387 return sd; 388 388 389 - err_out: 390 - kfree(dup_name); 389 + err_out2: 391 390 kmem_cache_free(sysfs_dir_cachep, sd); 391 + err_out1: 392 + kfree(dup_name); 392 393 return NULL; 393 394 } 394 395