* [PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc()
@ 2025-10-17 6:34 Li Qiang
2025-10-17 8:12 ` Thomas Richard
2025-10-17 12:42 ` Guenter Roeck
0 siblings, 2 replies; 3+ messages in thread
From: Li Qiang @ 2025-10-17 6:34 UTC (permalink / raw)
To: thomas.richard, linux; +Cc: linux-hwmon, linux-kernel, Li Qiang
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc()
2025-10-17 6:34 [PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc() Li Qiang
@ 2025-10-17 8:12 ` Thomas Richard
2025-10-17 12:42 ` Guenter Roeck
1 sibling, 0 replies; 3+ messages in thread
From: Thomas Richard @ 2025-10-17 8:12 UTC (permalink / raw)
To: Li Qiang, linux; +Cc: linux-hwmon, linux-kernel
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc()
2025-10-17 6:34 [PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc() Li Qiang
2025-10-17 8:12 ` Thomas Richard
@ 2025-10-17 12:42 ` Guenter Roeck
1 sibling, 0 replies; 3+ messages in thread
From: Guenter Roeck @ 2025-10-17 12:42 UTC (permalink / raw)
To: Li Qiang; +Cc: thomas.richard, linux-hwmon, linux-kernel
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
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-10-17 12:42 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-10-17 6:34 [PATCH] hwmon: cgbc-hwmon: Add missing NULL check after devm_kzalloc() Li Qiang
2025-10-17 8:12 ` Thomas Richard
2025-10-17 12:42 ` Guenter Roeck
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox