[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Try to fix new coverity warnings in unit tests.



commit 3b61bdb5aebe3890443e39eed4d79da93aa19c8b
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Wed Sep 5 08:30:35 2018 -0400

    Try to fix new coverity warnings in unit tests.
---
 src/test/bench.c       | 9 ++++++---
 src/test/test_crypto.c | 1 +
 src/test/test_tortls.c | 6 +++++-
 src/test/test_x509.c   | 5 ++++-
 4 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/test/bench.c b/src/test/bench.c
index 359405905..959d4374b 100644
--- a/src/test/bench.c
+++ b/src/test/bench.c
@@ -473,18 +473,19 @@ bench_digest(void)
     for (int i = 0; lens[i] > 0; ++i) {
       reset_perftime();
       start = perftime();
+      int failures = 0;
       for (int j = 0; j < N; ++j) {
         switch (alg) {
           case DIGEST_SHA1:
-            crypto_digest(out, buf, lens[i]);
+            failures += crypto_digest(out, buf, lens[i]) < 0;
             break;
           case DIGEST_SHA256:
           case DIGEST_SHA3_256:
-            crypto_digest256(out, buf, lens[i], alg);
+            failures += crypto_digest256(out, buf, lens[i], alg) < 0;
             break;
           case DIGEST_SHA512:
           case DIGEST_SHA3_512:
-            crypto_digest512(out, buf, lens[i], alg);
+            failures += crypto_digest512(out, buf, lens[i], alg) < 0;
             break;
           default:
             tor_assert(0);
@@ -494,6 +495,8 @@ bench_digest(void)
       printf("%s(%d): %.2f ns per call\n",
              crypto_digest_algorithm_get_name(alg),
              lens[i], NANOCOUNT(start,end,N));
+      if (failures)
+        printf("ERROR: crypto_digest failed %d times.\n", failures);
     }
   }
 }
diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c
index b08c5cbc2..81d2fa6f3 100644
--- a/src/test/test_crypto.c
+++ b/src/test/test_crypto.c
@@ -79,6 +79,7 @@ test_crypto_dh(void *arg)
   dh1_dup = crypto_dh_dup(dh1);
   s1len = crypto_dh_compute_secret(LOG_WARN, dh1_dup, p2, DH1024_KEY_LEN,
                                    s1, 50);
+  tt_i64_op(s1len, OP_GE, 0);
   tt_mem_op(s1,OP_EQ, s2, s1len);
 
   {
diff --git a/src/test/test_tortls.c b/src/test/test_tortls.c
index 7ab4b5c2a..f4315364a 100644
--- a/src/test/test_tortls.c
+++ b/src/test/test_tortls.c
@@ -110,7 +110,11 @@ read_cert_from(const char *str)
 {
   size_t len = strlen(str);
   uint8_t *raw_cert = tor_malloc(len);
-  size_t true_len = pem_decode(raw_cert, len, str, len, "CERTIFICATE");
+  ssize_t true_len = pem_decode(raw_cert, len, str, len, "CERTIFICATE");
+  if (true_len < 0) {
+    tor_free(raw_cert);
+    return NULL;
+  }
   tor_x509_cert_t *cert = tor_x509_cert_decode(raw_cert, true_len);
   tor_free(raw_cert);
   if (! cert) {
diff --git a/src/test/test_x509.c b/src/test/test_x509.c
index 4f814129e..912895849 100644
--- a/src/test/test_x509.c
+++ b/src/test/test_x509.c
@@ -91,9 +91,11 @@ test_x509_consume_ec_cert(void *arg)
     "+FSPvQIhAM7kY9Tlt0ELmyMnORPp1VJieXn/qhL5VoxGxSedTbny\n";
   const time_t now = 1535045321; /* when I'm writing this test. */
   tor_x509_cert_t *cert = cert_from_der64(certificate);
+  crypto_pk_t *key = NULL;
   tt_assert(cert);
 
-  tt_ptr_op(NULL, OP_EQ, tor_tls_cert_get_key(cert));
+  key = tor_tls_cert_get_key(cert);
+  tt_ptr_op(NULL, OP_EQ, key); // Can't get an RSA key out of an EC cert.
 
   /* It's a self-signed cert -- make sure it signed itself. */
   tt_assert(tor_tls_cert_is_valid(LOG_ERR, cert, cert, now, 0));
@@ -105,6 +107,7 @@ test_x509_consume_ec_cert(void *arg)
 
  done:
   tor_x509_cert_free(cert);
+  crypto_pk_free(key);
   teardown_capture_of_logs();
 }
 



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits