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

iio: dummy: iio_simple_dummy: check the return value of kstrdup()

kstrdup() is also a memory allocation-related function, it returns NULL
when some memory errors happen. So it is better to check the return
value of it so to catch the memory error in time. Besides, there should
have a kfree() to clear up the allocation if we get a failure later in
this function to prevent memory leak.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
Link: https://lore.kernel.org/r/tencent_C920CFCC33B9CC1C63141FE1334A39FF8508@qq.com
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>

authored by

Xiaoke Wang and committed by
Jonathan Cameron
ba936421 879a5237

+12 -8
+12 -8
drivers/iio/dummy/iio_simple_dummy.c
··· 575 575 */ 576 576 577 577 swd = kzalloc(sizeof(*swd), GFP_KERNEL); 578 - if (!swd) { 579 - ret = -ENOMEM; 580 - goto error_kzalloc; 581 - } 578 + if (!swd) 579 + return ERR_PTR(-ENOMEM); 580 + 582 581 /* 583 582 * Allocate an IIO device. 584 583 * ··· 589 590 indio_dev = iio_device_alloc(parent, sizeof(*st)); 590 591 if (!indio_dev) { 591 592 ret = -ENOMEM; 592 - goto error_ret; 593 + goto error_free_swd; 593 594 } 594 595 595 596 st = iio_priv(indio_dev); ··· 615 616 * indio_dev->name = spi_get_device_id(spi)->name; 616 617 */ 617 618 indio_dev->name = kstrdup(name, GFP_KERNEL); 619 + if (!indio_dev->name) { 620 + ret = -ENOMEM; 621 + goto error_free_device; 622 + } 618 623 619 624 /* Provide description of available channels */ 620 625 indio_dev->channels = iio_dummy_channels; ··· 635 632 636 633 ret = iio_simple_dummy_events_register(indio_dev); 637 634 if (ret < 0) 638 - goto error_free_device; 635 + goto error_free_name; 639 636 640 637 ret = iio_simple_dummy_configure_buffer(indio_dev); 641 638 if (ret < 0) ··· 652 649 iio_simple_dummy_unconfigure_buffer(indio_dev); 653 650 error_unregister_events: 654 651 iio_simple_dummy_events_unregister(indio_dev); 652 + error_free_name: 653 + kfree(indio_dev->name); 655 654 error_free_device: 656 655 iio_device_free(indio_dev); 657 - error_ret: 656 + error_free_swd: 658 657 kfree(swd); 659 - error_kzalloc: 660 658 return ERR_PTR(ret); 661 659 } 662 660