* [PATCH] iio: dummy: iio_simple_dummy: check the return value of kstrdup()
@ 2021-12-13 9:18 Xiaoke Wang
2021-12-16 16:30 ` Jonathan Cameron
0 siblings, 1 reply; 2+ messages in thread
From: Xiaoke Wang @ 2021-12-13 9:18 UTC (permalink / raw)
To: jic23; +Cc: lars, linux-iio, linux-kernel, Xiaoke Wang
kstrdup() is also a memory allocation-related function, it return 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.
Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
drivers/iio/dummy/iio_simple_dummy.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/drivers/iio/dummy/iio_simple_dummy.c b/drivers/iio/dummy/iio_simple_dummy.c
index c0b7ef9..a5e8071 100644
--- a/drivers/iio/dummy/iio_simple_dummy.c
+++ b/drivers/iio/dummy/iio_simple_dummy.c
@@ -616,6 +616,10 @@ static struct iio_sw_device *iio_dummy_probe(const char *name)
* indio_dev->name = spi_get_device_id(spi)->name;
*/
indio_dev->name = kstrdup(name, GFP_KERNEL);
+ if (!indio_dev->name) {
+ ret = -ENOMEM;
+ goto error_free_device;
+ }
/* Provide description of available channels */
indio_dev->channels = iio_dummy_channels;
--
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] iio: dummy: iio_simple_dummy: check the return value of kstrdup()
2021-12-13 9:18 [PATCH] iio: dummy: iio_simple_dummy: check the return value of kstrdup() Xiaoke Wang
@ 2021-12-16 16:30 ` Jonathan Cameron
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2021-12-16 16:30 UTC (permalink / raw)
To: Xiaoke Wang; +Cc: lars, linux-iio, linux-kernel
On Mon, 13 Dec 2021 17:18:21 +0800
Xiaoke Wang <xkernel.wang@foxmail.com> wrote:
> kstrdup() is also a memory allocation-related function, it return 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.
>
> Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
Hi.
Good spot, but there is more to do to fix this. The error path doesn't
clear up the allocation if we get a failure later in this function.
So should have a kfree() error case.
Also, a side note is the error labels in that function are terrible!
(given I probably wrote them I can be rude about them:)
So cleaning that up as well might be a good thing to do whilst here.
Make them reflect what they are cleaning up rather that the crazy
naming there currently. When error_ret label goes to a place where
it does more than just return it's not good.
Also drop the first error_kzalloc label and goto in favour of direct return.
Great if you can tidy this up, if not I'll do it at somepoint.
Jonathan
> ---
> drivers/iio/dummy/iio_simple_dummy.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/drivers/iio/dummy/iio_simple_dummy.c b/drivers/iio/dummy/iio_simple_dummy.c
> index c0b7ef9..a5e8071 100644
> --- a/drivers/iio/dummy/iio_simple_dummy.c
> +++ b/drivers/iio/dummy/iio_simple_dummy.c
> @@ -616,6 +616,10 @@ static struct iio_sw_device *iio_dummy_probe(const char *name)
> * indio_dev->name = spi_get_device_id(spi)->name;
> */
> indio_dev->name = kstrdup(name, GFP_KERNEL);
> + if (!indio_dev->name) {
> + ret = -ENOMEM;
> + goto error_free_device;
> + }
>
> /* Provide description of available channels */
> indio_dev->channels = iio_dummy_channels;
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-12-16 16:25 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-13 9:18 [PATCH] iio: dummy: iio_simple_dummy: check the return value of kstrdup() Xiaoke Wang
2021-12-16 16:30 ` Jonathan Cameron
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).