diff mbox series

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

Message ID 164269263036.39378.10026871398998590929.stgit@localhost
State New
Headers show
Series FWU: Add FWU Multi Bank Update for DeveloerBox | expand

Commit Message

Masami Hiramatsu Jan. 20, 2022, 3:30 p.m. UTC
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@linaro.org>
---
 lib/fwu_updates/fwu_mdata_gpt_blk.c |   11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
diff mbox series

Patch

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)