Skip to contentSkip to conversation

counter: fix an IS_ERR() vs NULL bug

PATCH Superseded Dan Carpenter 3 participants 4 messages Jan 11, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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")
---
 drivers/counter/counter-core.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/counter/counter-core.c +5 -7
index 7e0957eea094..0abb28cd3d16 100644
@@ -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;
+	if (!ch)
 		goto err_alloc_ch;
-	}
 
 	counter = &ch->counter;
 	dev = &counter->dev;
@@ -125,7 +123,7 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
 	kfree(ch);
 err_alloc_ch:
 
-	return ERR_PTR(err);
+	return NULL;
 }
 EXPORT_SYMBOL_GPL(counter_alloc);
 
@@ -208,12 +206,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;
 }
Signature · 1 line
2.20.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Dan Carpenter

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")

Oh indeed. I wonder how you found that problem.

There is a possibility that counter_alloc() (as it's implemented now in next) returns an error value != -ENOMEM. (ida_alloc() can return -ENOSPC, counter_chrdev_add() can return -EINVAL). Still returning NULL on error looks sane.

Thanks
Uwe

Signature · 2 lines
Pengutronix e.K.                           | Uwe Kleine-König            |
Industrial Linux Solutions                 | https://www.pengutronix.de/ |
#2 lore raw
Quoted 10 lines · Uwe Kleine-König
Quoted 7 lines · Dan Carpenter

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")

Oh indeed. I wonder how you found that problem.

Of course from static analysis. Each caller triggers a "this NULL check should be an IS_ERR() check warning" followed by a "potential error pointer dereference" warning.

regards,
dan carpenter

#3 lore raw
In reply to Dan Carpenter
Quoted 7 lines · Dan Carpenter

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")

Hi Dan,

Thank you for catching this bug. I do request a minor adjustment to this patch for the sake of matching the format of the rest of the code in this file: please remove the goto err_alloc_ch and simply exit early with a return NULL.

Quoted 9 lines
---
 drivers/counter/counter-core.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/counter/counter-core.c +0 -0
index 7e0957eea094..0abb28cd3d16 100644

drivers/counter/counter-core.c struct counter_device *counter_alloc(size_t sizeof_priv) · line 94

Quoted 7 lines
drivers/counter/counter-core.c +1 -2
@@ -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;
+	if (!ch)
 		goto err_alloc_ch;

So return NULL here.

drivers/counter/counter-core.c struct counter_device *counter_alloc(size_t sizeof_priv) · line 124

Quoted 7 lines
drivers/counter/counter-core.c +0 -1
-	}
 
 	counter = &ch->counter;
 	dev = &counter->dev;
@@ -125,7 +123,7 @@ struct counter_device *counter_alloc(size_t sizeof_priv)
 	kfree(ch);
 err_alloc_ch:

Remove this label.

Thank you,

William Breathitt Gray

Quoted 25 lines
drivers/counter/counter-core.c +4 -4
-	return ERR_PTR(err);
+	return NULL;
 }
 EXPORT_SYMBOL_GPL(counter_alloc);
 
@@ -208,12 +206,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;
 }
Signature · 1 line
2.20.1