[2/2] thermal/of: accessing potential uninitialized value

Message ID 20230719011636.2893238-2-peng.fan@oss.nxp.com
State New
Headers
Series [1/2] thermal/core: fix potential memory leak |

Commit Message

Peng Fan (OSS) July 19, 2023, 1:16 a.m. UTC
  From: Peng Fan <peng.fan@nxp.com>

If of_parse_phandle_with_args returns failure, the cooling_spec.np is
uninitialized. So move of_node_put below the return value check to avoid
accessing uninitialized value.

Addresses-Coverity: 25772185 ("Uninitialized pointer read")
Addresses-Coverity: 25772845 ("Uninitialized pointer read")
Signed-off-by: Peng Fan <peng.fan@nxp.com>
---
 drivers/thermal/thermal_of.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
  

Patch

diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c
index 6fb14e521197..021327fb434f 100644
--- a/drivers/thermal/thermal_of.c
+++ b/drivers/thermal/thermal_of.c
@@ -298,13 +298,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;
@@ -331,13 +331,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;