[PATCH V2] charger: max14577: Handle NULL pdata when CONFIG_OF is not set

Charles Han posted 1 patch 7 months ago
There is a newer version of this series
drivers/power/supply/max14577_charger.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH V2] charger: max14577: Handle NULL pdata when CONFIG_OF is not set
Posted by Charles Han 7 months ago
When the kernel is not configured  CONFIG_OF, the max14577_charger_dt_init
function returns NULL. Fix the max14577_charger_probe functionby returning
 -ENODATA instead of potentially passing a NULL pointer to PTR_ERR.

Fix below smatch warning.
smatch warnings:
drivers/power/supply/max14577_charger.c:576 max14577_charger_probe() warn: passing zero to 'PTR_ERR'

Fixes: e30110e9c96f ("charger: max14577: Configure battery-dependent settings from DTS and sysfs")
Signed-off-by: Charles Han <hanchunchao@inspur.com>
---
 drivers/power/supply/max14577_charger.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/power/supply/max14577_charger.c b/drivers/power/supply/max14577_charger.c
index 1cef2f860b5f..af1694cac5ea 100644
--- a/drivers/power/supply/max14577_charger.c
+++ b/drivers/power/supply/max14577_charger.c
@@ -501,7 +501,7 @@ static struct max14577_charger_platform_data *max14577_charger_dt_init(
 static struct max14577_charger_platform_data *max14577_charger_dt_init(
 		struct platform_device *pdev)
 {
-	return NULL;
+	return -ENODATA;
 }
 #endif /* CONFIG_OF */
 
@@ -572,7 +572,7 @@ static int max14577_charger_probe(struct platform_device *pdev)
 	chg->max14577 = max14577;
 
 	chg->pdata = max14577_charger_dt_init(pdev);
-	if (IS_ERR_OR_NULL(chg->pdata))
+	if (IS_ERR(chg->pdata))
 		return PTR_ERR(chg->pdata);
 
 	ret = max14577_charger_reg_init(chg);
-- 
2.43.0
Re: [PATCH V2] charger: max14577: Handle NULL pdata when CONFIG_OF is not set
Posted by kernel test robot 7 months ago
Hi Charles,

kernel test robot noticed the following build errors:

[auto build test ERROR on sre-power-supply/for-next]
[also build test ERROR on linus/master v6.15-rc7 next-20250516]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Charles-Han/charger-max14577-Handle-NULL-pdata-when-CONFIG_OF-is-not-set/20250519-095431
base:   https://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply.git for-next
patch link:    https://lore.kernel.org/r/20250519014804.2244-1-hanchunchao%40inspur.com
patch subject: [PATCH V2] charger: max14577: Handle NULL pdata when CONFIG_OF is not set
config: sh-randconfig-002-20250519 (https://download.01.org/0day-ci/archive/20250519/202505191305.un0tzZu1-lkp@intel.com/config)
compiler: sh4-linux-gcc (GCC) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250519/202505191305.un0tzZu1-lkp@intel.com/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of
the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202505191305.un0tzZu1-lkp@intel.com/

All errors (new ones prefixed by >>):

   drivers/power/supply/max14577_charger.c: In function 'max14577_charger_dt_init':
>> drivers/power/supply/max14577_charger.c:504:16: error: returning 'int' from a function with return type 'struct max14577_charger_platform_data *' makes pointer from integer without a cast [-Wint-conversion]
     504 |         return -ENODATA;
         |                ^


vim +504 drivers/power/supply/max14577_charger.c

   454	
   455	#ifdef CONFIG_OF
   456	static struct max14577_charger_platform_data *max14577_charger_dt_init(
   457			struct platform_device *pdev)
   458	{
   459		struct max14577_charger_platform_data *pdata;
   460		struct device_node *np = pdev->dev.of_node;
   461		int ret;
   462	
   463		if (!np) {
   464			dev_err(&pdev->dev, "No charger OF node\n");
   465			return ERR_PTR(-EINVAL);
   466		}
   467	
   468		pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
   469		if (!pdata)
   470			return ERR_PTR(-ENOMEM);
   471	
   472		ret = of_property_read_u32(np, "maxim,constant-uvolt",
   473				&pdata->constant_uvolt);
   474		if (ret) {
   475			dev_err(&pdev->dev, "Cannot parse maxim,constant-uvolt field from DT\n");
   476			return ERR_PTR(ret);
   477		}
   478	
   479		ret = of_property_read_u32(np, "maxim,fast-charge-uamp",
   480				&pdata->fast_charge_uamp);
   481		if (ret) {
   482			dev_err(&pdev->dev, "Cannot parse maxim,fast-charge-uamp field from DT\n");
   483			return ERR_PTR(ret);
   484		}
   485	
   486		ret = of_property_read_u32(np, "maxim,eoc-uamp", &pdata->eoc_uamp);
   487		if (ret) {
   488			dev_err(&pdev->dev, "Cannot parse maxim,eoc-uamp field from DT\n");
   489			return ERR_PTR(ret);
   490		}
   491	
   492		ret = of_property_read_u32(np, "maxim,ovp-uvolt", &pdata->ovp_uvolt);
   493		if (ret) {
   494			dev_err(&pdev->dev, "Cannot parse maxim,ovp-uvolt field from DT\n");
   495			return ERR_PTR(ret);
   496		}
   497	
   498		return pdata;
   499	}
   500	#else /* CONFIG_OF */
   501	static struct max14577_charger_platform_data *max14577_charger_dt_init(
   502			struct platform_device *pdev)
   503	{
 > 504		return -ENODATA;
   505	}
   506	#endif /* CONFIG_OF */
   507	

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH V2] charger: max14577: Handle NULL pdata when CONFIG_OF is not set
Posted by Krzysztof Kozlowski 7 months ago
On 19/05/2025 03:48, Charles Han wrote:
> When the kernel is not configured  CONFIG_OF, the max14577_charger_dt_init
> function returns NULL. Fix the max14577_charger_probe functionby returning
>  -ENODATA instead of potentially passing a NULL pointer to PTR_ERR.
> 
> Fix below smatch warning.
> smatch warnings:
> drivers/power/supply/max14577_charger.c:576 max14577_charger_probe() warn: passing zero to 'PTR_ERR'
> 
> Fixes: e30110e9c96f ("charger: max14577: Configure battery-dependent settings from DTS and sysfs")
> Signed-off-by: Charles Han <hanchunchao@inspur.com>
> ---
>  drivers/power/supply/max14577_charger.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/power/supply/max14577_charger.c b/drivers/power/supply/max14577_charger.c
> index 1cef2f860b5f..af1694cac5ea 100644
> --- a/drivers/power/supply/max14577_charger.c
> +++ b/drivers/power/supply/max14577_charger.c
> @@ -501,7 +501,7 @@ static struct max14577_charger_platform_data *max14577_charger_dt_init(
>  static struct max14577_charger_platform_data *max14577_charger_dt_init(
>  		struct platform_device *pdev)
>  {
> -	return NULL;
> +	return -ENODATA;

No, you did not test it and it is obviously buggy code. Please learn
first about pointers.

Best regards,
Krzysztof