[PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc()

Li Qiang posted 1 patch 3 months, 3 weeks ago
drivers/hwmon/cgbc-hwmon.c | 3 +++
1 file changed, 3 insertions(+)
[PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc()
Posted by Li Qiang 3 months, 3 weeks ago
The driver allocates memory for sensor data using devm_kzalloc(), but
did not check if the allocation succeeded. In case of memory allocation
failure, dereferencing the NULL pointer would lead to a kernel crash.

Add a NULL pointer check and return -ENOMEM to handle allocation failure
properly.

Signed-off-by: Li Qiang <liqiang01@kylinos.cn>
---
 drivers/hwmon/cgbc-hwmon.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/hwmon/cgbc-hwmon.c b/drivers/hwmon/cgbc-hwmon.c
index 772f44d56ccf..3aff4e092132 100644
--- a/drivers/hwmon/cgbc-hwmon.c
+++ b/drivers/hwmon/cgbc-hwmon.c
@@ -107,6 +107,9 @@ static int cgbc_hwmon_probe_sensors(struct device *dev, struct cgbc_hwmon_data *
 	nb_sensors = data[0];
 
 	hwmon->sensors = devm_kzalloc(dev, sizeof(*hwmon->sensors) * nb_sensors, GFP_KERNEL);
+	if (!hwmon->sensors)
+		return -ENOMEM;
+
 	sensor = hwmon->sensors;
 
 	for (i = 0; i < nb_sensors; i++) {
-- 
2.25.1
Re: [PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc()
Posted by Guenter Roeck 3 months, 3 weeks ago
On Fri, Oct 17, 2025 at 02:34:14PM +0800, Li Qiang wrote:
> The driver allocates memory for sensor data using devm_kzalloc(), but
> did not check if the allocation succeeded. In case of memory allocation
> failure, dereferencing the NULL pointer would lead to a kernel crash.
> 
> Add a NULL pointer check and return -ENOMEM to handle allocation failure
> properly.
> 
> Signed-off-by: Li Qiang <liqiang01@kylinos.cn>
> Reviewed-by: Thomas Richard <thomas.richard@bootlin.com>

Applied.

Guenter
Re: [PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc()
Posted by Thomas Richard 3 months, 3 weeks ago
Hi,

On 10/17/25 8:34 AM, Li Qiang wrote:
> The driver allocates memory for sensor data using devm_kzalloc(), but
> did not check if the allocation succeeded. In case of memory allocation
> failure, dereferencing the NULL pointer would lead to a kernel crash.
> 
> Add a NULL pointer check and return -ENOMEM to handle allocation failure
> properly.
> 
> Signed-off-by: Li Qiang <liqiang01@kylinos.cn>
> ---
>  drivers/hwmon/cgbc-hwmon.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/hwmon/cgbc-hwmon.c b/drivers/hwmon/cgbc-hwmon.c
> index 772f44d56ccf..3aff4e092132 100644
> --- a/drivers/hwmon/cgbc-hwmon.c
> +++ b/drivers/hwmon/cgbc-hwmon.c
> @@ -107,6 +107,9 @@ static int cgbc_hwmon_probe_sensors(struct device *dev, struct cgbc_hwmon_data *
>  	nb_sensors = data[0];
>  
>  	hwmon->sensors = devm_kzalloc(dev, sizeof(*hwmon->sensors) * nb_sensors, GFP_KERNEL);
> +	if (!hwmon->sensors)
> +		return -ENOMEM;
> +
Thanks for your patch.

Reviewed-by: Thomas Richard <thomas.richard@bootlin.com>

Best Regards,

Thomas