[PATCH v2] dt-ops: fix memory leak when new_node malloc fails

qiuguorui1 qiuguorui1 at huawei.com
Mon Nov 30 23:06:16 EST 2020


In function dtb_set_property, when malloc new_node fails,
we need to free new_dtb before return.

Fixes: f56cbcf4c2766 ("kexec/dt-ops.c: Fix '/chosen' v/s 'chosen' node
being passed to fdt helper functions")
Signed-off-by: qiuguorui1 <qiuguorui1 at huawei.com>

v2:
  use goto instead of free here,based on Simon Horman's advice:
  "Can we use the goto idiom here, as is used elsewhere in this function?"

v1: http://lists.infradead.org/pipermail/kexec/2020-November/021809.html
---
 kexec/dt-ops.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/kexec/dt-ops.c b/kexec/dt-ops.c
index dd2feaa..0a96b75 100644
--- a/kexec/dt-ops.c
+++ b/kexec/dt-ops.c
@@ -89,7 +89,8 @@ int dtb_set_property(char **dtb, off_t *dtb_size, const char *node,
 	new_node = malloc(strlen("/") + strlen(node) + 1);
 	if (!new_node) {
 		dbgprintf("%s: malloc failed\n", __func__);
-		return -ENOMEM;
+		result = -ENOMEM;
+		goto on_error;
 	}
 
 	strcpy(new_node, "/");
-- 
2.12.3




More information about the kexec mailing list