]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Fix bugs in test programs which Clang Static Analyzer pointed out.
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Wed, 14 Nov 2012 07:10:40 +0000 (16:10 +0900)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Wed, 14 Nov 2012 07:10:40 +0000 (16:10 +0900)
26 files changed:
cpio/test/main.c
cpio/test/test_basic.c
cpio/test/test_format_newc.c
libarchive/test/main.c
libarchive/test/test_acl_freebsd_posix1e.c
libarchive/test/test_acl_nfs4.c
libarchive/test/test_acl_posix1e.c
libarchive/test/test_archive_cmdline.c
libarchive/test/test_archive_read_multiple_data_objects.c
libarchive/test/test_entry.c
libarchive/test/test_fuzz.c
libarchive/test/test_read_disk.c
libarchive/test/test_read_format_cpio_afio.c
libarchive/test/test_read_truncated_filter.c
libarchive/test/test_write_disk_sparse.c
libarchive/test/test_write_filter_bzip2.c
libarchive/test/test_write_filter_gzip.c
libarchive/test/test_write_filter_gzip_timestamp.c
libarchive/test/test_write_filter_lzip.c
libarchive/test/test_write_filter_lzma.c
libarchive/test/test_write_filter_xz.c
libarchive/test/test_write_format_iso9660_empty.c
libarchive/test/test_write_format_iso9660_filename.c
libarchive/test/test_write_format_iso9660_zisofs.c
tar/test/main.c
tar/test/test_option_r.c

index 5ea9c0db8aff0cbcc9b12b9d5a238c84e734a79b..a571eed0574c224e413212f5333abfca4c3a549a 100644 (file)
@@ -748,6 +748,8 @@ assertion_equal_mem(const char *file, int line,
        assertion_count(file, line);
        if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
                return (1);
+       if (v1 == NULL || v2 == NULL)
+               return (0);
 
        failure_start(file, line, "%s != %s", e1, e2);
        logprintf("      size %s = %d\n", ld, (int)l);
@@ -1011,8 +1013,8 @@ assertion_file_contains_lines_any_order(const char *file, int line,
        char *buff;
        size_t buff_size;
        size_t expected_count, actual_count, i, j;
-       char **expected;
-       char *p, **actual;
+       char **expected = NULL;
+       char *p, **actual = NULL;
        char c;
        int expected_failure = 0, actual_failure = 0;
 
@@ -1025,14 +1027,21 @@ assertion_file_contains_lines_any_order(const char *file, int line,
                return (0);
        }
 
-       /* Make a copy of the provided lines and count up the expected file size. */
-       expected_count = 0;
+       /* Make a copy of the provided lines and count up the expected
+        * file size. */
        for (i = 0; lines[i] != NULL; ++i) {
        }
        expected_count = i;
-       expected = malloc(sizeof(char *) * expected_count);
-       for (i = 0; lines[i] != NULL; ++i) {
-               expected[i] = strdup(lines[i]);
+       if (expected_count) {
+               expected = malloc(sizeof(char *) * expected_count);
+               if (expected == NULL) {
+                       failure_start(pathname, line, "Can't allocate memory");
+                       failure_finish(NULL);
+                       return (0);
+               }
+               for (i = 0; lines[i] != NULL; ++i) {
+                       expected[i] = strdup(lines[i]);
+               }
        }
 
        /* Break the file into lines */
@@ -1044,11 +1053,19 @@ assertion_file_contains_lines_any_order(const char *file, int line,
                        ++actual_count;
                c = *p;
        }
-       actual = malloc(sizeof(char *) * actual_count);
-       for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
-               if (*p != '\0') {
-                       actual[j] = p;
-                       ++j;
+       if (actual_count) {
+               actual = calloc(sizeof(char *), actual_count);
+               if (actual == NULL) {
+                       failure_start(pathname, line, "Can't allocate memory");
+                       failure_finish(NULL);
+                       free(expected);
+                       return (0);
+               }
+               for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
+                       if (*p != '\0') {
+                               actual[j] = p;
+                               ++j;
+                       }
                }
        }
 
@@ -2762,6 +2779,7 @@ main(int argc, char **argv)
                        if (test_num < 0) {
                                printf("*** INVALID Test %s\n", *argv);
                                free(refdir_alloc);
+                               free(testprogdir);
                                usage(progname);
                                return (1);
                        }
index c40813e9a26ff596a1df86c69b40093c5607190b..7213062e8d2071089942d7144623424867cc6245 100644 (file)
@@ -148,7 +148,7 @@ DEFINE_TEST(test_basic)
                strncat(result,
                    "bsdcpio: file: large inode number truncated: "
                    "Numerical result out of range\n",
-                   sizeof(result) - strlen(result));
+                   sizeof(result) - strlen(result) -1);
 
        /* hardlink to above file. */
        assertMakeHardlink("linkfile", "file");
@@ -157,7 +157,7 @@ DEFINE_TEST(test_basic)
                strncat(result,
                    "bsdcpio: linkfile: large inode number truncated: "
                    "Numerical result out of range\n",
-                   sizeof(result) - strlen(result));
+                   sizeof(result) - strlen(result) -1);
 
        /* Symlink to above file. */
        if (canSymlink()) {
@@ -167,7 +167,7 @@ DEFINE_TEST(test_basic)
                        strncat(result,
                            "bsdcpio: symlink: large inode number truncated: "
                                "Numerical result out of range\n",
-                           sizeof(result) - strlen(result));
+                           sizeof(result) - strlen(result) -1);
        }
 
        /* Another file with different permissions. */
@@ -177,7 +177,7 @@ DEFINE_TEST(test_basic)
                strncat(result,
                    "bsdcpio: file2: large inode number truncated: "
                    "Numerical result out of range\n",
-                   sizeof(result) - strlen(result));
+                   sizeof(result) - strlen(result) -1);
 
        /* Directory. */
        assertMakeDir("dir", 0775);
@@ -186,8 +186,8 @@ DEFINE_TEST(test_basic)
                strncat(result,
                    "bsdcpio: dir: large inode number truncated: "
                    "Numerical result out of range\n",
-                   sizeof(result) - strlen(result));
-       strncat(result, "2 blocks\n", sizeof(result) - strlen(result));
+                   sizeof(result) - strlen(result) -1);
+       strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
 
        /* All done. */
        fclose(filelist);
index ced62a639e7a197bb992bdae1efe9ca64b3cd81d..d2daa46ab71c6c7d0db12a64f24ff2800ddb4f24 100644 (file)
@@ -157,9 +157,9 @@ DEFINE_TEST(test_format_newc)
 
        /* Verify that nothing went to stderr. */
        if (canSymlink()) {
-               strncat(result, "2 blocks\n", sizeof(result) - strlen(result));
+               strncat(result, "2 blocks\n", sizeof(result) - strlen(result) -1);
        } else {
-               strncat(result, "1 block\n", sizeof(result) - strlen(result));
+               strncat(result, "1 block\n", sizeof(result) - strlen(result) -1);
        }
        assertTextFileContents(result, "newc.err");
 
index d442da0678ccf3943e056c39c4741bc37f64f5b0..68e965adcfc54fbf980244ac1bc86b950e420b89 100644 (file)
@@ -746,6 +746,8 @@ assertion_equal_mem(const char *file, int line,
        assertion_count(file, line);
        if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
                return (1);
+       if (v1 == NULL || v2 == NULL)
+               return (0);
 
        failure_start(file, line, "%s != %s", e1, e2);
        logprintf("      size %s = %d\n", ld, (int)l);
@@ -1009,8 +1011,8 @@ assertion_file_contains_lines_any_order(const char *file, int line,
        char *buff;
        size_t buff_size;
        size_t expected_count, actual_count, i, j;
-       char **expected;
-       char *p, **actual;
+       char **expected = NULL;
+       char *p, **actual = NULL;
        char c;
        int expected_failure = 0, actual_failure = 0;
 
@@ -1023,14 +1025,21 @@ assertion_file_contains_lines_any_order(const char *file, int line,
                return (0);
        }
 
-       /* Make a copy of the provided lines and count up the expected file size. */
-       expected_count = 0;
+       /* Make a copy of the provided lines and count up the expected
+        * file size. */
        for (i = 0; lines[i] != NULL; ++i) {
        }
        expected_count = i;
-       expected = malloc(sizeof(char *) * expected_count);
-       for (i = 0; lines[i] != NULL; ++i) {
-               expected[i] = strdup(lines[i]);
+       if (expected_count) {
+               expected = malloc(sizeof(char *) * expected_count);
+               if (expected == NULL) {
+                       failure_start(pathname, line, "Can't allocate memory");
+                       failure_finish(NULL);
+                       return (0);
+               }
+               for (i = 0; lines[i] != NULL; ++i) {
+                       expected[i] = strdup(lines[i]);
+               }
        }
 
        /* Break the file into lines */
@@ -1042,11 +1051,20 @@ assertion_file_contains_lines_any_order(const char *file, int line,
                        ++actual_count;
                c = *p;
        }
-       actual = malloc(sizeof(char *) * actual_count);
-       for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
-               if (*p != '\0') {
-                       actual[j] = p;
-                       ++j;
+       if (actual_count) {
+               actual = calloc(sizeof(char *), actual_count);
+               if (actual == NULL) {
+                       failure_start(pathname, line, "Can't allocate memory");
+                       failure_finish(NULL);
+                       free(expected);
+                       return (0);
+               }
+               for (j = 0, p = buff; p < buff + buff_size;
+                   p += 1 + strlen(p)) {
+                       if (*p != '\0') {
+                               actual[j] = p;
+                               ++j;
+                       }
                }
        }
 
@@ -2768,6 +2786,7 @@ main(int argc, char **argv)
                        if (test_num < 0) {
                                printf("*** INVALID Test %s\n", *argv);
                                free(refdir_alloc);
+                               free(testprogdir);
                                usage(progname);
                                return (1);
                        }
index d2ae6d476936f8fbae32b04bdb3e2a93c918a473..36f9499f4c73d54077a59f38734e85a17641cc57 100644 (file)
@@ -139,9 +139,14 @@ compare_acls(acl_t acl, struct myacl_t *myacls)
        /* Count ACL entries in myacls array and allocate an indirect array. */
        for (n = 0; myacls[n].name != NULL; ++n)
                continue;
-       marker = malloc(sizeof(marker[0]) * n);
-       for (i = 0; i < n; i++)
-               marker[i] = i;
+       if (n) {
+               marker = malloc(sizeof(marker[0]) * n);
+               if (marker == NULL)
+                       return;
+               for (i = 0; i < n; i++)
+                       marker[i] = i;
+       } else
+               marker = NULL;
 
        /*
         * Iterate over acls in system acl object, try to match each
index ebf9a4656ba5252916f1a5918901255bd811c435..c8f59371a52b5d09a60e63e7f09af387a851de85 100644 (file)
@@ -174,8 +174,11 @@ set_acls(struct archive_entry *ae, struct acl_t *acls, int n)
 }
 
 static int
-acl_match(struct acl_t *acl, int type, int permset, int tag, int qual, const char *name)
+acl_match(struct acl_t *acl, int type, int permset, int tag, int qual,
+    const char *name)
 {
+       if (acl == NULL)
+               return (0);
        if (type != acl->type)
                return (0);
        if (permset != acl->permset)
@@ -193,10 +196,12 @@ acl_match(struct acl_t *acl, int type, int permset, int tag, int qual, const cha
        if (name == NULL) {
                if (acl->name == NULL || acl->name[0] == '\0')
                        return (1);
+               return (0);
        }
        if (acl->name == NULL) {
                if (name[0] == '\0')
                        return (1);
+               return (0);
        }
        return (0 == strcmp(name, acl->name));
 }
index 2055682bb2abb6655b5670b7fe3c073051ae7597..9984d44188ee5f7efa23c68c93dde546c5bf0b8d 100644 (file)
@@ -137,10 +137,12 @@ acl_match(struct acl_t *acl, int type, int permset, int tag, int qual, const cha
        if (name == NULL) {
                if (acl->name == NULL || acl->name[0] == '\0')
                        return (1);
+               return (0);
        }
        if (acl->name == NULL) {
                if (name[0] == '\0')
                        return (1);
+               return (0);
        }
        return (0 == strcmp(name, acl->name));
 }
index 963454d48d770c6a8896dc9429c65107594c81a9..a411821356c917ecd8b753d0796009044d981266 100644 (file)
@@ -35,6 +35,8 @@ DEFINE_TEST(test_archive_cmdline)
 
        /* Command name only. */
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "gzip"));
        assertEqualInt(1, cl->argc);
        assertEqualString("gzip", cl->path);
@@ -42,6 +44,8 @@ DEFINE_TEST(test_archive_cmdline)
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
 
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "gzip "));
        assertEqualInt(1, cl->argc);
        failure("path should not include a space character");
@@ -51,6 +55,8 @@ DEFINE_TEST(test_archive_cmdline)
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_free(cl));
 
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl,
            "/usr/bin/gzip "));
        assertEqualInt(1, cl->argc);
@@ -62,6 +68,8 @@ DEFINE_TEST(test_archive_cmdline)
 
        /* A command line includes space characer. */
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "\"gzip \""));
        assertEqualInt(1, cl->argc);
        failure("path should include a space character");
@@ -72,6 +80,8 @@ DEFINE_TEST(test_archive_cmdline)
 
        /* A command line includes space characer: pattern 2.*/
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "\"gzip \"x"));
        assertEqualInt(1, cl->argc);
        failure("path should include a space character");
@@ -82,6 +92,8 @@ DEFINE_TEST(test_archive_cmdline)
 
        /* A command line includes space characer: pattern 3.*/
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl,
            "\"gzip \"x\" s \""));
        assertEqualInt(1, cl->argc);
@@ -93,6 +105,8 @@ DEFINE_TEST(test_archive_cmdline)
 
        /* A command line includes space characer: pattern 4.*/
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl,
            "\"gzip\\\" \""));
        assertEqualInt(1, cl->argc);
@@ -104,6 +118,8 @@ DEFINE_TEST(test_archive_cmdline)
 
        /* A command name with a argument. */
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "gzip -d"));
        assertEqualInt(2, cl->argc);
        assertEqualString("gzip", cl->path);
@@ -113,6 +129,8 @@ DEFINE_TEST(test_archive_cmdline)
 
        /* A command name with two arguments. */
        assert((cl = __archive_cmdline_allocate()) != NULL);
+       if (cl == NULL)
+               return;
        assertEqualInt(ARCHIVE_OK, __archive_cmdline_parse(cl, "gzip -d -q"));
        assertEqualInt(3, cl->argc);
        assertEqualString("gzip", cl->path);
index 391bf673f28096d33d2bdf3c9c8d0377b08a9f84..9962cf7fdf33f334f2c416882d5633c095d9186f 100644 (file)
@@ -234,6 +234,8 @@ static int
 file_close(struct archive *a, void *data)
 {
   struct mydata *mydata = (struct mydata *)data;
+  if (mydata == NULL)
+    return (ARCHIVE_FATAL);
   file_switch(a, mydata, NULL);
   free(mydata->filename);
   free(mydata);
@@ -280,8 +282,17 @@ test_customized_multiple_data_objects(void)
   for (i = 0; filename != NULL;)
   {
     assert((mydata = (struct mydata *)calloc(1, sizeof(*mydata))) != NULL);
+    if (mydata == NULL) {
+      assertEqualInt(ARCHIVE_OK, archive_read_free(a));
+      return;
+    }
     assert((mydata->filename =
       (char *)calloc(1, strlen(filename) + 1)) != NULL);
+    if (mydata->filename == NULL) {
+      free(mydata);
+      assertEqualInt(ARCHIVE_OK, archive_read_free(a));
+      return;
+    }
     strcpy(mydata->filename, filename);
     mydata->fd = -1;
     filename = reffiles[++i];
index 8249c7319eff8e10b91e5f08a28d88d2b9a745bd..0ccc9e8fb355f18ccd82135f77107a595bde1c28 100644 (file)
@@ -696,6 +696,8 @@ DEFINE_TEST(test_entry)
        archive_entry_set_uid(e, 23);
        /* Retrieve a stat structure. */
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        /* Check that the values match. */
        assertEqualInt(pst->st_atime, 456789);
        assertEqualInt(pst->st_ctime, 345678);
@@ -717,33 +719,53 @@ DEFINE_TEST(test_entry)
        /* Changing any one value should update struct stat. */
        archive_entry_set_atime(e, 456788, 0);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_atime, 456788);
        archive_entry_set_ctime(e, 345677, 431);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_ctime, 345677);
        archive_entry_set_dev(e, 122);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_dev, 122);
        archive_entry_set_gid(e, 33);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_gid, 33);
        archive_entry_set_ino(e, 233);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_ino, 233);
        archive_entry_set_mode(e, 012344);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_mode, 012344);
        archive_entry_set_mtime(e, 234566, 542);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_mtime, 234566);
        archive_entry_set_nlink(e, 344);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_nlink, 344);
        archive_entry_set_size(e, 123456788);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_size, 123456788);
        archive_entry_set_uid(e, 22);
        assert((pst = archive_entry_stat(e)) != NULL);
+       if (pst == NULL)
+               return;
        assertEqualInt(pst->st_uid, 22);
        /* We don't need to check high-res fields here. */
 
index a03f7517a83202be73852483c345c9abdc56f539..2b95cc2093a3e1d82f09fb5a79b2d636b1a9084a 100644 (file)
@@ -256,7 +256,7 @@ DEFINE_TEST(test_fuzz)
                struct archive_entry *ae;
                struct archive *a;
                char *rawimage = NULL, *image = NULL, *tmp = NULL;
-               size_t size, oldsize = 0;
+               size_t size = 0, oldsize = 0;
                int i, q;
 
                extract_reference_files(filesets[n].names);
@@ -311,8 +311,12 @@ DEFINE_TEST(test_fuzz)
                                        continue;
                        }
                }
+               if (size == 0)
+                       continue;
                image = malloc(size);
                assert(image != NULL);
+               if (image == NULL)
+                       return;
                srand((unsigned)time(NULL));
 
                for (i = 0; i < 100; ++i) {
index b69e55cfa292a1af751ccc4706b7e4e42a2ed617..bd36c396a1afef12f1ed56c42c6f76dc7a7170cf 100644 (file)
@@ -126,7 +126,8 @@ DEFINE_TEST(test_read_disk)
 
                /* Get the group name for group 0 and see if it makes sense. */
                p = archive_read_disk_gname(a, 0);
-               if (assert(p != NULL)) {
+               assert(p != NULL);
+               if (p != NULL) {
                        i = 0;
                        while (i < sizeof(zero_groups)/sizeof(zero_groups[0])) {
                                if (strcmp(zero_groups[i], p) == 0)
index 3d5814bce7e53b20f9199b4d975f5b7738dac5bd..db6003f95a603b7f6e941cb27e32da043032b431 100644 (file)
@@ -84,7 +84,8 @@ DEFINE_TEST(test_read_format_cpio_afio)
 
        /* The default block size of afio is 5120. we simulate it */
        size = (sizeof(archive) + 5120 -1 / 5120) * 5120;
-       if (!assert((p = malloc(size)) != NULL))
+       assert((p = malloc(size)) != NULL);
+       if (p == NULL)
                return;
        memset(p, 0, size);
        memcpy(p, archive, sizeof(archive));
index 2fe7f6553267cdefd92fdfd4e0340df898e2b9a5..bf82546d48b718df659927800b68001ed3518393 100644 (file)
@@ -45,9 +45,15 @@ test_truncation(const char *compression,
 
        buffsize = 2000000;
        assert(NULL != (buff = (char *)malloc(buffsize)));
+       if (buff == NULL)
+               return;
 
        datasize = 10000;
        assert(NULL != (data = (char *)malloc(datasize)));
+       if (data == NULL) {
+               free(buff);
+               return;
+       }
        memset(data, 0, datasize);
 
        /*
@@ -62,6 +68,8 @@ test_truncation(const char *compression,
                skipping("%s writing not supported on this platform",
                    compression);
                assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+               free(buff);
+               free(data);
                return;
        }
        use_prog = (r == ARCHIVE_WARN && can_prog);
index d25b6c00637d0155c9f1e0dac10c4873925c8fbf..36ecf322087d04db18015bda05b598fa6d1c4895 100644 (file)
@@ -44,6 +44,8 @@ verify_write_data(struct archive *a, int sparse)
 
        buff = malloc(buff_size);
        assert(buff != NULL);
+       if (buff == NULL)
+               return;
 
        ae = archive_entry_new();
        assert(ae != NULL);
@@ -79,8 +81,11 @@ verify_write_data(struct archive *a, int sparse)
        assert(0 == stat(archive_entry_pathname(ae), &st));
         assertEqualInt(st.st_size, 8 * buff_size);
        f = fopen(archive_entry_pathname(ae), "rb");
-       if (!assert(f != NULL))
+       assert(f != NULL);
+       if (f == NULL) {
+               free(buff);
                return;
+       }
 
        /* Check first block. */
        assertEqualInt(buff_size, fread(buff, 1, buff_size, f));
@@ -136,6 +141,8 @@ verify_write_data_block(struct archive *a, int sparse)
 
        buff = malloc(buff_size);
        assert(buff != NULL);
+       if (buff == NULL)
+               return;
 
        ae = archive_entry_new();
        assert(ae != NULL);
@@ -175,8 +182,11 @@ verify_write_data_block(struct archive *a, int sparse)
        assert(0 == stat(archive_entry_pathname(ae), &st));
         assertEqualInt(st.st_size, 8 * buff_size);
        f = fopen(archive_entry_pathname(ae), "rb");
-       if (!assert(f != NULL))
+       assert(f != NULL);
+       if (f == NULL) {
+               free(buff);
                return;
+       }
 
        /* Check 100-byte gap at beginning */
        assertEqualInt(100, fread(buff, 1, 100, f));
index 74150c8fb6eba4f1fb191cd526372a7f96a439d1..4f32d28cbed4a1bde6688ded57e4ac4be941907f 100644 (file)
@@ -45,9 +45,15 @@ DEFINE_TEST(test_write_filter_bzip2)
 
        buffsize = 2000000;
        assert(NULL != (buff = (char *)malloc(buffsize)));
+       if (buff == NULL)
+               return;
 
        datasize = 10000;
        assert(NULL != (data = (char *)malloc(datasize)));
+       if (data == NULL) {
+               free(buff);
+               return;
+       }
        memset(data, 0, datasize);
 
        /*
@@ -60,6 +66,8 @@ DEFINE_TEST(test_write_filter_bzip2)
        if (r != ARCHIVE_OK && !use_prog) {
                skipping("bzip2 writing not supported on this platform");
                assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+               free(buff);
+               free(data);
                return;
        }
 
index 118da2e466b096e27458852e57f98b0043611cb2..7bbdb1232a970d69f3cd7e654d5289127a31d195 100644 (file)
@@ -45,9 +45,15 @@ DEFINE_TEST(test_write_filter_gzip)
 
        buffsize = 2000000;
        assert(NULL != (buff = (char *)malloc(buffsize)));
+       if (buff == NULL)
+               return;
 
        datasize = 10000;
        assert(NULL != (data = (char *)malloc(datasize)));
+       if (data == NULL) {
+               free(buff);
+               return;
+       }
        memset(data, 0, datasize);
 
        /*
@@ -62,6 +68,8 @@ DEFINE_TEST(test_write_filter_gzip)
                else {
                        skipping("gzip writing not supported on this platform");
                        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+                       free(buff);
+                       free(data);
                        return;
                }
        }
index 5dc020d8fd8b87baa69447a7f2f288c5200211db..23b11dba79a620c41f3b4175ea16a361e6837bed 100644 (file)
@@ -39,9 +39,15 @@ DEFINE_TEST(test_write_filter_gzip_timestamp)
 
        buffsize = 10000;
        assert(NULL != (buff = (char *)malloc(buffsize)));
+       if (buff == NULL)
+               return;
 
        datasize = 10000;
        assert(NULL != (data = (char *)malloc(datasize)));
+       if (data == NULL) {
+               free(buff);
+               return;
+       }
        memset(data, 0, datasize);
 
        /* Test1: set "gzip:timestamp" option. */
@@ -54,6 +60,8 @@ DEFINE_TEST(test_write_filter_gzip_timestamp)
                else {
                        skipping("gzip writing not supported on this platform");
                        assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+                       free(buff);
+                       free(data);
                        return;
                }
        }
index 508ecd00cdd8e2b6d865f1f54f8611c0d1d2f36e..145a3084d3de36b4e84ea936bbcb08c969100d9f 100644 (file)
@@ -45,9 +45,15 @@ DEFINE_TEST(test_write_filter_lzip)
 
        buffsize = 2000000;
        assert(NULL != (buff = (char *)malloc(buffsize)));
+       if (buff == NULL)
+               return;
 
        datasize = 10000;
        assert(NULL != (data = (char *)malloc(datasize)));
+       if (data == NULL) {
+               free(buff);
+               return;
+       }
        memset(data, 0, datasize);
 
        /*
@@ -59,6 +65,8 @@ DEFINE_TEST(test_write_filter_lzip)
        if (r == ARCHIVE_FATAL) {
                skipping("lzip writing not supported on this platform");
                assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+               free(buff);
+               free(data);
                return;
        }
        assertEqualIntA(a, ARCHIVE_OK,
index 8f4324576536cbdef89f4d29ea911cfc34b0ed86..68e489832504d4140a2d784bf48f6b7058044b47 100644 (file)
@@ -44,9 +44,15 @@ DEFINE_TEST(test_write_filter_lzma)
 
        buffsize = 2000000;
        assert(NULL != (buff = (char *)malloc(buffsize)));
+       if (buff == NULL)
+               return;
 
        datasize = 10000;
        assert(NULL != (data = (char *)malloc(datasize)));
+       if (data == NULL) {
+               free(buff);
+               return;
+       }
        memset(data, 0, datasize);
 
        /*
@@ -58,6 +64,8 @@ DEFINE_TEST(test_write_filter_lzma)
        if (r == ARCHIVE_FATAL) {
                skipping("lzma writing not supported on this platform");
                assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+               free(buff);
+               free(data);
                return;
        }
        assertEqualIntA(a, ARCHIVE_OK,
index ae148cc44f81b6f1e7e85d7859bd193a5dd5048d..bf1265c65b6f376483c678d91af0d17e89e33b56 100644 (file)
@@ -45,9 +45,15 @@ DEFINE_TEST(test_write_filter_xz)
 
        buffsize = 2000000;
        assert(NULL != (buff = (char *)malloc(buffsize)));
+       if (buff == NULL)
+               return;
 
        datasize = 10000;
        assert(NULL != (data = (char *)malloc(datasize)));
+       if (data == NULL) {
+               free(buff);
+               return;
+       }
        memset(data, 0, datasize);
 
        /*
@@ -59,6 +65,8 @@ DEFINE_TEST(test_write_filter_xz)
        if (r == ARCHIVE_FATAL) {
                skipping("xz writing not supported on this platform");
                assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+               free(buff);
+               free(data);
                return;
        }
        assertEqualIntA(a, ARCHIVE_OK,
index c731d6ef20d8e0e65f50e37f15e22c4d2b0bb7f9..55c6c3c7e5675746350f26e8140260479df682df 100644 (file)
@@ -58,6 +58,8 @@ DEFINE_TEST(test_write_format_iso9660_empty)
 
        buff = malloc(buffsize);
        assert(buff != NULL);
+       if (buff == NULL)
+               return;
 
        /* ISO9660 format: Create a new archive in memory. */
        assert((a = archive_write_new()) != NULL);
index 7b57ed5f0b91ae23b88a988f3af0a4e417f06b7a..713883bdc8cbb25cd425d58783c4022b7487544a 100644 (file)
@@ -312,6 +312,8 @@ DEFINE_TEST(test_write_format_iso9660_filename)
 
        buff = malloc(buffsize);
        assert(buff != NULL);
+       if (buff == NULL)
+               return;
        memset(&fns, 0, sizeof(fns));
 
        /*
@@ -321,6 +323,10 @@ DEFINE_TEST(test_write_format_iso9660_filename)
 
        fns.names = (char **)malloc(sizeof(char *) * fcnt);
        assert(fns.names != NULL);
+       if (fns.names == NULL) {
+               free(buff);
+               return;
+       }
        fns.alloc = fcnt;
 
        /* Verify rockridge filenames. */
index 26a30586bc981d0338307db94f89f1fca40ebf05..136255b32ef91fca25ae3b510686158e3c017f9b 100644 (file)
@@ -106,6 +106,8 @@ test_write_format_iso9660_zisofs_1(void)
        memset(nullb, 0, sizeof(nullb));
        buff = malloc(buffsize);
        assert(buff != NULL);
+       if (buff == NULL)
+               return;
 
        /* ISO9660 format: Create a new archive in memory. */
        assert((a = archive_write_new()) != NULL);
@@ -115,6 +117,7 @@ test_write_format_iso9660_zisofs_1(void)
        if (r == ARCHIVE_FATAL) {
                skipping("zisofs option not supported on this platform");
                assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+               free(buff);
                return;
        }
        assertEqualIntA(a, 0, archive_write_set_option(a, NULL, "pad", NULL));
@@ -335,6 +338,8 @@ test_write_format_iso9660_zisofs_2(void)
 
        buff = malloc(buffsize);
        assert(buff != NULL);
+       if (buff == NULL)
+               return;
 
        /* ISO9660 format: Create a new archive in memory. */
        assert((a = archive_write_new()) != NULL);
@@ -344,6 +349,7 @@ test_write_format_iso9660_zisofs_2(void)
        if (r == ARCHIVE_FATAL) {
                skipping("zisofs option not supported on this platform");
                assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+               free(buff);
                return;
        }
        assertEqualIntA(a, 0, archive_write_set_option(a, NULL, "pad", NULL));
@@ -585,6 +591,8 @@ test_write_format_iso9660_zisofs_3(void)
        memset(nullb, 0, sizeof(nullb));
        buff = malloc(buffsize);
        assert(buff != NULL);
+       if (buff == NULL)
+               return;
 
        /* ISO9660 format: Create a new archive in memory. */
        assert((a = archive_write_new()) != NULL);
@@ -594,6 +602,7 @@ test_write_format_iso9660_zisofs_3(void)
        if (r == ARCHIVE_FATAL) {
                skipping("zisofs option not supported on this platform");
                assertEqualInt(ARCHIVE_OK, archive_write_free(a));
+               free(buff);
                return;
        }
        assertEqualIntA(a, 0, archive_write_set_option(a, NULL, "boot", "boot.img"));
index 14d9b3e642ab227d6137f15b86c5c9625899fba9..d440c6be106331cfb9cbc9b0f04e97dbfb98c81e 100644 (file)
@@ -748,6 +748,8 @@ assertion_equal_mem(const char *file, int line,
        assertion_count(file, line);
        if (v1 == v2 || (v1 != NULL && v2 != NULL && memcmp(v1, v2, l) == 0))
                return (1);
+       if (v1 == NULL || v2 == NULL)
+               return (0);
 
        failure_start(file, line, "%s != %s", e1, e2);
        logprintf("      size %s = %d\n", ld, (int)l);
@@ -1011,8 +1013,8 @@ assertion_file_contains_lines_any_order(const char *file, int line,
        char *buff;
        size_t buff_size;
        size_t expected_count, actual_count, i, j;
-       char **expected;
-       char *p, **actual;
+       char **expected = NULL;
+       char *p, **actual = NULL;
        char c;
        int expected_failure = 0, actual_failure = 0;
 
@@ -1025,14 +1027,22 @@ assertion_file_contains_lines_any_order(const char *file, int line,
                return (0);
        }
 
-       /* Make a copy of the provided lines and count up the expected file size. */
-       expected_count = 0;
+       /* Make a copy of the provided lines and count up the expected
+        * file size. */
        for (i = 0; lines[i] != NULL; ++i) {
        }
        expected_count = i;
-       expected = malloc(sizeof(char *) * expected_count);
-       for (i = 0; lines[i] != NULL; ++i) {
-               expected[i] = strdup(lines[i]);
+       if (expected_count) {
+               expected = malloc(sizeof(char *) * expected_count);
+               if (expected == NULL) {
+                       failure_start(pathname, line, "Can't allocate memory");
+                       failure_finish(NULL);
+                       free(expected);
+                       return (0);
+               }
+               for (i = 0; lines[i] != NULL; ++i) {
+                       expected[i] = strdup(lines[i]);
+               }
        }
 
        /* Break the file into lines */
@@ -1044,11 +1054,19 @@ assertion_file_contains_lines_any_order(const char *file, int line,
                        ++actual_count;
                c = *p;
        }
-       actual = malloc(sizeof(char *) * actual_count);
-       for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
-               if (*p != '\0') {
-                       actual[j] = p;
-                       ++j;
+       if (actual_count) {
+               actual = calloc(sizeof(char *), actual_count);
+               if (actual == NULL) {
+                       failure_start(pathname, line, "Can't allocate memory");
+                       failure_finish(NULL);
+                       free(expected);
+                       return (0);
+               }
+               for (j = 0, p = buff; p < buff + buff_size; p += 1 + strlen(p)) {
+                       if (*p != '\0') {
+                               actual[j] = p;
+                               ++j;
+                       }
                }
        }
 
@@ -2762,6 +2780,7 @@ main(int argc, char **argv)
                        if (test_num < 0) {
                                printf("*** INVALID Test %s\n", *argv);
                                free(refdir_alloc);
+                               free(testprogdir);
                                usage(progname);
                                return (1);
                        }
index bfac8ddc30f4641a51f71f8512935d81386f056f..77876857ea7766611a375498ca58f9fecda1d4aa 100644 (file)
@@ -60,6 +60,11 @@ 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;
+       }
+
        for (i = 0; i < (int)buff_size; ++i)
                buff[i] = "abcdefghijklmnopqrstuvwxyz"[rand() % 26];
        buff[buff_size - 1] = '\0';