From 9400da9b5e44bfce0684a3b36edb37465be514d6 Mon Sep 17 00:00:00 2001 From: teor Date: Sat, 9 Mar 2019 10:50:07 +1000 Subject: [PATCH 1/2] test/sr: Free SRVs before replacing them in test_sr_setup_srv() Stop leaking parts of the shared random state in the shared-random unit tests. The previous fix in 29599 was incomplete. Fixes bug 29706; bugfix on 0.2.9.1-alpha. --- changes/bug29706_minimal | 4 ++++ src/or/shared_random_state.c | 4 ++-- src/or/shared_random_state.h | 2 ++ src/test/test_shared_random.c | 4 ++++ 4 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 changes/bug29706_minimal diff --git a/changes/bug29706_minimal b/changes/bug29706_minimal new file mode 100644 index 0000000000..9d4a43326c --- /dev/null +++ b/changes/bug29706_minimal @@ -0,0 +1,4 @@ + o Minor bugfixes (memory management, testing): + - Stop leaking parts of the shared random state in the shared-random unit + tests. The previous fix in 29599 was incomplete. + Fixes bug 29706; bugfix on 0.2.9.1-alpha. diff --git a/src/or/shared_random_state.c b/src/or/shared_random_state.c index 8438d46404..f27eccafc7 100644 --- a/src/or/shared_random_state.c +++ b/src/or/shared_random_state.c @@ -1007,7 +1007,7 @@ state_query(sr_state_action_t action, sr_state_object_t obj_type, /* Delete the current SRV value from the state freeing it and the value is set * to NULL meaning empty. */ -static void +STATIC void state_del_current_srv(void) { state_query(SR_STATE_ACTION_DEL, SR_STATE_OBJ_CURSRV, NULL, NULL); @@ -1015,7 +1015,7 @@ state_del_current_srv(void) /* Delete the previous SRV value from the state freeing it and the value is * set to NULL meaning empty. */ -static void +STATIC void state_del_previous_srv(void) { state_query(SR_STATE_ACTION_DEL, SR_STATE_OBJ_PREVSRV, NULL, NULL); diff --git a/src/or/shared_random_state.h b/src/or/shared_random_state.h index 43a7f1d284..cf027f2d35 100644 --- a/src/or/shared_random_state.h +++ b/src/or/shared_random_state.h @@ -140,6 +140,8 @@ STATIC int is_phase_transition(sr_phase_t next_phase); STATIC void set_sr_phase(sr_phase_t phase); STATIC sr_state_t *get_sr_state(void); +STATIC void state_del_previous_srv(void); +STATIC void state_del_current_srv(void); #endif /* TOR_UNIT_TESTS */ diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c index cebe772d94..0a3c2e119b 100644 --- a/src/test/test_shared_random.c +++ b/src/test/test_shared_random.c @@ -461,6 +461,8 @@ test_sr_setup_srv(int also_current) "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ", sizeof(srv->value)); + /* sr_state_set_previous_srv() does not free() the old previous srv. */ + state_del_previous_srv(); sr_state_set_previous_srv(srv); if (also_current) { @@ -470,6 +472,8 @@ test_sr_setup_srv(int also_current) "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", sizeof(srv->value)); + /* sr_state_set_previous_srv() does not free() the old current srv. */ + state_del_current_srv(); sr_state_set_current_srv(srv); } } From c44ad396f89e755f5a639b382e0a55ed1cbcc177 Mon Sep 17 00:00:00 2001 From: teor Date: Mon, 18 Mar 2019 11:12:25 +1000 Subject: [PATCH 2/2] test/sr: Clear SRVs after init, and before setup Already merged to 0.4.0 and later in tor-github/pr/776. Backported to 0.2.9 and later with minor comment changes. Part of 29706. --- src/test/test_shared_random.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/test/test_shared_random.c b/src/test/test_shared_random.c index 0a3c2e119b..526fb09b67 100644 --- a/src/test/test_shared_random.c +++ b/src/test/test_shared_random.c @@ -54,6 +54,9 @@ init_authority_state(void) * the phase we are currently in which uses "now" as the starting * timestamp. Delete it before we do any testing below. */ sr_state_delete_commits(); + /* It's also possible that a current SRV has been generated, if we are at + * state transition time. But let's just forget about that SRV. */ + sr_state_clean_srvs(); done: UNMOCK(get_my_v3_authority_cert); @@ -449,20 +452,26 @@ test_encoding(void *arg) ; } -/** Setup some SRVs in our SR state. If also_current is set, then set - * both current and previous SRVs. - * Helper of test_vote() and test_sr_compute_srv(). */ +/** Setup some SRVs in our SR state. + * If also_current is set, then set both current and previous SRVs. + * Otherwise, just set the previous SRV. (And clear the current SRV.) + * + * You must call sr_state_free() to free the state at the end of each test + * function (on pass or fail). */ static void test_sr_setup_srv(int also_current) { + /* Clear both SRVs before starting. + * In 0.3.5 and earlier, sr_state_set_previous_srv() and + * sr_state_set_current_srv() do not free() the old srvs. */ + sr_state_clean_srvs(); + sr_srv_t *srv = tor_malloc_zero(sizeof(sr_srv_t)); srv->num_reveals = 42; memcpy(srv->value, "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ", sizeof(srv->value)); - /* sr_state_set_previous_srv() does not free() the old previous srv. */ - state_del_previous_srv(); sr_state_set_previous_srv(srv); if (also_current) { @@ -472,8 +481,6 @@ test_sr_setup_srv(int also_current) "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", sizeof(srv->value)); - /* sr_state_set_previous_srv() does not free() the old current srv. */ - state_del_current_srv(); sr_state_set_current_srv(srv); } }