ti_sci: Prevent memory leak
authorAndrew Goodbody <andrew.goodbody@linaro.org>
Wed, 8 Oct 2025 09:23:35 +0000 (10:23 +0100)
committerTom Rini <trini@konsulko.com>
Thu, 16 Oct 2025 21:02:14 +0000 (15:02 -0600)
temp is assigned the pointer returned by malloc which is used without a
NULL check and then never freed. Add a NULL check and ensure temp is
freed on all return paths.

This issue was found by Smatch.

Reviewed-by: Udit Kumar <u-kumar1@ti.com>
Signed-off-by: Andrew Goodbody <andrew.goodbody@linaro.org>
Reviewed-by: Nishanth Menon <nm@ti.com>
Tested-by: Anshul Dalal <anshuld@ti.com>
drivers/firmware/ti_sci.c

index 8013afe..2042d88 100644 (file)
@@ -3083,7 +3083,10 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
                dev_err(dev, "%s resource type ids not available\n", of_prop);
                return ERR_PTR(sets);
        }
-       temp = malloc(sets);
+       temp = devm_kmalloc(dev, sets, GFP_KERNEL);
+       if (!temp)
+               return ERR_PTR(-ENOMEM);
+
        sets /= sizeof(u32);
        res->sets = sets;
 
@@ -3123,6 +3126,7 @@ devm_ti_sci_get_of_resource(const struct ti_sci_handle *handle,
                        return ERR_PTR(-ENOMEM);
        }
 
+       devm_kfree(dev, temp);
        if (valid_set)
                return res;