[tor-commits] [tor/master] Cover the error cases of parsing protocol versions

nickm at torproject.org nickm at torproject.org
Mon Sep 26 18:03:40 UTC 2016


commit a232161f7beeecebf31b2259a571e8b26cb0b541
Author: Nick Mathewson <nickm at torproject.org>
Date:   Tue Aug 23 14:02:48 2016 -0400

    Cover the error cases of parsing protocol versions
    
    Also, detect an additional failure type. Thanks, tests!
    
    (How distinctly I recall thee)
---
 src/or/protover.c        | 12 +++++++++---
 src/test/test_protover.c | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/src/or/protover.c b/src/or/protover.c
index 3a2d401..2bde02c 100644
--- a/src/or/protover.c
+++ b/src/or/protover.c
@@ -39,8 +39,10 @@ protocol_type_to_str(protocol_type_t pr)
     if (PROTOCOL_NAMES[i].protover_type == pr)
       return PROTOCOL_NAMES[i].name;
   }
+  /* LCOV_EXCL_START */
   tor_assert_nonfatal_unreached_once();
   return "UNKNOWN";
+  /* LCOV_EXCL_STOP */
 }
 
 /**
@@ -150,6 +152,10 @@ parse_single_entry(const char *s, const char *end_of_entry)
   if (!equals)
     goto error;
 
+  /* The name must be nonempty */
+  if (equals == s)
+    goto error;
+
   out->name = tor_strndup(s, equals-s);
 
   tor_assert(equals < end_of_entry);
@@ -388,7 +394,7 @@ contract_protocol_list(const smartlist_t *proto_strings)
   SMARTLIST_FOREACH_BEGIN(proto_strings, const char *, s) {
     proto_entry_t *ent = parse_single_entry(s, s+strlen(s));
     if (BUG(!ent))
-      continue;
+      continue; // LCOV_EXCL_LINE
     smartlist_t *lst = strmap_get(entry_lists_by_name, ent->name);
     if (!lst) {
       smartlist_add(all_names, ent->name);
@@ -591,11 +597,11 @@ protocol_list_contains(const smartlist_t *protos,
                        protocol_type_t pr, uint32_t ver)
 {
   if (BUG(protos == NULL)) {
-    return 0;
+    return 0; // LCOV_EXCL_LINE
   }
   const char *pr_name = protocol_type_to_str(pr);
   if (BUG(pr_name == NULL)) {
-    return 0;
+    return 0; // LCOV_EXCL_LINE
   }
 
   SMARTLIST_FOREACH_BEGIN(protos, const proto_entry_t *, ent) {
diff --git a/src/test/test_protover.c b/src/test/test_protover.c
index 651d2fd..c86c3b2 100644
--- a/src/test/test_protover.c
+++ b/src/test/test_protover.c
@@ -81,6 +81,41 @@ test_protover_parse(void *arg)
 }
 
 static void
+test_protover_parse_fail(void *arg)
+{
+  (void)arg;
+  smartlist_t *elts;
+
+  /* random junk */
+  elts = parse_protocol_list("!!3@*");
+  tt_assert(elts == NULL);
+
+  /* Missing equals sign in an entry */
+  elts = parse_protocol_list("Link=4 Haprauxymatyve Desc=9");
+  tt_assert(elts == NULL);
+
+  /* Missing word. */
+  elts = parse_protocol_list("Link=4 =3 Desc=9");
+  tt_assert(elts == NULL);
+
+  /* Broken numbers */
+  elts = parse_protocol_list("Link=fred");
+  tt_assert(elts == NULL);
+  elts = parse_protocol_list("Link=1,fred");
+  tt_assert(elts == NULL);
+  elts = parse_protocol_list("Link=1,fred,3");
+  tt_assert(elts == NULL);
+
+  /* Broken range */
+  elts = parse_protocol_list("Link=1,9-8,3");
+  tt_assert(elts == NULL);
+
+ done:
+  ;
+}
+
+
+static void
 test_protover_vote(void *arg)
 {
   (void) arg;
@@ -154,6 +189,7 @@ test_protover_all_supported(void *arg)
 
 struct testcase_t protover_tests[] = {
   PV_TEST(parse, 0),
+  PV_TEST(parse_fail, 0),
   PV_TEST(vote, 0),
   PV_TEST(all_supported, 0),
   END_OF_TESTCASES





More information about the tor-commits mailing list