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 <[email protected]>
---
 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)

Reply via email to