upgrade-apply-deltafs: Fix memory leak 86/287486/3
authorMateusz Moscicki <m.moscicki2@partner.samsung.com>
Mon, 30 Jan 2023 14:45:23 +0000 (15:45 +0100)
committerMateusz Moscicki <m.moscicki2@partner.samsung.com>
Fri, 3 Feb 2023 12:17:50 +0000 (13:17 +0100)
Change-Id: Ifbcc809a3888c76032ff9b91a5b6b9c57f0055f2

src/upgrade-apply-deltafs/engine/SS_PatchDelta.c

index 233cb36..9346bc6 100755 (executable)
@@ -244,8 +244,8 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename,
                                        if (output < 0) {
                                                strerror_r(errno, buf, sizeof(buf));
                                                LOGE("failed to open output file %s: %s\n", outname, buf);
-                                               SS_Free(outname);
-                                               return E_SS_FAILURE;
+                                               result = E_SS_FAILURE;
+                                               goto exit;
                                        }
                                }
                        }
@@ -259,9 +259,9 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename,
                if (result != S_SS_SUCCESS) {
                        if (retry == 0) {
                                LOGE("applying patch failed result : [%d]\n", result);
-                               SS_Free(outname);//wgid: 20739
                                SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
-                               return E_SS_FAILURE;
+                               result = E_SS_FAILURE;
+                               goto exit;
                        } else {
                                LOGE("applying patch failed; retrying\n");
                                SS_Free(outname);//wgid: 20739
@@ -277,7 +277,8 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename,
        int fd = open(outname, O_RDONLY);
        if (fd == -1) {
                LOGE("cannot open %s to verify SHA1\n", outname);
-               return E_SS_FAILURE;
+               result = E_SS_FAILURE;
+               goto exit;
        }
        unsigned char buff[4096];
        for (;;) {
@@ -285,7 +286,8 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename,
                if (res == -1) {
                        LOGE("cannot read %s to verify SHA1\n", outname);
                        close(fd);
-                       return E_SS_FAILURE;
+                       result = E_SS_FAILURE;
+                       goto exit;
                }
                if (res == 0)
                        break;
@@ -298,9 +300,8 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename,
        if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) {
                LOGE("patch did not produce expected sha1\n");
                SS_SetUpgradeState(E_SS_FSSHA_MISMATCH);
-               if (outname != NULL)
-                       SS_Free(outname);
-               return E_SS_FAILURE;
+               result = E_SS_FAILURE;
+               goto exit;
        }
 
        // Finally, rename the .patch file to replace the target file.
@@ -309,23 +310,22 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename,
                strerror_r(errno, buf, sizeof(buf));
                LOGE("rename of .patch to \"%s\" failed: %s\n", target_filename, buf);
                SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
-               if (outname != NULL)
-                       SS_Free(outname);
-               return E_SS_FAILURE;
+               result = E_SS_FAILURE;
+               goto exit;
        }
 #else
        if (rename(outname, target_filename) != 0) {
                strerror_r(errno, buf, sizeof(buf));
                LOGE("rename of .patch to \"%s\" failed: %s\n", target_filename, buf);
                SS_SetUpgradeState(E_SS_FSUPDATEFAILED);
-               if (outname != NULL)
-                       SS_Free(outname);
-               return E_SS_FAILURE;
+               result = E_SS_FAILURE;
+               goto exit;
        }
        //remove source file if target is not same
        if (strcmp(source_filename, target_filename) != 0)
                unlink(source_filename);
 #endif
+exit:
        SS_Free(outname);
 
        return result;