[tor-commits] [tor/master] Resolve memory leaks in the unit tests and benchmarks (found by coverity)

nickm at torproject.org nickm at torproject.org
Mon Feb 11 20:24:24 UTC 2013


commit 717946089b6008e4744522845014dafa1fc4e37c
Author: Nick Mathewson <nickm at torproject.org>
Date:   Mon Feb 11 14:57:10 2013 -0500

    Resolve memory leaks in the unit tests and benchmarks (found by coverity)
    
    These shouldn't really matter, but it's nice to be leak-free.
---
 src/test/bench.c        |    2 ++
 src/test/test.c         |    1 +
 src/test/test_crypto.c  |    3 +++
 src/test/test_ntor_cl.c |   24 ++++++++++++++----------
 src/test/test_util.c    |    2 +-
 5 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/test/bench.c b/src/test/bench.c
index d57aeb8..18a5154 100644
--- a/src/test/bench.c
+++ b/src/test/bench.c
@@ -164,6 +164,7 @@ bench_onion_TAP(void)
     int s;
     dh = crypto_dh_dup(dh_out);
     s = onion_skin_TAP_client_handshake(dh, or, key_out, sizeof(key_out));
+    crypto_dh_free(dh);
     tor_assert(s == 0);
   }
   end = perftime();
@@ -203,6 +204,7 @@ bench_onion_ntor(void)
   end = perftime();
   printf("Client-side, part 1: %f usec.\n", NANOCOUNT(start, end, iters)/1e3);
 
+  state = NULL;
   onion_skin_ntor_create(nodeid, &keypair1.pubkey, &state, os);
   start = perftime();
   for (i = 0; i < iters; ++i) {
diff --git a/src/test/test.c b/src/test/test.c
index b838172..be0737c 100644
--- a/src/test/test.c
+++ b/src/test/test.c
@@ -1789,6 +1789,7 @@ test_geoip(void)
 
  done:
   tor_free(s);
+  tor_free(v);
 }
 
 /** Run unit tests for stats code. */
diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
index 33c3d44..afb338a 100644
--- a/src/test/test_crypto.c
+++ b/src/test/test_crypto.c
@@ -1082,6 +1082,7 @@ test_crypto_curve25519_persist(void *arg)
   tt_int_op(0,==,curve25519_keypair_write_to_file(&keypair, fname, "testing"));
   tt_int_op(0,==,curve25519_keypair_read_from_file(&keypair2, &tag, fname));
   tt_str_op(tag,==,"testing");
+  tor_free(tag);
 
   test_memeq(keypair.pubkey.public_key,
              keypair2.pubkey.public_key,
@@ -1109,6 +1110,7 @@ test_crypto_curve25519_persist(void *arg)
   fname = tor_strdup(get_fname("bogus_keypair"));
 
   tt_int_op(-1, ==, curve25519_keypair_read_from_file(&keypair2, &tag, fname));
+  tor_free(tag);
 
   content[69] ^= 0xff;
   tt_int_op(0, ==, write_bytes_to_file(fname, content, st.st_size, 1));
@@ -1117,6 +1119,7 @@ test_crypto_curve25519_persist(void *arg)
  done:
   tor_free(fname);
   tor_free(content);
+  tor_free(tag);
 }
 
 #endif
diff --git a/src/test/test_ntor_cl.c b/src/test/test_ntor_cl.c
index d16b088a..f2b7a72 100644
--- a/src/test/test_ntor_cl.c
+++ b/src/test/test_ntor_cl.c
@@ -43,13 +43,11 @@ client1(int argc, char **argv)
   /* client1 nodeID B -> msg state */
   curve25519_public_key_t B;
   uint8_t node_id[DIGEST_LEN];
-  ntor_handshake_state_t *state;
+  ntor_handshake_state_t *state = NULL;
   uint8_t msg[NTOR_ONIONSKIN_LEN];
 
   char buf[1024];
 
-  memset(&state, 0, sizeof(state));
-
   N_ARGS(4);
   BASE16(2, node_id, DIGEST_LEN);
   BASE16(3, B.public_key, CURVE25519_PUBKEY_LEN);
@@ -63,6 +61,7 @@ client1(int argc, char **argv)
   printf("%s\n", buf);
   base16_encode(buf, sizeof(buf), (void*)state, sizeof(*state));
   printf("%s\n", buf);
+
   ntor_handshake_state_free(state);
   return 0;
 }
@@ -77,8 +76,9 @@ server1(int argc, char **argv)
   int keybytes;
 
   uint8_t msg_out[NTOR_REPLY_LEN];
-  uint8_t *keys;
-  char *hexkeys;
+  uint8_t *keys = NULL;
+  char *hexkeys = NULL;
+  int result = 0;
 
   char buf[256];
 
@@ -98,7 +98,8 @@ server1(int argc, char **argv)
                                 msg_in, keymap, NULL, node_id, msg_out, keys,
                                 (size_t)keybytes)<0) {
     fprintf(stderr, "handshake failed");
-    return 2;
+    result = 2;
+    goto done;
   }
 
   base16_encode(buf, sizeof(buf), (const char*)msg_out, sizeof(msg_out));
@@ -106,9 +107,10 @@ server1(int argc, char **argv)
   base16_encode(hexkeys, keybytes*2+1, (const char*)keys, keybytes);
   printf("%s\n", hexkeys);
 
+ done:
   tor_free(keys);
   tor_free(hexkeys);
-  return 0;
+  return result;
 }
 
 static int
@@ -119,6 +121,7 @@ client2(int argc, char **argv)
   int keybytes;
   uint8_t *keys;
   char *hexkeys;
+  int result = 0;
 
   N_ARGS(5);
   BASE16(2, (&state), sizeof(state));
@@ -129,16 +132,17 @@ client2(int argc, char **argv)
   hexkeys = tor_malloc(keybytes*2+1);
   if (onion_skin_ntor_client_handshake(&state, msg, keys, keybytes)<0) {
     fprintf(stderr, "handshake failed");
-    return 2;
+    result = 2;
+    goto done;
   }
 
   base16_encode(hexkeys, keybytes*2+1, (const char*)keys, keybytes);
   printf("%s\n", hexkeys);
 
+ done:
   tor_free(keys);
   tor_free(hexkeys);
-
-  return 0;
+  return result;
 }
 
 int
diff --git a/src/test/test_util.c b/src/test/test_util.c
index a66941b..b7a69b2 100644
--- a/src/test/test_util.c
+++ b/src/test/test_util.c
@@ -2724,7 +2724,7 @@ test_util_join_win_cmdline(void *ptr)
   }
 
  done:
-  ;
+  tor_free(joined_argv);
 }
 
 #define MAX_SPLIT_LINE_COUNT 4





More information about the tor-commits mailing list