thermal/of: Fix potential uninitialized value access
authorPeng Fan <peng.fan@nxp.com>
Wed, 19 Jul 2023 01:16:36 +0000 (09:16 +0800)
committerRafael J. Wysocki <rafael.j.wysocki@intel.com>
Tue, 22 Aug 2023 19:22:02 +0000 (21:22 +0200)
If of_parse_phandle_with_args() called from __thermal_of_bind() or
__thermal_of_unbind() fails, cooling_spec.np will not be initialized,
so move the of_node_put() calls below the respective return value checks
to avoid dereferencing an uninitialized pointer.

Fixes: 3fd6d6e2b4e8 ("thermal/of: Rework the thermal device tree initialization")
Signed-off-by: Peng Fan <peng.fan@nxp.com>
[ rjw: Subject and changelog edits ]
Signed-off-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
drivers/thermal/thermal_of.c

index 330690a3a2089f00ca8137ef752331d0b4b10099..4ca905723429c7653e8add3f389602c12b80b439 100644 (file)
@@ -291,13 +291,13 @@ static int __thermal_of_unbind(struct device_node *map_np, int index, int trip_i
        ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
                                         index, &cooling_spec);
 
-       of_node_put(cooling_spec.np);
-
        if (ret < 0) {
                pr_err("Invalid cooling-device entry\n");
                return ret;
        }
 
+       of_node_put(cooling_spec.np);
+
        if (cooling_spec.args_count < 2) {
                pr_err("wrong reference to cooling device, missing limits\n");
                return -EINVAL;
@@ -324,13 +324,13 @@ static int __thermal_of_bind(struct device_node *map_np, int index, int trip_id,
        ret = of_parse_phandle_with_args(map_np, "cooling-device", "#cooling-cells",
                                         index, &cooling_spec);
 
-       of_node_put(cooling_spec.np);
-
        if (ret < 0) {
                pr_err("Invalid cooling-device entry\n");
                return ret;
        }
 
+       of_node_put(cooling_spec.np);
+
        if (cooling_spec.args_count < 2) {
                pr_err("wrong reference to cooling device, missing limits\n");
                return -EINVAL;