From a875bd416968c4418ddc7f5dd25618e80817188d Mon Sep 17 00:00:00 2001 From: Michihiro NAKAJIMA Date: Wed, 14 Nov 2012 16:10:40 +0900 Subject: [PATCH] Fix bugs in test programs which Clang Static Analyzer pointed out. --- cpio/test/main.c | 42 ++++++++++++------ cpio/test/test_basic.c | 12 +++--- cpio/test/test_format_newc.c | 4 +- libarchive/test/main.c | 43 +++++++++++++------ libarchive/test/test_acl_freebsd_posix1e.c | 11 +++-- libarchive/test/test_acl_nfs4.c | 7 ++- libarchive/test/test_acl_posix1e.c | 2 + libarchive/test/test_archive_cmdline.c | 18 ++++++++ .../test_archive_read_multiple_data_objects.c | 11 +++++ libarchive/test/test_entry.c | 22 ++++++++++ libarchive/test/test_fuzz.c | 6 ++- libarchive/test/test_read_disk.c | 3 +- libarchive/test/test_read_format_cpio_afio.c | 3 +- libarchive/test/test_read_truncated_filter.c | 8 ++++ libarchive/test/test_write_disk_sparse.c | 14 +++++- libarchive/test/test_write_filter_bzip2.c | 8 ++++ libarchive/test/test_write_filter_gzip.c | 8 ++++ .../test/test_write_filter_gzip_timestamp.c | 8 ++++ libarchive/test/test_write_filter_lzip.c | 8 ++++ libarchive/test/test_write_filter_lzma.c | 8 ++++ libarchive/test/test_write_filter_xz.c | 8 ++++ .../test/test_write_format_iso9660_empty.c | 2 + .../test/test_write_format_iso9660_filename.c | 6 +++ .../test/test_write_format_iso9660_zisofs.c | 9 ++++ tar/test/main.c | 43 +++++++++++++------ tar/test/test_option_r.c | 5 +++ 26 files changed, 266 insertions(+), 53 deletions(-) diff --git a/cpio/test/main.c b/cpio/test/main.c index 5ea9c0db8..a571eed05 100644 --- a/cpio/test/main.c +++ b/cpio/test/main.c @@ -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); } diff --git a/cpio/test/test_basic.c b/cpio/test/test_basic.c index c40813e9a..7213062e8 100644 --- a/cpio/test/test_basic.c +++ b/cpio/test/test_basic.c @@ -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); diff --git a/cpio/test/test_format_newc.c b/cpio/test/test_format_newc.c index ced62a639..d2daa46ab 100644 --- a/cpio/test/test_format_newc.c +++ b/cpio/test/test_format_newc.c @@ -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"); diff --git a/libarchive/test/main.c b/libarchive/test/main.c index d442da067..68e965adc 100644 --- a/libarchive/test/main.c +++ b/libarchive/test/main.c @@ -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); } diff --git a/libarchive/test/test_acl_freebsd_posix1e.c b/libarchive/test/test_acl_freebsd_posix1e.c index d2ae6d476..36f9499f4 100644 --- a/libarchive/test/test_acl_freebsd_posix1e.c +++ b/libarchive/test/test_acl_freebsd_posix1e.c @@ -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 diff --git a/libarchive/test/test_acl_nfs4.c b/libarchive/test/test_acl_nfs4.c index ebf9a4656..c8f59371a 100644 --- a/libarchive/test/test_acl_nfs4.c +++ b/libarchive/test/test_acl_nfs4.c @@ -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)); } diff --git a/libarchive/test/test_acl_posix1e.c b/libarchive/test/test_acl_posix1e.c index 2055682bb..9984d4418 100644 --- a/libarchive/test/test_acl_posix1e.c +++ b/libarchive/test/test_acl_posix1e.c @@ -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)); } diff --git a/libarchive/test/test_archive_cmdline.c b/libarchive/test/test_archive_cmdline.c index 963454d48..a41182135 100644 --- a/libarchive/test/test_archive_cmdline.c +++ b/libarchive/test/test_archive_cmdline.c @@ -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); diff --git a/libarchive/test/test_archive_read_multiple_data_objects.c b/libarchive/test/test_archive_read_multiple_data_objects.c index 391bf673f..9962cf7fd 100644 --- a/libarchive/test/test_archive_read_multiple_data_objects.c +++ b/libarchive/test/test_archive_read_multiple_data_objects.c @@ -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]; diff --git a/libarchive/test/test_entry.c b/libarchive/test/test_entry.c index 8249c7319..0ccc9e8fb 100644 --- a/libarchive/test/test_entry.c +++ b/libarchive/test/test_entry.c @@ -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. */ diff --git a/libarchive/test/test_fuzz.c b/libarchive/test/test_fuzz.c index a03f7517a..2b95cc209 100644 --- a/libarchive/test/test_fuzz.c +++ b/libarchive/test/test_fuzz.c @@ -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) { diff --git a/libarchive/test/test_read_disk.c b/libarchive/test/test_read_disk.c index b69e55cfa..bd36c396a 100644 --- a/libarchive/test/test_read_disk.c +++ b/libarchive/test/test_read_disk.c @@ -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) diff --git a/libarchive/test/test_read_format_cpio_afio.c b/libarchive/test/test_read_format_cpio_afio.c index 3d5814bce..db6003f95 100644 --- a/libarchive/test/test_read_format_cpio_afio.c +++ b/libarchive/test/test_read_format_cpio_afio.c @@ -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)); diff --git a/libarchive/test/test_read_truncated_filter.c b/libarchive/test/test_read_truncated_filter.c index 2fe7f6553..bf82546d4 100644 --- a/libarchive/test/test_read_truncated_filter.c +++ b/libarchive/test/test_read_truncated_filter.c @@ -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); diff --git a/libarchive/test/test_write_disk_sparse.c b/libarchive/test/test_write_disk_sparse.c index d25b6c006..36ecf3220 100644 --- a/libarchive/test/test_write_disk_sparse.c +++ b/libarchive/test/test_write_disk_sparse.c @@ -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)); diff --git a/libarchive/test/test_write_filter_bzip2.c b/libarchive/test/test_write_filter_bzip2.c index 74150c8fb..4f32d28cb 100644 --- a/libarchive/test/test_write_filter_bzip2.c +++ b/libarchive/test/test_write_filter_bzip2.c @@ -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; } diff --git a/libarchive/test/test_write_filter_gzip.c b/libarchive/test/test_write_filter_gzip.c index 118da2e46..7bbdb1232 100644 --- a/libarchive/test/test_write_filter_gzip.c +++ b/libarchive/test/test_write_filter_gzip.c @@ -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; } } diff --git a/libarchive/test/test_write_filter_gzip_timestamp.c b/libarchive/test/test_write_filter_gzip_timestamp.c index 5dc020d8f..23b11dba7 100644 --- a/libarchive/test/test_write_filter_gzip_timestamp.c +++ b/libarchive/test/test_write_filter_gzip_timestamp.c @@ -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; } } diff --git a/libarchive/test/test_write_filter_lzip.c b/libarchive/test/test_write_filter_lzip.c index 508ecd00c..145a3084d 100644 --- a/libarchive/test/test_write_filter_lzip.c +++ b/libarchive/test/test_write_filter_lzip.c @@ -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, diff --git a/libarchive/test/test_write_filter_lzma.c b/libarchive/test/test_write_filter_lzma.c index 8f4324576..68e489832 100644 --- a/libarchive/test/test_write_filter_lzma.c +++ b/libarchive/test/test_write_filter_lzma.c @@ -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, diff --git a/libarchive/test/test_write_filter_xz.c b/libarchive/test/test_write_filter_xz.c index ae148cc44..bf1265c65 100644 --- a/libarchive/test/test_write_filter_xz.c +++ b/libarchive/test/test_write_filter_xz.c @@ -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, diff --git a/libarchive/test/test_write_format_iso9660_empty.c b/libarchive/test/test_write_format_iso9660_empty.c index c731d6ef2..55c6c3c7e 100644 --- a/libarchive/test/test_write_format_iso9660_empty.c +++ b/libarchive/test/test_write_format_iso9660_empty.c @@ -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); diff --git a/libarchive/test/test_write_format_iso9660_filename.c b/libarchive/test/test_write_format_iso9660_filename.c index 7b57ed5f0..713883bdc 100644 --- a/libarchive/test/test_write_format_iso9660_filename.c +++ b/libarchive/test/test_write_format_iso9660_filename.c @@ -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. */ diff --git a/libarchive/test/test_write_format_iso9660_zisofs.c b/libarchive/test/test_write_format_iso9660_zisofs.c index 26a30586b..136255b32 100644 --- a/libarchive/test/test_write_format_iso9660_zisofs.c +++ b/libarchive/test/test_write_format_iso9660_zisofs.c @@ -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")); diff --git a/tar/test/main.c b/tar/test/main.c index 14d9b3e64..d440c6be1 100644 --- a/tar/test/main.c +++ b/tar/test/main.c @@ -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); } diff --git a/tar/test/test_option_r.c b/tar/test/test_option_r.c index bfac8ddc3..77876857e 100644 --- a/tar/test/test_option_r.c +++ b/tar/test/test_option_r.c @@ -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'; -- 2.47.2