Browse Source

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.
Nick Mathewson 11 years ago
parent
commit
717946089b
5 changed files with 21 additions and 11 deletions
  1. 2 0
      src/test/bench.c
  2. 1 0
      src/test/test.c
  3. 3 0
      src/test/test_crypto.c
  4. 14 10
      src/test/test_ntor_cl.c
  5. 1 1
      src/test/test_util.c

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

+ 1 - 0
src/test/test.c

@@ -1789,6 +1789,7 @@ test_geoip(void)
 
  done:
   tor_free(s);
+  tor_free(v);
 }
 
 /** Run unit tests for stats code. */

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

+ 14 - 10
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

+ 1 - 1
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