Fix Coverity issues 31/304631/2 accepted/tizen/8.0/unified/20240123.170040 accepted/tizen/unified/20240125.112655 accepted/tizen/unified/x/20240205.063946
authorAdam Michalski <a.michalski2@partner.samsung.com>
Mon, 22 Jan 2024 13:08:42 +0000 (14:08 +0100)
committerAdam Michalski <a.michalski2@partner.samsung.com>
Mon, 22 Jan 2024 14:35:05 +0000 (15:35 +0100)
Ref.: CIDs #1749002, #1749004.

Change-Id: Ie652e892c770210b48c5844376f56b370430a21c

src/libisu/libisu-internal.c

index 5295511..1048caf 100644 (file)
@@ -78,6 +78,8 @@ static int copy_content(const char *src, const char *dst, const char **exclude)
         SLOGE("Cannot open directory %s: (%d) %m", src, errno);
         return -1;
     }
+
+    int src_file = -1, dst_file = -1;
     while ((entry = readdir(dir)) != NULL) {
         bool ignore = false;
 
@@ -112,37 +114,37 @@ static int copy_content(const char *src, const char *dst, const char **exclude)
                 goto exit;
             }
         } else if (entry->d_type == DT_REG) {
-            int src_file = open(newsrc, O_RDONLY);
-            int dst_file = open(newdst, O_CREAT | O_WRONLY, mode);
+            src_file = open(newsrc, O_RDONLY);
+            dst_file = open(newdst, O_CREAT | O_WRONLY, mode);
             if (src_file == -1 || dst_file == -1) {
-                if (src_file != -1) {
+                if (src_file != -1)
                     SLOGE("Cannot open file %s: (%d) %m", newdst, errno);
-                    close(src_file);
-                }
-                if (dst_file != -1) {
+                if (dst_file != -1)
                     SLOGE("Cannot open file %s: (%d) %m", newsrc, errno);
-                    close(dst_file);
-                }
                 goto exit;
             }
             struct stat fileinfo = {0};
             if (fstat(src_file, &fileinfo) == -1) {
                 SLOGE("Get file %s status error: (%d) %m", newsrc, errno);
-                close(src_file);
-                close(dst_file);
                 goto exit;
             }
 
             if (sendfile(dst_file, src_file, NULL, fileinfo.st_size) == -1) {
                 SLOGE("Copy file content (%s -> %s) error: (%d) %m", newsrc, newdst, errno);
-                close(src_file);
-                close(dst_file);
                 goto exit;
             }
+            close(src_file);
+            src_file = -1;
+            close(dst_file);
+            dst_file = -1;
         }
     }
     result = 0;
 exit:
+    if (src_file != -1)
+        close(src_file);
+    if (dst_file != -1)
+        close(dst_file);
     closedir(dir);
     return result;
 }