aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_options.c
diff options
context:
space:
mode:
authorTaylor Yu <catalyst@torproject.org>2017-05-03 07:55:20 -0400
committerTaylor Yu <catalyst@torproject.org>2017-05-03 08:59:29 -0400
commit7b64f1773d144bf38ac293d3300dcbc9ac2c445f (patch)
tree6a3329d860d228bab3119454c546f4d2015f3b52 /src/test/test_options.c
parente0b1fd4d3da05701c0970dabd93010d6c7550d43 (diff)
downloadtor-7b64f1773d144bf38ac293d3300dcbc9ac2c445f.tar.gz
tor-7b64f1773d144bf38ac293d3300dcbc9ac2c445f.zip
Fix memory management for #22103 tests
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.
Diffstat (limited to 'src/test/test_options.c')
-rw-r--r--src/test/test_options.c48
1 files changed, 31 insertions, 17 deletions
diff --git a/src/test/test_options.c b/src/test/test_options.c
index 29f6665f23..291147be6c 100644
--- a/src/test/test_options.c
+++ b/src/test/test_options.c
@@ -106,6 +106,35 @@ clear_log_messages(void)
" 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,
int expect_log_severity,
@@ -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;