Browse Source

Merge remote-tracking branch 'public/valgrind_tests'

Nick Mathewson 10 years ago
parent
commit
b51ce90777

+ 5 - 0
changes/bug11618

@@ -0,0 +1,5 @@
+  o Minor bugfixes:
+    - Fix all valgrind warnings produced by the unit tests. There were
+      over a thousand memory leak warnings previously, mostly produced
+      by forgetting to free things in the unit test code.  Fixes bug
+      11618, bugfixes on many versions of Tor.

+ 2 - 1
src/or/networkstatus.c

@@ -10,6 +10,7 @@
  * client or cache.
  */
 
+#define NETWORKSTATUS_PRIVATE
 #include "or.h"
 #include "channel.h"
 #include "circuitmux.h"
@@ -183,7 +184,7 @@ router_reload_consensus_networkstatus(void)
 }
 
 /** Free all storage held by the vote_routerstatus object <b>rs</b>. */
-static void
+STATIC void
 vote_routerstatus_free(vote_routerstatus_t *rs)
 {
   vote_microdesc_hash_t *h, *next;

+ 4 - 0
src/or/networkstatus.h

@@ -99,5 +99,9 @@ document_signature_t *document_signature_dup(const document_signature_t *sig);
 void networkstatus_free_all(void);
 int networkstatus_get_weight_scale_param(networkstatus_t *ns);
 
+#ifdef NETWORKSTATUS_PRIVATE
+STATIC void vote_routerstatus_free(vote_routerstatus_t *rs);
+#endif
+
 #endif
 

+ 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

+ 18 - 6
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"
@@ -416,9 +418,10 @@ test_onion_queues(void)
   or_circuit_t *circ1 = or_circuit_new(0, NULL);
   or_circuit_t *circ2 = or_circuit_new(0, NULL);
 
-  create_cell_t *onionskin = NULL;
+  create_cell_t *onionskin = NULL, *create2_ptr;
   create_cell_t *create1 = tor_malloc_zero(sizeof(create_cell_t));
   create_cell_t *create2 = tor_malloc_zero(sizeof(create_cell_t));
+  create2_ptr = create2; /* remember, but do not free */
 
   create_cell_init(create1, CELL_CREATE, ONION_HANDSHAKE_TYPE_TAP,
                    TAP_ONIONSKIN_CHALLENGE_LEN, buf1);
@@ -438,6 +441,7 @@ test_onion_queues(void)
   test_eq_ptr(circ2, onion_next_task(&onionskin));
   test_eq(1, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
   test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_NTOR));
+  tt_ptr_op(onionskin, ==, create2_ptr);
 
   clear_pending_onions();
   test_eq(0, onion_num_pending(ONION_HANDSHAKE_TYPE_TAP));
@@ -448,6 +452,7 @@ test_onion_queues(void)
   circuit_free(TO_CIRCUIT(circ2));
   tor_free(create1);
   tor_free(create2);
+  tor_free(onionskin);
 }
 
 static void
@@ -466,14 +471,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 +510,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 +601,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. */
@@ -946,6 +955,7 @@ test_geoip(void)
   geoip_start_dirreq((uint64_t) 1, 1024, DIRREQ_TUNNELED);
   s = geoip_format_dirreq_stats(now + 86400);
   test_streq(dirreq_stats_4, s);
+  tor_free(s);
 
   /* Stop collecting directory request statistics and start gathering
    * entry stats. */
@@ -1008,6 +1018,8 @@ test_geoip_with_pt(void)
   get_options_mutable()->BridgeRelay = 1;
   get_options_mutable()->BridgeRecordUsageByCountry = 1;
 
+  memset(&in6, 0, sizeof(in6));
+
   /* No clients seen yet. */
   s = geoip_get_transport_history();
   tor_assert(!s);

+ 1 - 0
src/test/test_buffers.c

@@ -522,6 +522,7 @@ test_buffer_allocation_tracking(void *arg)
   buf_free(buf1);
   buf_free(buf2);
   buf_shrink_freelists(1);
+  tor_free(junk);
 }
 
 static void

+ 14 - 4
src/test/test_config.c

@@ -124,6 +124,7 @@ test_config_addressmap(void *arg)
   test_assert(!addressmap_rewrite(address, sizeof(address), &expires, NULL));
 
   /* Test top-level-domain matching a bit harder */
+  config_free_lines(get_options_mutable()->AddressMap);
   addressmap_clear_configured();
   strlcpy(buf, "MapAddress *.com *.torserver.exit\n"
           "MapAddress *.torproject.org 1.1.1.1\n"
@@ -153,6 +154,7 @@ test_config_addressmap(void *arg)
   test_streq(address, "2.2.2.2");
 
   /* We don't support '*' as a mapping directive */
+  config_free_lines(get_options_mutable()->AddressMap);
   addressmap_clear_configured();
   strlcpy(buf, "MapAddress * *.torserver.exit\n", sizeof(buf));
   config_get_lines(buf, &(get_options_mutable()->AddressMap), 0);
@@ -170,7 +172,8 @@ test_config_addressmap(void *arg)
 #undef addressmap_rewrite
 
  done:
-  ;
+  config_free_lines(get_options_mutable()->AddressMap);
+  get_options_mutable()->AddressMap = NULL;
 }
 
 static int
@@ -193,9 +196,9 @@ static void
 test_config_check_or_create_data_subdir(void *arg)
 {
   or_options_t *options = get_options_mutable();
-  char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
+  char *datadir;
   const char *subdir = "test_stats";
-  char *subpath = get_datadir_fname(subdir);
+  char *subpath;
   struct stat st;
   int r;
 #if !defined (_WIN32) || defined (WINCE)
@@ -203,6 +206,10 @@ test_config_check_or_create_data_subdir(void *arg)
 #endif
   (void)arg;
 
+  tor_free(options->DataDirectory);
+  datadir = options->DataDirectory = tor_strdup(get_fname("datadir-0"));
+  subpath = get_datadir_fname(subdir);
+
 #if defined (_WIN32) && !defined (WINCE)
   tt_int_op(mkdir(options->DataDirectory), ==, 0);
 #else
@@ -251,7 +258,7 @@ static void
 test_config_write_to_data_subdir(void *arg)
 {
   or_options_t* options = get_options_mutable();
-  char *datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
+  char *datadir;
   char *cp = NULL;
   const char* subdir = "test_stats";
   const char* fname = "test_file";
@@ -273,6 +280,9 @@ test_config_write_to_data_subdir(void *arg)
   char* filepath = get_datadir_fname2(subdir, fname);
   (void)arg;
 
+  tor_free(options->DataDirectory);
+  datadir = options->DataDirectory = tor_strdup(get_fname("datadir-1"));
+
 #if defined (_WIN32) && !defined (WINCE)
   tt_int_op(mkdir(options->DataDirectory), ==, 0);
 #else

+ 15 - 6
src/test/test_controller_events.c

@@ -148,6 +148,7 @@ static void
 test_cntev_append_cell_stats(void *arg)
 {
   smartlist_t *event_parts;
+  char *cp = NULL;
   const char *key = "Z";
   uint64_t include_if_non_zero[CELL_COMMAND_MAX_ + 1],
            number_to_include[CELL_COMMAND_MAX_ + 1];
@@ -178,7 +179,9 @@ test_cntev_append_cell_stats(void *arg)
   append_cell_stats_by_command(event_parts, key,
                                include_if_non_zero,
                                number_to_include);
-  tt_str_op("Z=relay:1", ==, smartlist_pop_last(event_parts));
+  cp = smartlist_pop_last(event_parts);
+  tt_str_op("Z=relay:1", ==, cp);
+  tor_free(cp);
 
   /* Add four CREATE cells. */
   include_if_non_zero[CELL_CREATE] = 3;
@@ -186,20 +189,22 @@ test_cntev_append_cell_stats(void *arg)
   append_cell_stats_by_command(event_parts, key,
                                include_if_non_zero,
                                number_to_include);
-  tt_str_op("Z=create:4,relay:1", ==, smartlist_pop_last(event_parts));
+  cp = smartlist_pop_last(event_parts);
+  tt_str_op("Z=create:4,relay:1", ==, cp);
 
  done:
-  ;
+  tor_free(cp);
+  smartlist_free(event_parts);
 }
 
 static void
 test_cntev_format_cell_stats(void *arg)
 {
   char *event_string = NULL;
-  origin_circuit_t *ocirc;
-  or_circuit_t *or_circ;
+  origin_circuit_t *ocirc = NULL;
+  or_circuit_t *or_circ = NULL;
   cell_stats_t *cell_stats = NULL;
-  channel_tls_t *n_chan, *p_chan;
+  channel_tls_t *n_chan=NULL, *p_chan=NULL;
   (void)arg;
 
   n_chan = tor_malloc_zero(sizeof(channel_tls_t));
@@ -282,6 +287,10 @@ test_cntev_format_cell_stats(void *arg)
  done:
   tor_free(cell_stats);
   tor_free(event_string);
+  tor_free(or_circ);
+  tor_free(ocirc);
+  tor_free(p_chan);
+  tor_free(n_chan);
 }
 
 #define TEST(name, flags)                                               \

+ 11 - 6
src/test/test_dir.c

@@ -11,6 +11,7 @@
 #define ROUTER_PRIVATE
 #define ROUTERLIST_PRIVATE
 #define HIBERNATE_PRIVATE
+#define NETWORKSTATUS_PRIVATE
 #include "or.h"
 #include "config.h"
 #include "directory.h"
@@ -194,6 +195,7 @@ test_dir_formats(void)
   test_assert(crypto_pk_cmp_keys(rp1->onion_pkey, pk1) == 0);
   test_assert(crypto_pk_cmp_keys(rp1->identity_pkey, pk2) == 0);
   //test_assert(rp1->exit_policy == NULL);
+  tor_free(buf);
 
   strlcpy(buf2,
           "router Fred 10.3.2.1 9005 0 0\n"
@@ -277,6 +279,8 @@ test_dir_formats(void)
     routerinfo_free(r1);
   if (r2)
     routerinfo_free(r2);
+  if (rp2)
+    routerinfo_free(rp2);
 
   tor_free(buf);
   tor_free(pk1_str);
@@ -1011,16 +1015,14 @@ vote_tweaks_for_v3ns(networkstatus_t *v, int voter, time_t now)
     /* Monkey around with the list a bit */
     vrs = smartlist_get(v->routerstatus_list, 2);
     smartlist_del_keeporder(v->routerstatus_list, 2);
-    tor_free(vrs->version);
-    tor_free(vrs);
+    vote_routerstatus_free(vrs);
     vrs = smartlist_get(v->routerstatus_list, 0);
     vrs->status.is_fast = 1;
 
     if (voter == 3) {
       vrs = smartlist_get(v->routerstatus_list, 0);
       smartlist_del_keeporder(v->routerstatus_list, 0);
-      tor_free(vrs->version);
-      tor_free(vrs);
+      vote_routerstatus_free(vrs);
       vrs = smartlist_get(v->routerstatus_list, 0);
       memset(vrs->status.descriptor_digest, (int)'Z', DIGEST_LEN);
       test_assert(router_add_to_routerlist(
@@ -1360,7 +1362,8 @@ test_a_networkstatus(
   vote->dist_seconds = 300;
   authority_cert_free(vote->cert);
   vote->cert = authority_cert_dup(cert2);
-  vote->net_params = smartlist_new();
+  SMARTLIST_FOREACH(vote->net_params, char *, c, tor_free(c));
+  smartlist_clear(vote->net_params);
   smartlist_split_string(vote->net_params, "bar=2000000000 circuitwindow=20",
                          NULL, 0, 0);
   tor_free(vote->client_versions);
@@ -1404,7 +1407,8 @@ test_a_networkstatus(
   vote->dist_seconds = 250;
   authority_cert_free(vote->cert);
   vote->cert = authority_cert_dup(cert3);
-  vote->net_params = smartlist_new();
+  SMARTLIST_FOREACH(vote->net_params, char *, c, tor_free(c));
+  smartlist_clear(vote->net_params);
   smartlist_split_string(vote->net_params, "circuitwindow=80 foo=660",
                          NULL, 0, 0);
   smartlist_add(vote->supported_methods, tor_strdup("4"));
@@ -1981,6 +1985,7 @@ vote_tweaks_for_umbw(networkstatus_t *v, int voter, time_t now)
   (void)now;
 
   test_assert(v->supported_methods);
+  SMARTLIST_FOREACH(v->supported_methods, char *, c, tor_free(c));
   smartlist_clear(v->supported_methods);
   /* Method 17 is MIN_METHOD_TO_CLIP_UNMEASURED_BW_KB */
   smartlist_split_string(v->supported_methods,

+ 1 - 0
src/test/test_extorport.c

@@ -172,6 +172,7 @@ test_ext_or_init_auth(void *arg)
   (void)arg;
 
   /* Check default filename location */
+  tor_free(options->DataDirectory);
   options->DataDirectory = tor_strdup("foo");
   cp = get_ext_or_auth_cookie_file_name();
   tt_str_op(cp, ==, "foo"PATH_SEPARATOR"extended_orport_auth_cookie");

+ 1 - 0
src/test/test_microdesc.c

@@ -261,6 +261,7 @@ test_md_cache_broken(void *data)
 
   options = get_options_mutable();
   tt_assert(options);
+  tor_free(options->DataDirectory);
   options->DataDirectory = tor_strdup(get_fname("md_datadir_test2"));
 
 #ifdef _WIN32

+ 11 - 0
src/test/test_oom.c

@@ -6,6 +6,7 @@
 #define RELAY_PRIVATE
 #define BUFFERS_PRIVATE
 #define CIRCUITLIST_PRIVATE
+#define CONNECTION_PRIVATE
 #include "or.h"
 #include "buffers.h"
 #include "circuitlist.h"
@@ -216,6 +217,7 @@ test_oom_streambuf(void *arg)
   struct timeval tv = { 1389641159, 0 };
   uint32_t tvms;
   int i;
+  smartlist_t *edgeconns = smartlist_new();
 
   (void) arg;
 
@@ -257,17 +259,21 @@ test_oom_streambuf(void *arg)
     tor_gettimeofday_cache_set(&tv);
     ec = dummy_edge_conn_new(c1, CONN_TYPE_EXIT, 1000, 1000);
     tt_assert(ec);
+    smartlist_add(edgeconns, ec);
     tv.tv_usec += 10*1000;
     tor_gettimeofday_cache_set(&tv);
     ec = dummy_edge_conn_new(c2, CONN_TYPE_AP, 1000, 1000);
     tt_assert(ec);
+    smartlist_add(edgeconns, ec);
     tv.tv_usec += 10*1000;
     tor_gettimeofday_cache_set(&tv);
     ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000); /* Yes, 4 twice*/
     tt_assert(ec);
+    smartlist_add(edgeconns, ec);
     tv.tv_usec += 10*1000;
     tor_gettimeofday_cache_set(&tv);
     ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
+    smartlist_add(edgeconns, ec);
     tt_assert(ec);
   }
 
@@ -302,6 +308,7 @@ test_oom_streambuf(void *arg)
     tor_gettimeofday_cache_set(&tv);
     ec = dummy_edge_conn_new(c4, CONN_TYPE_EXIT, 1000, 1000);
     tt_assert(ec);
+    smartlist_add(edgeconns, ec);
   }
   tt_int_op(buf_get_total_allocation(), ==, 4096*17*2);
   tt_int_op(circuit_max_queued_item_age(c4, tvms), ==, 1000);
@@ -337,6 +344,10 @@ test_oom_streambuf(void *arg)
   circuit_free(c4);
   circuit_free(c5);
 
+  SMARTLIST_FOREACH(edgeconns, edge_connection_t *, ec,
+                    connection_free_(TO_CONN(ec)));
+  smartlist_free(edgeconns);
+
   UNMOCK(circuit_mark_for_close_);
 }
 

+ 2 - 0
src/test/test_policy.c

@@ -394,6 +394,7 @@ test_dump_exit_policy_to_string(void *arg)
  ep = router_dump_exit_policy_to_string(ri,1,1);
 
  test_streq("accept *:*\nreject *:25\nreject 8.8.8.8:*",ep);
+ tor_free(ep);
 
  policy_entry =
  router_parse_addr_policy_item_from_string("reject6 [FC00::]/7:*",-1);
@@ -421,6 +422,7 @@ test_dump_exit_policy_to_string(void *arg)
  if (ri->exit_policy) {
    SMARTLIST_FOREACH(ri->exit_policy, addr_policy_t *,
                      entry, addr_policy_free(entry));
+   smartlist_free(ri->exit_policy);
  }
  tor_free(ri);
  tor_free(ep);

+ 16 - 1
src/test/test_pt.c

@@ -129,6 +129,8 @@ test_pt_parsing(void)
   test_assert(parse_version(line, mp) == 0);
 
  done:
+  reset_mp(mp);
+  smartlist_free(mp->transports);
   tor_free(mp);
 }
 
@@ -227,6 +229,10 @@ test_pt_protocol(void)
   test_assert(mp->conf_state == PT_PROTO_CONFIGURED);
 
  done:
+  reset_mp(mp);
+  smartlist_free(mp->transports);
+  tor_free(mp->argv[0]);
+  tor_free(mp->argv);
   tor_free(mp);
 }
 
@@ -423,7 +429,7 @@ test_pt_configure_proxy(void *arg)
   }
 
  done:
-  tor_free(dummy_state);
+  or_state_free(dummy_state);
   UNMOCK(tor_get_lines_from_handle);
   UNMOCK(tor_process_handle_destroy);
   UNMOCK(get_or_state);
@@ -433,6 +439,15 @@ test_pt_configure_proxy(void *arg)
     smartlist_free(controlevent_msgs);
     controlevent_msgs = NULL;
   }
+  if (mp->transports) {
+    SMARTLIST_FOREACH(mp->transports, transport_t *, t, transport_free(t));
+    smartlist_free(mp->transports);
+  }
+  smartlist_free(mp->transports_to_launch);
+  tor_free(mp->process_handle);
+  tor_free(mp->argv[0]);
+  tor_free(mp->argv);
+  tor_free(mp);
 }
 
 #define PT_LEGACY(name)                                               \

+ 1 - 0
src/test/test_routerkeys.c

@@ -27,6 +27,7 @@ test_routerkeys_write_fingerprint(void *arg)
   tt_assert(key);
 
   options->ORPort_set = 1; /* So that we can get the server ID key */
+  tor_free(options->DataDirectory);
   options->DataDirectory = tor_strdup(ddir);
   options->Nickname = tor_strdup("haflinger");
   set_server_identity_key(key);

+ 12 - 7
src/test/test_status.c

@@ -613,6 +613,9 @@ NS_DECL(or_state_t *, get_or_state, (void));
 NS_DECL(int, accounting_is_enabled, (const or_options_t *options));
 NS_DECL(time_t, accounting_get_end_time, (void));
 
+static or_state_t * NS(mock_state) = NULL;
+static or_options_t * NS(mock_options) = NULL;
+
 static void
 NS(test_main)(void *arg)
 {
@@ -652,6 +655,8 @@ NS(test_main)(void *arg)
     NS_UNMOCK(server_mode);
     NS_UNMOCK(accounting_is_enabled);
     NS_UNMOCK(accounting_get_end_time);
+    tor_free_(NS(mock_state));
+    tor_free_(NS(mock_options));
 }
 
 static double
@@ -669,10 +674,10 @@ NS(we_are_hibernating)(void)
 static const or_options_t *
 NS(get_options)(void)
 {
-  or_options_t *mock_options = tor_malloc_zero(sizeof(or_options_t));
-  mock_options->AccountingMax = 0;
+  NS(mock_options) = tor_malloc_zero(sizeof(or_options_t));
+  NS(mock_options)->AccountingMax = 0;
 
-  return mock_options;
+  return NS(mock_options);
 }
 
 static int
@@ -771,11 +776,11 @@ NS(accounting_get_end_time)(void)
 static or_state_t *
 NS(get_or_state)(void)
 {
-  or_state_t *mock_state = tor_malloc_zero(sizeof(or_state_t));
-  mock_state->AccountingBytesReadInInterval = 0;
-  mock_state->AccountingBytesWrittenInInterval = 0;
+  NS(mock_state) = tor_malloc_zero(sizeof(or_state_t));
+  NS(mock_state)->AccountingBytesReadInInterval = 0;
+  NS(mock_state)->AccountingBytesWrittenInInterval = 0;
 
-  return mock_state;
+  return NS(mock_state);
 }
 
 #undef NS_SUBMODULE

+ 5 - 0
src/test/test_util.c

@@ -2241,18 +2241,21 @@ test_util_asprintf(void *ptr)
   test_assert(cp);
   test_streq("simple string 100% safe", cp);
   test_eq(strlen(cp), r);
+  tor_free(cp);
 
   /* empty string */
   r = tor_asprintf(&cp, "%s", "");
   test_assert(cp);
   test_streq("", cp);
   test_eq(strlen(cp), r);
+  tor_free(cp);
 
   /* numbers (%i) */
   r = tor_asprintf(&cp, "I like numbers-%2i, %i, etc.", -1, 2);
   test_assert(cp);
   test_streq("I like numbers--1, 2, etc.", cp);
   test_eq(strlen(cp), r);
+  /* don't free cp; next test uses it. */
 
   /* numbers (%d) */
   r = tor_asprintf(&cp2, "First=%d, Second=%d", 101, 202);
@@ -3143,6 +3146,8 @@ smartlist_new_from_text_lines(const char *lines)
   last_line = smartlist_pop_last(sl);
   if (last_line != NULL && *last_line != '\0') {
     smartlist_add(sl, last_line);
+  } else {
+    tor_free(last_line);
   }
 
   return sl;