fix file descriptor leaks reported by cppcheck
authorKamil Dudka <kdudka@redhat.com>
Tue, 15 Oct 2013 23:48:41 +0000 (15 19:48 -0400)
committerChris Frey <cdfrey@foursquare.net>
Tue, 15 Oct 2013 23:48:41 +0000 (15 19:48 -0400)
Bug: https://bugzilla.redhat.com/785760

lib/append.c
lib/extract.c
libtar/libtar.c

index e8bd89d..ff58532 100644 (file)
@@ -216,6 +216,7 @@ tar_append_regfile(TAR *t, const char *realname)
        int filefd;
        int i, j;
        size_t size;
+       int rv = -1;
 
        filefd = open(realname, O_RDONLY);
        if (filefd == -1)
@@ -234,25 +235,28 @@ tar_append_regfile(TAR *t, const char *realname)
                {
                        if (j != -1)
                                errno = EINVAL;
-                       return -1;
+                       goto fail;
                }
                if (tar_block_write(t, &block) == -1)
-                       return -1;
+                       goto fail;
        }
 
        if (i > 0)
        {
                j = read(filefd, &block, i);
                if (j == -1)
-                       return -1;
+                       goto fail;
                memset(&(block[i]), 0, T_BLOCKSIZE - i);
                if (tar_block_write(t, &block) == -1)
-                       return -1;
+                       goto fail;
        }
 
+       /* success! */
+       rv = 0;
+fail:
        close(filefd);
 
-       return 0;
+       return rv;
 }
 
 
index 36357e7..9fc6ad5 100644 (file)
@@ -228,13 +228,17 @@ tar_extract_regfile(TAR *t, char *realname)
                {
                        if (k != -1)
                                errno = EINVAL;
+                       close(fdout);
                        return -1;
                }
 
                /* write block to output file */
                if (write(fdout, buf,
                          ((i > T_BLOCKSIZE) ? T_BLOCKSIZE : i)) == -1)
+               {
+                       close(fdout);
                        return -1;
+               }
        }
 
        /* close output file */
index 9fa92b2..bb5644c 100644 (file)
@@ -83,7 +83,10 @@ gzopen_frontend(char *pathname, int oflags, int mode)
                return -1;
 
        if ((oflags & O_CREAT) && fchmod(fd, mode))
+       {
+               close(fd);
                return -1;
+       }
 
        gzf = gzdopen(fd, gzoflags);
        if (!gzf)