Permalink
Browse files

Fix bugs in test programs which Clang Static Analyzer pointed out.

  • Loading branch information...
ggcueroad committed Nov 14, 2012
1 parent 5923321 commit a875bd416968c4418ddc7f5dd25618e80817188d
View
@@ -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);
}
View
@@ -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);
@@ -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");
View
@@ -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);
}
@@ -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
@@ -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));
}
@@ -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));
}
@@ -35,13 +35,17 @@ 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);
assertEqualString("gzip", cl->argv[0]);
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);
@@ -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];
Oops, something went wrong.

0 comments on commit a875bd4

Please sign in to comment.