2022-01-11 17:35:16

by Dan Carpenter

[permalink] [raw]
Subject: [PATCH v2] counter: fix an IS_ERR() vs NULL bug

There are 8 callers for devm_counter_alloc() and they all check for NULL
instead of error pointers. I think NULL is the better thing to return
for allocation functions so update counter_alloc() and devm_counter_alloc()
to return NULL instead of error pointers.

Fixes: c18e2760308e ("counter: Provide alternative counter registration functions")
Signed-off-by: Dan Carpenter <[email protected]>
---
v2: make additional style changes

drivers/counter/counter-core.c | 15 ++++++---------
1 file changed, 6 insertions(+), 9 deletions(-)

diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
index 7e0957eea094..869894b74741 100644
--- a/drivers/counter/counter-core.c
+++ b/drivers/counter/counter-core.c
@@ -90,10 +90,8 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
int err;

ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
- if (!ch) {
- err = -ENOMEM;
- goto err_alloc_ch;
- }
+ if (!ch)
+ return NULL;

counter = &ch->counter;
dev = &counter->dev;
@@ -123,9 +121,8 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
err_ida_alloc:

kfree(ch);
-err_alloc_ch:

- return ERR_PTR(err);
+ return NULL;
}
EXPORT_SYMBOL_GPL(counter_alloc);

@@ -208,12 +205,12 @@ struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv
int err;

counter = counter_alloc(sizeof_priv);
- if (IS_ERR(counter))
- return counter;
+ if (!counter)
+ return NULL;

err = devm_add_action_or_reset(dev, devm_counter_put, counter);
if (err < 0)
- return ERR_PTR(err);
+ return NULL;

return counter;
}
--
2.20.1



2022-01-11 20:34:15

by Uwe Kleine-König

[permalink] [raw]
Subject: Re: [PATCH v2] counter: fix an IS_ERR() vs NULL bug

On Tue, Jan 11, 2022 at 08:32:43PM +0300, Dan Carpenter wrote:
> There are 8 callers for devm_counter_alloc() and they all check for NULL
> instead of error pointers. I think NULL is the better thing to return
> for allocation functions so update counter_alloc() and devm_counter_alloc()
> to return NULL instead of error pointers.
>
> Fixes: c18e2760308e ("counter: Provide alternative counter registration functions")
> Signed-off-by: Dan Carpenter <[email protected]>

Acked-by: Uwe Kleine-K?nig <[email protected]>

Thanks
Uwe

--
Pengutronix e.K. | Uwe Kleine-K?nig |
Industrial Linux Solutions | https://www.pengutronix.de/ |


Attachments:
(No filename) (708.00 B)
signature.asc (488.00 B)
Download all attachments

2022-01-11 22:49:05

by William Breathitt Gray

[permalink] [raw]
Subject: Re: [PATCH v2] counter: fix an IS_ERR() vs NULL bug

On Tue, Jan 11, 2022 at 08:32:43PM +0300, Dan Carpenter wrote:
> There are 8 callers for devm_counter_alloc() and they all check for NULL
> instead of error pointers. I think NULL is the better thing to return
> for allocation functions so update counter_alloc() and devm_counter_alloc()
> to return NULL instead of error pointers.
>
> Fixes: c18e2760308e ("counter: Provide alternative counter registration functions")
> Signed-off-by: Dan Carpenter <[email protected]>

Acked-by: William Breathitt Gray <[email protected]>

> ---
> v2: make additional style changes
>
> drivers/counter/counter-core.c | 15 ++++++---------
> 1 file changed, 6 insertions(+), 9 deletions(-)
>
> diff --git a/drivers/counter/counter-core.c b/drivers/counter/counter-core.c
> index 7e0957eea094..869894b74741 100644
> --- a/drivers/counter/counter-core.c
> +++ b/drivers/counter/counter-core.c
> @@ -90,10 +90,8 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
> int err;
>
> ch = kzalloc(sizeof(*ch) + sizeof_priv, GFP_KERNEL);
> - if (!ch) {
> - err = -ENOMEM;
> - goto err_alloc_ch;
> - }
> + if (!ch)
> + return NULL;
>
> counter = &ch->counter;
> dev = &counter->dev;
> @@ -123,9 +121,8 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
> err_ida_alloc:
>
> kfree(ch);
> -err_alloc_ch:
>
> - return ERR_PTR(err);
> + return NULL;
> }
> EXPORT_SYMBOL_GPL(counter_alloc);
>
> @@ -208,12 +205,12 @@ struct counter_device *devm_counter_alloc(struct device *dev, size_t sizeof_priv
> int err;
>
> counter = counter_alloc(sizeof_priv);
> - if (IS_ERR(counter))
> - return counter;
> + if (!counter)
> + return NULL;
>
> err = devm_add_action_or_reset(dev, devm_counter_put, counter);
> if (err < 0)
> - return ERR_PTR(err);
> + return NULL;
>
> return counter;
> }
> --
> 2.20.1
>


Attachments:
(No filename) (1.81 kB)
signature.asc (833.00 B)
Download all attachments