]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Issue #801: close the file descriptor when testing link counts
authorTim Kientzle <kientzle@gmail.com>
Wed, 12 Oct 2016 01:38:44 +0000 (18:38 -0700)
committerTim Kientzle <kientzle@gmail.com>
Wed, 12 Oct 2016 01:38:44 +0000 (18:38 -0700)
Thanks to Ed Maste for reporting this leak.
Found by: Coverity

cpio/test/main.c
cpio/test/test.h
libarchive/test/main.c
libarchive/test/test.h
tar/test/main.c
tar/test/test.h

index 29bbc37a46bbf8263f645c7f7faf3bf559f1d0e1..f3b431d0a6dff5f4232a757268182e6bbd11e7ae 100644 (file)
@@ -130,6 +130,13 @@ __FBSDID("$FreeBSD: src/usr.bin/cpio/test/main.c,v 1.3 2008/08/24 04:58:22 kient
 # include <crtdbg.h>
 #endif
 
+mode_t umasked(mode_t expected_mode)
+{
+       mode_t mode = umask(0);
+       umask(mode);
+       return expected_mode & ~mode;
+}
+
 /* Path to working directory for current test */
 const char *testworkdir;
 #ifdef PROGRAM
@@ -1294,6 +1301,11 @@ assertion_file_time(const char *file, int line,
        switch (type) {
        case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
        case 'b': filet = st.st_birthtime;
+               /* FreeBSD filesystems that don't support birthtime
+                * (e.g., UFS1) always return -1 here. */
+               if (filet == -1) {
+                       return (1);
+               }
                filet_nsec = st.st_birthtimespec.tv_nsec; break;
        case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
        default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
@@ -1425,7 +1437,7 @@ assertion_file_nlinks(const char *file, int line,
        assertion_count(file, line);
        r = lstat(pathname, &st);
        if (r == 0 && (int)st.st_nlink == nlinks)
-                       return (1);
+               return (1);
        failure_start(file, line, "File %s has %d links, expected %d",
            pathname, st.st_nlink, nlinks);
        failure_finish(NULL);
@@ -1661,6 +1673,7 @@ assertion_make_file(const char *file, int line,
        if (0 != chmod(path, mode)) {
                failure_start(file, line, "Could not chmod %s", path);
                failure_finish(NULL);
+               close(fd);
                return (0);
        }
        if (contents != NULL) {
@@ -1675,6 +1688,7 @@ assertion_make_file(const char *file, int line,
                        failure_start(file, line,
                            "Could not write to %s", path);
                        failure_finish(NULL);
+                       close(fd);
                        return (0);
                }
        }
index ecc6f27911b27b6676f2e96263f75f886aea20fc..0a8b31e1c8648828d3800b30d3e049f9c40e7e7a 100644 (file)
   assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
 #define assertFileSize(pathname, size)  \
   assertion_file_size(__FILE__, __LINE__, pathname, size)
+#define assertFileMode(pathname, mode)  \
+  assertion_file_mode(__FILE__, __LINE__, pathname, mode)
 #define assertTextFileContents(text, pathname) \
   assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
 #define assertFileContainsLinesAnyOrder(pathname, lines)       \
@@ -327,6 +329,9 @@ void copy_reference_file(const char *);
  */
 void extract_reference_files(const char **);
 
+/* Subtract umask from mode */
+mode_t umasked(mode_t expected_mode);
+
 /* Path to working directory for current test */
 extern const char *testworkdir;
 
index baf9d59800160fe0413bcc3017134f6a9618dbe8..aaa5cfa5d4b6bd5a9630623a0d0675e995607f3b 100644 (file)
@@ -128,6 +128,13 @@ __FBSDID("$FreeBSD: head/lib/libarchive/test/main.c 201247 2009-12-30 05:59:21Z
 # include <crtdbg.h>
 #endif
 
+mode_t umasked(mode_t expected_mode)
+{
+       mode_t mode = umask(0);
+       umask(mode);
+       return expected_mode & ~mode;
+}
+
 /* Path to working directory for current test */
 const char *testworkdir;
 #ifdef PROGRAM
@@ -1364,6 +1371,31 @@ assertion_file_birthtime_recent(const char *file, int line,
        return assertion_file_time(file, line, pathname, 0, 0, 'b', 1);
 }
 
+/* Verify mode of 'pathname'. */
+int
+assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
+{
+       int mode;
+       int r;
+
+       assertion_count(file, line);
+#if defined(_WIN32) && !defined(__CYGWIN__)
+       failure_start(file, line, "assertFileMode not yet implemented for Windows");
+#else
+       {
+               struct stat st;
+               r = lstat(pathname, &st);
+               mode = (int)(st.st_mode & 0777);
+       }
+       if (r == 0 && mode == expected_mode)
+                       return (1);
+       failure_start(file, line, "File %s has mode %o, expected %o",
+           pathname, mode, expected_mode);
+#endif
+       failure_finish(NULL);
+       return (0);
+}
+
 /* Verify mtime of 'pathname'. */
 int
 assertion_file_mtime(const char *file, int line,
@@ -1440,31 +1472,6 @@ assertion_file_size(const char *file, int line, const char *pathname, long size)
        return (0);
 }
 
-/* Verify mode of 'pathname'. */
-int
-assertion_file_mode(const char *file, int line, const char *pathname, int expected_mode)
-{
-       int mode;
-       int r;
-
-       assertion_count(file, line);
-#if defined(_WIN32) && !defined(__CYGWIN__)
-       failure_start(file, line, "assertFileMode not yet implemented for Windows");
-#else
-       {
-               struct stat st;
-               r = lstat(pathname, &st);
-               mode = (int)(st.st_mode & 0777);
-       }
-       if (r == 0 && mode == expected_mode)
-                       return (1);
-       failure_start(file, line, "File %s has mode %o, expected %o",
-           pathname, mode, expected_mode);
-#endif
-       failure_finish(NULL);
-       return (0);
-}
-
 /* Assert that 'pathname' is a dir.  If mode >= 0, verify that too. */
 int
 assertion_is_dir(const char *file, int line, const char *pathname, int mode)
@@ -1664,6 +1671,7 @@ assertion_make_file(const char *file, int line,
        if (0 != chmod(path, mode)) {
                failure_start(file, line, "Could not chmod %s", path);
                failure_finish(NULL);
+               close(fd);
                return (0);
        }
        if (contents != NULL) {
@@ -1678,6 +1686,7 @@ assertion_make_file(const char *file, int line,
                        failure_start(file, line,
                            "Could not write to %s", path);
                        failure_finish(NULL);
+                       close(fd);
                        return (0);
                }
        }
index 2fe09ff169b7fc88b417403529165975686aedcb..4d5030329d53ad90bd5cf6f6d45de549fc1f939d 100644 (file)
@@ -243,12 +243,12 @@ int assertion_file_birthtime_recent(const char *, int, const char *);
 int assertion_file_contains_lines_any_order(const char *, int, const char *, const char **);
 int assertion_file_contents(const char *, int, const void *, int, const char *);
 int assertion_file_exists(const char *, int, const char *);
+int assertion_file_mode(const char *, int, const char *, int);
 int assertion_file_mtime(const char *, int, const char *, long, long);
 int assertion_file_mtime_recent(const char *, int, const char *);
 int assertion_file_nlinks(const char *, int, const char *, int);
 int assertion_file_not_exists(const char *, int, const char *);
 int assertion_file_size(const char *, int, const char *, long);
-int assertion_file_mode(const char *, int, const char *, int);
 int assertion_is_dir(const char *, int, const char *, int);
 int assertion_is_hardlink(const char *, int, const char *, const char *);
 int assertion_is_not_hardlink(const char *, int, const char *, const char *);
@@ -329,6 +329,9 @@ void copy_reference_file(const char *);
  */
 void extract_reference_files(const char **);
 
+/* Subtract umask from mode */
+mode_t umasked(mode_t expected_mode);
+
 /* Path to working directory for current test */
 extern const char *testworkdir;
 
index a9300d3f2b24586a72f28b822c317484112eafec..d4a3cce7a4d2d1d6f3e10ceb1732e91291bb971e 100644 (file)
@@ -1301,6 +1301,11 @@ assertion_file_time(const char *file, int line,
        switch (type) {
        case 'a': filet_nsec = st.st_atimespec.tv_nsec; break;
        case 'b': filet = st.st_birthtime;
+               /* FreeBSD filesystems that don't support birthtime
+                * (e.g., UFS1) always return -1 here. */
+               if (filet == -1) {
+                       return (1);
+               }
                filet_nsec = st.st_birthtimespec.tv_nsec; break;
        case 'm': filet_nsec = st.st_mtimespec.tv_nsec; break;
        default: fprintf(stderr, "INTERNAL: Bad type %c for file time", type);
@@ -1432,7 +1437,7 @@ assertion_file_nlinks(const char *file, int line,
        assertion_count(file, line);
        r = lstat(pathname, &st);
        if (r == 0 && (int)st.st_nlink == nlinks)
-                       return (1);
+               return (1);
        failure_start(file, line, "File %s has %d links, expected %d",
            pathname, st.st_nlink, nlinks);
        failure_finish(NULL);
@@ -1668,6 +1673,7 @@ assertion_make_file(const char *file, int line,
        if (0 != chmod(path, mode)) {
                failure_start(file, line, "Could not chmod %s", path);
                failure_finish(NULL);
+               close(fd);
                return (0);
        }
        if (contents != NULL) {
@@ -1682,6 +1688,7 @@ assertion_make_file(const char *file, int line,
                        failure_start(file, line,
                            "Could not write to %s", path);
                        failure_finish(NULL);
+                       close(fd);
                        return (0);
                }
        }
index dbf9f2de076e4c43c087eff9084dbaf54956e8ec..73c0668ee67ed5739c541bbe9e5bce90ff392b06 100644 (file)
   assertion_file_nlinks(__FILE__, __LINE__, pathname, nlinks)
 #define assertFileSize(pathname, size)  \
   assertion_file_size(__FILE__, __LINE__, pathname, size)
+#define assertFileMode(pathname, mode)  \
+  assertion_file_mode(__FILE__, __LINE__, pathname, mode)
 #define assertTextFileContents(text, pathname) \
   assertion_text_file_contents(__FILE__, __LINE__, text, pathname)
 #define assertFileContainsLinesAnyOrder(pathname, lines)       \