[tor-commits] [tor/master] Fix memory leaks in test_circuit_timeout
nickm at torproject.org
nickm at torproject.org
Thu May 1 16:31:43 UTC 2014
commit c7951731ed1bcc06d1eb404bd699cc0e0392e626
Author: Nick Mathewson <nickm at torproject.org>
Date: Sat Apr 26 00:10:04 2014 -0400
Fix memory leaks in test_circuit_timeout
Found with valgrind.
---
src/or/statefile.c | 29 ++++++++++++++++++++++-------
src/or/statefile.h | 2 ++
src/test/test.c | 16 +++++++++++-----
3 files changed, 35 insertions(+), 12 deletions(-)
diff --git a/src/or/statefile.c b/src/or/statefile.c
index da31341..a50eae5 100644
--- a/src/or/statefile.c
+++ b/src/or/statefile.c
@@ -294,6 +294,16 @@ or_state_save_broken(char *fname)
tor_free(fname2);
}
+STATIC or_state_t *
+or_state_new(void)
+{
+ or_state_t *new_state = tor_malloc_zero(sizeof(or_state_t));
+ new_state->magic_ = OR_STATE_MAGIC;
+ config_init(&state_format, new_state);
+
+ return new_state;
+}
+
/** Reload the persistent state from disk, generating a new state as needed.
* Return 0 on success, less than 0 on failure.
*/
@@ -321,9 +331,7 @@ or_state_load(void)
log_warn(LD_GENERAL,"State file \"%s\" is not a file? Failing.", fname);
goto done;
}
- new_state = tor_malloc_zero(sizeof(or_state_t));
- new_state->magic_ = OR_STATE_MAGIC;
- config_init(&state_format, new_state);
+ new_state = or_state_new();
if (contents) {
config_line_t *lines=NULL;
int assign_retval;
@@ -358,9 +366,7 @@ or_state_load(void)
tor_free(contents);
config_free(&state_format, new_state);
- new_state = tor_malloc_zero(sizeof(or_state_t));
- new_state->magic_ = OR_STATE_MAGIC;
- config_init(&state_format, new_state);
+ new_state = or_state_new();
} else if (contents) {
log_info(LD_GENERAL, "Loaded state from \"%s\"", fname);
} else {
@@ -625,10 +631,19 @@ save_transport_to_state(const char *transport,
tor_free(transport_addrport);
}
+STATIC void
+or_state_free(or_state_t *state)
+{
+ if (!state)
+ return;
+
+ config_free(&state_format, state);
+}
+
void
or_state_free_all(void)
{
- config_free(&state_format, global_state);
+ or_state_free(global_state);
global_state = NULL;
}
diff --git a/src/or/statefile.h b/src/or/statefile.h
index c1413ff..15bb0b4 100644
--- a/src/or/statefile.h
+++ b/src/or/statefile.h
@@ -20,6 +20,8 @@ void or_state_free_all(void);
#ifdef STATEFILE_PRIVATE
STATIC config_line_t *get_transport_in_state_by_name(const char *transport);
+STATIC void or_state_free(or_state_t *state);
+STATIC or_state_t *or_state_new(void);
#endif
#endif
diff --git a/src/test/test.c b/src/test/test.c
index 771725e..f1486e9 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -32,6 +32,7 @@ const char tor_git_revision[] = "";
#define ROUTER_PRIVATE
#define CIRCUITSTATS_PRIVATE
#define CIRCUITLIST_PRIVATE
+#define STATEFILE_PRIVATE
/*
* Linux doesn't provide lround in math.h by default, but mac os does...
@@ -59,6 +60,7 @@ double fabs(double x);
#include "policies.h"
#include "rephist.h"
#include "routerparse.h"
+#include "statefile.h"
#ifdef CURVE25519_ENABLED
#include "crypto_curve25519.h"
#include "onion_ntor.h"
@@ -466,14 +468,14 @@ test_circuit_timeout(void)
circuit_build_times_t estimate;
circuit_build_times_t final;
double timeout1, timeout2;
- or_state_t state;
+ or_state_t *state=NULL;
int i, runs;
double close_ms;
circuit_build_times_init(&initial);
circuit_build_times_init(&estimate);
circuit_build_times_init(&final);
- memset(&state, 0, sizeof(or_state_t));
+ state = or_state_new();
circuitbuild_running_unit_tests();
#define timeout0 (build_time_t)(30*1000.0)
@@ -505,8 +507,9 @@ test_circuit_timeout(void)
test_assert(estimate.total_build_times <= CBT_NCIRCUITS_TO_OBSERVE);
- circuit_build_times_update_state(&estimate, &state);
- test_assert(circuit_build_times_parse_state(&final, &state) == 0);
+ circuit_build_times_update_state(&estimate, state);
+ circuit_build_times_free_timeouts(&final);
+ test_assert(circuit_build_times_parse_state(&final, state) == 0);
circuit_build_times_update_alpha(&final);
timeout2 = circuit_build_times_calculate_timeout(&final,
@@ -595,7 +598,10 @@ test_circuit_timeout(void)
}
done:
- return;
+ circuit_build_times_free_timeouts(&initial);
+ circuit_build_times_free_timeouts(&estimate);
+ circuit_build_times_free_timeouts(&final);
+ or_state_free(state);
}
/** Test encoding and parsing of rendezvous service descriptors. */
More information about the tor-commits
mailing list