Skip to content

Commit

Permalink
Fix memory management for #22103 tests
Browse files Browse the repository at this point in the history
Code movement in the commit introducings tests for #22103 uncovered a
latent memory management bug.

Refactor the log message checking from test_options_checkmsgs() into a
helper test_options_checklog().  This avoids a memory leak (and
possible double-free) in a test failure condition.

Don't reuse variables (especially pointers to allocated memory!) for
multiple unrelated purposes.

Fixes CID 1405778.
  • Loading branch information
tlyu committed May 3, 2017
1 parent e0b1fd4 commit 7b64f17
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions src/test/test_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,35 @@ clear_log_messages(void)
"EDE6D711294FADF8E7951F4DE6CA56B58 194.109.206.212:80 7EA6 EAD6 FD83" \
" 083C 538F 4403 8BBF A077 587D D755\n"

static int
test_options_checklog(const char *configuration, int expect_log_severity,
const char *expect_log)
{
int found = 0, ret = -1;
char *actual_log = NULL;

if (messages) {
SMARTLIST_FOREACH_BEGIN(messages, logmsg_t *, m) {
if (m->severity == expect_log_severity &&
strstr(m->msg, expect_log)) {
found = 1;
break;
}
} SMARTLIST_FOREACH_END(m);
}
if (!found) {
actual_log = dump_logs();
TT_DIE(("Expected log message [%s] %s from <%s>, but got <%s>.",
log_level_to_string(expect_log_severity), expect_log,
configuration, actual_log));
}
ret = 0;

done:
tor_free(actual_log);
return ret;
}

static int
test_options_checkmsgs(const char *configuration,
const char *expect_errmsg,
Expand All @@ -123,23 +152,8 @@ test_options_checkmsgs(const char *configuration,
configuration, msg));
}
if (expect_log) {
int found = 0;
if (messages) {
SMARTLIST_FOREACH_BEGIN(messages, logmsg_t *, m) {
if (m->severity == expect_log_severity &&
strstr(m->msg, expect_log)) {
found = 1;
break;
}
} SMARTLIST_FOREACH_END(m);
}
if (!found) {
tor_free(msg);
msg = dump_logs();
TT_DIE(("Expected log message [%s] %s from <%s>, but got <%s>.",
log_level_to_string(expect_log_severity), expect_log,
configuration, msg));
}
return test_options_checklog(configuration, expect_log_severity,
expect_log);
}
return 0;

Expand Down

0 comments on commit 7b64f17

Please sign in to comment.