Memory Leak: File descriptor not closed. 90/188390/5
authorGourav Mittal <gourav.m@samsung.com>
Tue, 4 Sep 2018 10:29:33 +0000 (15:59 +0530)
committerGourav Mittal <gourav.m@samsung.com>
Tue, 4 Sep 2018 14:32:10 +0000 (20:02 +0530)
Change-Id: I3861b4dcd6ec8934c76afc9de81f2e0922d7802d
Signed-off-by: Gourav Mittal <gourav.m@samsung.com>
src/extract_descs_strings.c

index 64d5b7e..4e538ac 100644 (file)
@@ -8,8 +8,8 @@
 
 int main() {
 
-    int ret;
-    FILE *descs, *strs;
+    int ret = 0;
+    FILE *descs = NULL, *strs = NULL;
 
     /* open file for descriptors */
     descs = fopen("descs","w");
@@ -23,6 +23,10 @@ int main() {
     strs = fopen("strs", "w");
     if (!strs) {
         ret = -errno;
+
+        if (fclose(descs) != 0)
+            perror("could not close descriptor");
+
         perror("could not open file with strings");
         return ret;
     }
@@ -30,19 +34,38 @@ int main() {
     /* write descriptors to file */
     ret = fwrite(&descriptors, sizeof(descriptors), 1, descs);
     if (ret < 0) {
+        ret = ferror(descs);
+
+        if (fclose(descs) != 0)
+            perror("could not close descriptor");
+
+        if (fclose(strs) != 0)
+            perror("could not close descriptor");
+
         perror("could not write descriptors");
-        return ferror(descs);
+        return ret;
     }
 
     /* write strings to file */
     ret = fwrite(&strings, sizeof(strings), 1, strs);
     if(ret < 0) {
+        ret = ferror(strs);
+
+        if (fclose(descs) != 0)
+            perror("could not close descriptor");
+
+        if (fclose(strs) != 0)
+            perror("could not close descriptor");
+
        perror("could not write strings");
-       return ferror(strs);
+       return ret;
     }
 
-    fclose(descs);
-    fclose(strs);
+    if (fclose(descs) != 0)
+        perror("could not close descriptor");
+
+    if (fclose(strs) != 0)
+        perror("could not close descriptor");
 
     return 0;
 }