diff mbox

mfd: twl-core: One function call less in add_numbered_child() after error detection

Message ID 5682D228.7070902@users.sourceforge.net (mailing list archive)
State New, archived
Headers show

Commit Message

SF Markus Elfring Dec. 29, 2015, 6:34 p.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Tue, 29 Dec 2015 19:29:08 +0100

The platform_device_put() function was called in one case by the
add_numbered_child() function during error handling even if the passed
variable "pdev" contained a null pointer.

Implementation details could be improved by the adjustment of jump targets
according to the Linux coding style convention.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/mfd/twl-core.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

Comments

Lee Jones Jan. 11, 2016, 8:29 a.m. UTC | #1
On Tue, 29 Dec 2015, SF Markus Elfring wrote:

> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Tue, 29 Dec 2015 19:29:08 +0100
> 
> The platform_device_put() function was called in one case by the
> add_numbered_child() function during error handling even if the passed
> variable "pdev" contained a null pointer.
> 
> Implementation details could be improved by the adjustment of jump targets
> according to the Linux coding style convention.
> 
> This issue was detected by using the Coccinelle software.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  drivers/mfd/twl-core.c | 21 ++++++++++-----------
>  1 file changed, 10 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/mfd/twl-core.c b/drivers/mfd/twl-core.c
> index 831696e..0d9350c 100644
> --- a/drivers/mfd/twl-core.c
> +++ b/drivers/mfd/twl-core.c
> @@ -625,7 +625,7 @@ add_numbered_child(unsigned mod_no, const char *name, int num,
>  	if (!pdev) {
>  		dev_dbg(&twl->client->dev, "can't alloc dev\n");

Change this to be dev_err()

>  		status = -ENOMEM;
> -		goto err;
> +		goto report_failure;

... and just return status from here.

>  	}
>  
>  	pdev->dev.parent = &twl->client->dev;
> @@ -634,7 +634,7 @@ add_numbered_child(unsigned mod_no, const char *name, int num,
>  		status = platform_device_add_data(pdev, pdata, pdata_len);
>  		if (status < 0) {
>  			dev_dbg(&pdev->dev, "can't add platform_data\n");
> -			goto err;
> +			goto put_device;
>  		}
>  	}
>  
> @@ -647,21 +647,20 @@ add_numbered_child(unsigned mod_no, const char *name, int num,
>  		status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
>  		if (status < 0) {
>  			dev_dbg(&pdev->dev, "can't add irqs\n");
> -			goto err;
> +			goto put_device;
>  		}
>  	}
>  
>  	status = platform_device_add(pdev);
> -	if (status == 0)
> +	if (!status) {

You've changed the way you handle errors from this point.  To be more
consistent it would be better if you checked for status, then jump to
put_device in the case of a failure, as you do above.

>  		device_init_wakeup(&pdev->dev, can_wakeup);
> -
> -err:
> -	if (status < 0) {
> -		platform_device_put(pdev);
> -		dev_err(&twl->client->dev, "can't add %s dev\n", name);
> -		return ERR_PTR(status);
> +		return &pdev->dev;
>  	}
> -	return &pdev->dev;
> +put_device:
> +	platform_device_put(pdev);
> +report_failure:
> +	dev_err(&twl->client->dev, "can't add %s dev\n", name);
> +	return ERR_PTR(status);
>  }
>  
>  static inline struct device *add_child(unsigned mod_no, const char *name,
SF Markus Elfring May 15, 2016, 6:11 p.m. UTC | #2
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Sun, 15 May 2016 19:55:30 +0200

A few update suggestions were taken into account
from static source code analysis.

Markus Elfring (2):
  Return directly after a failed platform_device_alloc() in add_numbered_child()
  Refactoring for add_numbered_child()

 drivers/mfd/twl-core.c | 24 +++++++++++-------------
 1 file changed, 11 insertions(+), 13 deletions(-)
diff mbox

Patch

diff --git a/drivers/mfd/twl-core.c b/drivers/mfd/twl-core.c
index 831696e..0d9350c 100644
--- a/drivers/mfd/twl-core.c
+++ b/drivers/mfd/twl-core.c
@@ -625,7 +625,7 @@  add_numbered_child(unsigned mod_no, const char *name, int num,
 	if (!pdev) {
 		dev_dbg(&twl->client->dev, "can't alloc dev\n");
 		status = -ENOMEM;
-		goto err;
+		goto report_failure;
 	}
 
 	pdev->dev.parent = &twl->client->dev;
@@ -634,7 +634,7 @@  add_numbered_child(unsigned mod_no, const char *name, int num,
 		status = platform_device_add_data(pdev, pdata, pdata_len);
 		if (status < 0) {
 			dev_dbg(&pdev->dev, "can't add platform_data\n");
-			goto err;
+			goto put_device;
 		}
 	}
 
@@ -647,21 +647,20 @@  add_numbered_child(unsigned mod_no, const char *name, int num,
 		status = platform_device_add_resources(pdev, r, irq1 ? 2 : 1);
 		if (status < 0) {
 			dev_dbg(&pdev->dev, "can't add irqs\n");
-			goto err;
+			goto put_device;
 		}
 	}
 
 	status = platform_device_add(pdev);
-	if (status == 0)
+	if (!status) {
 		device_init_wakeup(&pdev->dev, can_wakeup);
-
-err:
-	if (status < 0) {
-		platform_device_put(pdev);
-		dev_err(&twl->client->dev, "can't add %s dev\n", name);
-		return ERR_PTR(status);
+		return &pdev->dev;
 	}
-	return &pdev->dev;
+put_device:
+	platform_device_put(pdev);
+report_failure:
+	dev_err(&twl->client->dev, "can't add %s dev\n", name);
+	return ERR_PTR(status);
 }
 
 static inline struct device *add_child(unsigned mod_no, const char *name,