[RFC PATCH 07/14] FWU: Free metadata copy if gpt_get_mdata() failed

Masami Hiramatsu masami.hiramatsu at linaro.org
Thu Jan 20 16:30:30 CET 2022


It is better if a function which returns an error then release
all allocated memory resources. This simplifies the mind model
and less chance to forgot to free memory and double free.

Signed-off-by: Masami Hiramatsu <masami.hiramatsu at linaro.org>
---
 lib/fwu_updates/fwu_mdata_gpt_blk.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/lib/fwu_updates/fwu_mdata_gpt_blk.c b/lib/fwu_updates/fwu_mdata_gpt_blk.c
index 0259da37c3..7f8b8b68fc 100644
--- a/lib/fwu_updates/fwu_mdata_gpt_blk.c
+++ b/lib/fwu_updates/fwu_mdata_gpt_blk.c
@@ -215,7 +215,8 @@ static int gpt_get_mdata(struct fwu_mdata **mdata)
 	ret = gpt_read_mdata(desc, *mdata, primary_mpart);
 	if (ret < 0) {
 		log_err("Failed to read the FWU metadata from the device\n");
-		return -EIO;
+		ret = -EIO;
+		goto out;
 	}
 
 	ret = fwu_verify_mdata(*mdata, 1);
@@ -230,7 +231,8 @@ static int gpt_get_mdata(struct fwu_mdata **mdata)
 	ret = gpt_read_mdata(desc, *mdata, secondary_mpart);
 	if (ret < 0) {
 		log_err("Failed to read the FWU metadata from the device\n");
-		return -EIO;
+		ret = -EIO;
+		goto out;
 	}
 
 	ret = fwu_verify_mdata(*mdata, 0);
@@ -238,7 +240,10 @@ static int gpt_get_mdata(struct fwu_mdata **mdata)
 		return 0;
 
 	/* Both the FWU metadata copies are corrupted. */
-	return -1;
+	ret = -1;
+out:
+	free(*mdata);
+	return ret;
 }
 
 static int gpt_check_mdata_validity(void)



More information about the U-Boot mailing list