]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Free p (the memory allocated via slurpfile) when done with the contents
authorNgie Cooper <yanegomi@gmail.com>
Sun, 11 Dec 2016 00:34:51 +0000 (16:34 -0800)
committerNgie Cooper <yanegomi@gmail.com>
Sun, 11 Dec 2016 01:15:46 +0000 (17:15 -0800)
Reported by: Coverity
CID: 1331627-133163013553321331634-133164513316471355331

17 files changed:
tar/test/test_leading_slash.c
tar/test/test_option_a.c
tar/test/test_option_b64encode.c
tar/test/test_option_gid_gname.c
tar/test/test_option_grzip.c
tar/test/test_option_j.c
tar/test/test_option_lrzip.c
tar/test/test_option_lz4.c
tar/test/test_option_lzma.c
tar/test/test_option_lzop.c
tar/test/test_option_r.c
tar/test/test_option_uid_uname.c
tar/test/test_option_uuencode.c
tar/test/test_option_xz.c
tar/test/test_option_z.c
tar/test/test_stdio.c
tar/test/test_version.c

index a8921ebcbae86f72af41d7f33c7d9a30944f47a9..572c45e3cbb72d9a7ea32611a0d81c568131c9c1 100644 (file)
@@ -44,6 +44,7 @@ DEFINE_TEST(test_leading_slash)
        if (assertFileExists("test.err")) {
                errfile = slurpfile(&errfile_size, "test.err");
                assert(strstr(errfile, expected_errmsg) != NULL);
+               free(errfile);
        }
 }
 
index a000621cb8b57d4a1878b3f19c4d66182ff55320..d9eed8777ff2cd3202aee08d12c6b13721e7be2c 100644 (file)
@@ -43,6 +43,7 @@ DEFINE_TEST(test_option_a)
        assert(s > 2);
        failure("The archive should be compressed");
        assertEqualMem(p, "\x1f\x9d", 2);
+       free(p);
 
        /* Test2: archive it with .taZ suffix. */
        assertEqualInt(0,
@@ -53,6 +54,7 @@ DEFINE_TEST(test_option_a)
        assert(s > 2);
        failure("The archive should be compressed");
        assertEqualMem(p, "\x1f\x9d", 2);
+       free(p);
 
        /* Test3: archive it with .tar.Z.uu suffix. */
        assertEqualInt(0,
@@ -63,6 +65,7 @@ DEFINE_TEST(test_option_a)
        assert(s > 12);
        failure("The archive should be uuencoded");
        assertEqualMem(p, "begin 644 -\n", 12);
+       free(p);
 
        /* Test4: archive it with .zip suffix. */
        assertEqualInt(0,
@@ -73,6 +76,7 @@ DEFINE_TEST(test_option_a)
        assert(s > 4);
        failure("The archive should be zipped");
        assertEqualMem(p, "\x50\x4b\x03\x04", 4);
+       free(p);
 
        /* Test5: archive it with .tar.Z suffix and --uuencode option. */
        assertEqualInt(0,
@@ -84,6 +88,7 @@ DEFINE_TEST(test_option_a)
        assert(s > 2);
        failure("The archive should be compressed, ignoring --uuencode option");
        assertEqualMem(p, "\x1f\x9d", 2);
+       free(p);
 
        /* Test6: archive it with .xxx suffix(unknown suffix) and
         * --uuencode option. */
@@ -96,6 +101,7 @@ DEFINE_TEST(test_option_a)
        assert(s > 12);
        failure("The archive should be uuencoded");
        assertEqualMem(p, "begin 644 -\n", 12);
+       free(p);
 
        /* Test7: archive it with .tar.Z suffix using a long-name option. */
        assertEqualInt(0,
@@ -107,4 +113,5 @@ DEFINE_TEST(test_option_a)
        assert(s > 2);
        failure("The archive should be compressed");
        assertEqualMem(p, "\x1f\x9d", 2);
+       free(p);
 }
index 1e7c57175ce140d820dc4500e3ae0febae28ec6b..1d0420430e09fd6cc019827c40246b5d57b11fe2 100644 (file)
@@ -42,6 +42,7 @@ DEFINE_TEST(test_option_b64encode)
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "begin-base64 644", 16);
+       free(p);
 
        /* Archive it with uuencode only. */
        assertEqualInt(0,
@@ -51,4 +52,5 @@ DEFINE_TEST(test_option_b64encode)
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "begin-base64 644", 16);
+       free(p);
 }
index e45da5b914635b548ca0ce62c60699e7b083352a..4e5f51c8f4861688f304aeb08d45a78cf80764f7 100644 (file)
@@ -53,6 +53,7 @@ DEFINE_TEST(test_option_gid_gname)
        /* Should force gid and gname fields in ustar header. */
        assertEqualMem(data + 116, "000021 \0", 8);
        assertEqualMem(data + 297, "foofoofoo\0", 10);
+       free(data);
 
        /* Again with just --gname */
        failure("Error invoking %s c", testprog);
@@ -65,6 +66,8 @@ DEFINE_TEST(test_option_gid_gname)
        /* Gid should be unchanged from original reference. */
        assertEqualMem(data + 116, reference + 116, 8);
        assertEqualMem(data + 297, "foofoofoo\0", 10);
+       free(data);
+       free(reference);
 
        /* Again with --gid  and force gname to empty. */
        failure("Error invoking %s c", testprog);
@@ -77,6 +80,7 @@ DEFINE_TEST(test_option_gid_gname)
        assertEqualMem(data + 116, "000021 \0", 8);
        /* Gname field in ustar header should be empty. */
        assertEqualMem(data + 297, "\0", 1);
+       free(data);
 
        /* TODO: It would be nice to verify that --gid= by itself
         * will look up the associated gname and use that, but
index 5132eeec26ec88f87e41e17d3021508b74ab76df..fbff252421d19da7c5e0c32dbdabc117bed94831 100644 (file)
@@ -45,8 +45,11 @@ DEFINE_TEST(test_option_grzip)
            testprog));
        p = slurpfile(&s, "archive.err");
        p[s] = '\0';
+       free(p);
+
        /* Check that the archive file has an grzip signature. */
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "GRZipII\x00\x02\x04:)", 12);
+       free(p);
 }
index 3202c3b839149e295d74868d95ccd4ddd7f9402c..838234a2aeb5a59e6b7c4ed0fa0badc80a3ac8cc 100644 (file)
@@ -42,15 +42,18 @@ DEFINE_TEST(test_option_j)
        if (r != 0) {
                if (!canBzip2()) {
                        skipping("bzip2 is not supported on this platform");
-                       return;
+                       goto done;
                }
                failure("-j option is broken");
                assertEqualInt(r, 0);
-               return;
+               goto done;
        }
+       free(p);
        assertEmptyFile("archive.err");
        /* Check that the archive file has a bzip2 signature. */
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "BZh9", 4);
+done:
+       free(p);
 }
index d3486a3a27b76e7b19e7be536a57eb49d8df5ab6..11e9827e0405f7f33515b6bfb56ffd287d5bbf40 100644 (file)
@@ -45,8 +45,10 @@ DEFINE_TEST(test_option_lrzip)
            testprog));
        p = slurpfile(&s, "archive.err");
        p[s] = '\0';
+       free(p);
        /* Check that the archive file has an lzma signature. */
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "LRZI\x00", 5);
+       free(p);
 }
index 5dc945216300b63c81b245914ed24c16e9acb59b..1694e3ced37dc7aeb0a2d19a7f441385ade2800d 100644 (file)
@@ -43,7 +43,7 @@ DEFINE_TEST(test_option_lz4)
                if (strstr(p, "Unsupported compression") != NULL) {
                        skipping("This version of bsdtar was compiled "
                            "without lz4 support");
-                       return;
+                       goto done;
                }
                /* POSIX permits different handling of the spawnp
                 * system call used to launch the subsidiary
@@ -52,7 +52,7 @@ DEFINE_TEST(test_option_lz4)
                if (strstr(p, "Can't launch") != NULL && !canLz4()) {
                        skipping("This version of bsdtar uses an external lz4 program "
                            "but no such program is available on this system.");
-                       return;
+                       goto done;
                }
                /* Some systems successfully spawn the new process,
                 * but fail to exec a program within that process.
@@ -61,14 +61,18 @@ DEFINE_TEST(test_option_lz4)
                if (strstr(p, "Can't write") != NULL && !canLz4()) {
                        skipping("This version of bsdtar uses an external lz4 program "
                            "but no such program is available on this system.");
-                       return;
+                       goto done;
                }
                failure("--lz4 option is broken: %s", p);
                assertEqualInt(r, 0);
-               return;
+               goto done;
        }
+       free(p);
        /* Check that the archive file has an lz4 signature. */
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "\x04\x22\x4d\x18", 4);
+
+done:
+       free(p);
 }
index a845666e34f9aa15c620131cc4dbced91803d97e..a618ff8a34036acf734cf526431dd7677fdb0c25 100644 (file)
@@ -48,10 +48,13 @@ DEFINE_TEST(test_option_lzma)
                }
                failure("--lzma option is broken");
                assertEqualInt(r, 0);
-               return;
+               goto done;
        }
+       free(p);
        /* Check that the archive file has an lzma signature. */
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "\x5d\00\00", 3);
+done:
+       free(p);
 }
index 1145499a460541701a41a66297bbf3d4708e55cb..20ef06c5c8a0e752d886ee9ef3e1c56979af2ce5 100644 (file)
@@ -42,14 +42,17 @@ DEFINE_TEST(test_option_lzop)
        if (r != 0) {
                if (!canLzop()) {
                        skipping("lzop is not supported on this platform");
-                       return;
+                       goto done;
                }
                failure("--lzop option is broken");
                assertEqualInt(r, 0);
-               return;
+               goto done;
        }
+       free(p);
        /* Check that the archive file has an lzma signature. */
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "\x89\x4c\x5a\x4f\x00\x0d\x0a\x1a\x0a", 9);
+done:
+       free(p);
 }
index 77876857ea7766611a375498ca58f9fecda1d4aa..287e80939adf302c9d53fdfa00d95851ec7b5b97 100644 (file)
@@ -36,6 +36,10 @@ DEFINE_TEST(test_option_r)
        size_t s, buff_size_rounded;
        int r, i;
 
+       buff = NULL;
+       p0 = NULL;
+       p1 = NULL;
+
        /* Create an archive with one file. */
        assertMakeFile("f1", 0644, "abc");
        r = systemf("%s cf archive.tar --format=ustar f1 >step1.out 2>step1.err", testprog);
@@ -47,11 +51,9 @@ DEFINE_TEST(test_option_r)
        /* Do some basic validation of the constructed archive. */
        p0 = slurpfile(&s, "archive.tar");
        if (!assert(p0 != NULL))
-               return;
-       if (!assert(s >= 2048)) {
-               free(p0);
-               return;
-       }
+               goto done;
+       if (!assert(s >= 2048))
+               goto done;
        assertEqualMem(p0 + 0, "f1", 3);
        assertEqualMem(p0 + 512, "abc", 3);
        assertEqualMem(p0 + 1024, "\0\0\0\0\0\0\0\0", 8);
@@ -60,10 +62,8 @@ DEFINE_TEST(test_option_r)
        /* Edit that file with a lot more data and update the archive with a new copy. */
        buff = malloc(buff_size);
        assert(buff != NULL);
-       if (buff == NULL) {
-               free(p0);
-               return;
-       }
+       if (buff == NULL)
+               goto done;
 
        for (i = 0; i < (int)buff_size; ++i)
                buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
@@ -77,10 +77,8 @@ DEFINE_TEST(test_option_r)
 
        /* The constructed archive should just have the new entry appended. */
        p1 = slurpfile(&s, "archive.tar");
-       if (!assert(p1 != NULL)) {
-               free(p0);
-               return;
-       }
+       if (!assert(p1 != NULL))
+               goto done;
        buff_size_rounded = ((buff_size + 511) / 512) * 512;
        assert(s >= 2560 + buff_size_rounded);
        /* Verify first entry is unchanged. */
@@ -105,10 +103,8 @@ DEFINE_TEST(test_option_r)
 
        /* Validate the constructed archive. */
        p1 = slurpfile(&s, "archive.tar");
-       if (!assert(p1 != NULL)) {
-               free(p0);
-               return;
-       }
+       if (!assert(p1 != NULL))
+               goto done;
        assert(s >= 3584 + buff_size_rounded);
        /* Verify first two entries are unchanged. */
        assertEqualMem(p0, p1, 1536 + buff_size_rounded);
@@ -118,7 +114,6 @@ DEFINE_TEST(test_option_r)
        /* Verify end-of-archive marker. */
        assertEqualMem(p1 + 2560 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
        assertEqualMem(p1 + 3072 + buff_size_rounded, "\0\0\0\0\0\0\0\0", 8);
-       free(p0);
        free(p1);
 
        /* Unpack everything */
@@ -132,4 +127,7 @@ DEFINE_TEST(test_option_r)
 
        /* Verify that the second copy of f1 overwrote the first. */
        assertFileContents(buff, (int)strlen(buff), "f1");
+done:
+       free(buff);
+       free(p0);
 }
index 83ea5f1ecbbf40fe9f95678468b51d0c376b7939..0a8a9bb27a8c7281c75e874aa94227bba93b788c 100644 (file)
@@ -53,6 +53,7 @@ DEFINE_TEST(test_option_uid_uname)
        /* Should force uid and uname fields in ustar header. */
        assertEqualMem(data + 108, "000021 \0", 8);
        assertEqualMem(data + 265, "foofoofoo\0", 10);
+       free(data);
 
        /* Again with just --uid */
        failure("Error invoking %s c", testprog);
@@ -65,6 +66,7 @@ DEFINE_TEST(test_option_uid_uname)
        assertEqualMem(data + 108, "000021 \0", 8);
        /* Uname field in ustar header should be empty. */
        assertEqualMem(data + 265, "\0", 1);
+       free(data);
 
        /* Again with just --uname */
        failure("Error invoking %s c", testprog);
@@ -77,4 +79,7 @@ DEFINE_TEST(test_option_uid_uname)
        /* Uid should be unchanged from original reference. */
        assertEqualMem(data + 108, reference + 108, 8);
        assertEqualMem(data + 265, "foofoofoo\0", 10);
+       free(data);
+
+       free(reference);
 }
index cdc6babd5192f15ef13bd56e0cd07bd3d39aa1af..a28a8e3407a3405cd3c8f634bb5230f1e671c964 100644 (file)
@@ -42,6 +42,7 @@ DEFINE_TEST(test_option_uuencode)
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "begin 644", 9);
+       free(p);
 
        /* Archive it with uuencode only. */
        assertEqualInt(0,
@@ -51,4 +52,5 @@ DEFINE_TEST(test_option_uuencode)
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "begin 644", 9);
+       free(p);
 }
index 7387a604e072e0fceea88d1d634686a2c3ed520f..91da0730ac948bc887b86859d5e747e1348d8456 100644 (file)
@@ -44,14 +44,17 @@ DEFINE_TEST(test_option_xz)
                if (strstr(p, "Unsupported compression") != NULL) {
                        skipping("This version of bsdtar was compiled "
                            "without xz support");
-                       return;
+                       goto done;
                }
                failure("--xz option is broken");
                assertEqualInt(r, 0);
-               return;
+               goto done;
        }
+       free(p);
        /* Check that the archive file has an xz signature. */
        p = slurpfile(&s, "archive.out");
        assert(s > 2);
        assertEqualMem(p, "\xFD\x37\x7A\x58\x5A\x00", 6);
+done:
+       free(p);
 }
index 1f952abe3aff06430af2a0865c108150b0c0ca45..59744999fc5cebc82f7025d2510db6397c44f10d 100644 (file)
@@ -42,14 +42,17 @@ DEFINE_TEST(test_option_z)
        if (r != 0) {
                if (!canGzip()) {
                        skipping("gzip is not supported on this platform");
-                       return;
+                       goto done;
                }
                failure("-z option is broken");
                assertEqualInt(r, 0);
-               return;
+               goto done;
        }
+       free(p);
        /* Check that the archive file has a gzip signature. */
        p = slurpfile(&s, "archive.out");
        assert(s > 4);
        assertEqualMem(p, "\x1f\x8b\x08\x00", 4);
+done:
+       free(p);
 }
index bf5dd77443b73ea944294307a0d03bf75acac078..d6650a5eb20abb6c8ad948f6f08220b3c19f308e 100644 (file)
@@ -116,6 +116,7 @@ DEFINE_TEST(test_stdio)
        assertEqualInt((int)s, 3);
        assertEqualMem(p, "abc", 3);
        /* TODO: Verify xvf.err */
+       free(p);
 
        /* 'xvf -' should generate list on stderr, empty stdout. */
        r = systemf("%s xvf - < archive >xvf-.out 2>xvf-.err", testprog);
index 04d9e6c7ca9285291378fbc0df63baa92228d5a9..0c904d705c22921757221195def93c8e9ccd9734 100644 (file)
@@ -53,7 +53,7 @@ DEFINE_TEST(test_version)
        assert(s > 6);
        failure("Version must start with 'bsdtar': ``%s''", p);
        if (!assertEqualMem(q, "bsdtar ", 7))
-               return;
+               goto done;
        q += 7; s -= 7;
        /* Version number is a series of digits and periods. */
        while (s > 0 && (*q == '.' || (*q >= '0' && *q <= '9'))) {
@@ -98,5 +98,6 @@ DEFINE_TEST(test_version)
        failure("Version output must end with \\n or \\r\\n");
        if (*q == '\r') { ++q; --s; }
        assertEqualMem(q, "\n", 1);
+done:
        free(p);
 }