diff mbox series

power: supply: fix null pointer check order in __power_supply_register

Message ID 20230223081047.219326-1-qinyu32@huawei.com
State Accepted
Commit bfaecf465a058b167abc7c0dfad985f167ff3af1
Headers show
Series power: supply: fix null pointer check order in __power_supply_register | expand

Commit Message

qinyu Feb. 23, 2023, 8:10 a.m. UTC
There is an null pointer check order issue here: if we have to
check !desc and !desc->name anyway, check it before dereferencing it in
pr_warn().

Signed-off-by: qinyu <qinyu32@huawei.com>
---
 drivers/power/supply/power_supply_core.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Sebastian Reichel Feb. 25, 2023, 12:20 a.m. UTC | #1
Hi,

On Thu, Feb 23, 2023 at 04:10:47PM +0800, qinyu wrote:
> There is an null pointer check order issue here: if we have to
> check !desc and !desc->name anyway, check it before dereferencing it in
> pr_warn().
> 
> Signed-off-by: qinyu <qinyu32@huawei.com>
> ---

Thanks, queued for v6.3.

-- Sebastian

>  drivers/power/supply/power_supply_core.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
> index cc5b2e22b..f3d7c1da2 100644
> --- a/drivers/power/supply/power_supply_core.c
> +++ b/drivers/power/supply/power_supply_core.c
> @@ -1207,13 +1207,13 @@ __power_supply_register(struct device *parent,
>  	struct power_supply *psy;
>  	int rc;
>  
> +	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
> +		return ERR_PTR(-EINVAL);
> +
>  	if (!parent)
>  		pr_warn("%s: Expected proper parent device for '%s'\n",
>  			__func__, desc->name);
>  
> -	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
> -		return ERR_PTR(-EINVAL);
> -
>  	if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
>  	    (!desc->usb_types || !desc->num_usb_types))
>  		return ERR_PTR(-EINVAL);
> -- 
> 2.33.0
>
diff mbox series

Patch

diff --git a/drivers/power/supply/power_supply_core.c b/drivers/power/supply/power_supply_core.c
index cc5b2e22b..f3d7c1da2 100644
--- a/drivers/power/supply/power_supply_core.c
+++ b/drivers/power/supply/power_supply_core.c
@@ -1207,13 +1207,13 @@  __power_supply_register(struct device *parent,
 	struct power_supply *psy;
 	int rc;
 
+	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
+		return ERR_PTR(-EINVAL);
+
 	if (!parent)
 		pr_warn("%s: Expected proper parent device for '%s'\n",
 			__func__, desc->name);
 
-	if (!desc || !desc->name || !desc->properties || !desc->num_properties)
-		return ERR_PTR(-EINVAL);
-
 	if (psy_has_property(desc, POWER_SUPPLY_PROP_USB_TYPE) &&
 	    (!desc->usb_types || !desc->num_usb_types))
 		return ERR_PTR(-EINVAL);