Skip to content

Commit bdbc70d

Browse files
committed
Be more defensive in do_test(..)
Shuffle around assertions with `nlines` to the appropriate locations and initialize `lines[]` to an array of `NULL` pointers to avoid reading unintialized memory. Signed-off-by: Enji Cooper <ngie@FreeBSD.org>
1 parent a3dd72c commit bdbc70d

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

atf-c/detail/sanity_test.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,16 @@ do_test_child(void *v)
8585
exit(EXIT_SUCCESS);
8686
}
8787

88+
#define MAX_LINES 3
89+
8890
static
8991
void
9092
do_test(enum type t, bool cond)
9193
{
9294
atf_process_child_t child;
9395
atf_process_status_t status;
9496
int nlines;
95-
char *lines[3];
97+
char *lines[MAX_LINES] = { 0 };
9698

9799
{
98100
atf_process_stream_t outsb, errsb;
@@ -106,16 +108,17 @@ do_test(enum type t, bool cond)
106108
}
107109

108110
nlines = 0;
109-
while (nlines < 3 && (lines[nlines] =
111+
while (nlines < MAX_LINES && (lines[nlines] =
110112
atf_utils_readline(atf_process_child_stderr(&child))) != NULL)
111113
nlines++;
112-
ATF_REQUIRE(nlines == 0 || nlines == 3);
113114

114115
RE(atf_process_child_wait(&child, &status));
115116
if (!cond) {
117+
ATF_REQUIRE(nlines == MAX_LINES);
116118
ATF_REQUIRE(atf_process_status_signaled(&status));
117119
ATF_REQUIRE(atf_process_status_termsig(&status) == SIGABRT);
118120
} else {
121+
ATF_REQUIRE(nlines == 0);
119122
ATF_REQUIRE(atf_process_status_exited(&status));
120123
ATF_REQUIRE(atf_process_status_exitstatus(&status) == EXIT_SUCCESS);
121124
}

0 commit comments

Comments
 (0)