Browse Source

Fix memory leaks in test_circuit_timeout

Found with valgrind.
Nick Mathewson 10 years ago
parent
commit
c7951731ed
3 changed files with 35 additions and 12 deletions
  1. 22 7
      src/or/statefile.c
  2. 2 0
      src/or/statefile.h
  3. 11 5
      src/test/test.c

+ 22 - 7
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;
 }
 

+ 2 - 0
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

+ 11 - 5
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. */