瀏覽代碼

Don't explode on NULL or empty string

rl1987 6 年之前
父節點
當前提交
6b6d003f43
共有 2 個文件被更改,包括 20 次插入2 次删除
  1. 7 2
      src/common/util.c
  2. 13 0
      src/test/test_util.c

+ 7 - 2
src/common/util.c

@@ -1081,8 +1081,13 @@ string_is_valid_dest(const char *string)
   int retval;
   size_t len = strlen(string);
 
-  tor_assert(string);
-  tor_assert(len > 0);
+  if (string == NULL)
+    return 0;
+
+  len = strlen(string);
+
+  if (len == 0)
+    return 0;
 
   if (string[0] == '[' && string[len - 1] == ']')
     string = tmp = tor_strndup(string + 1, len - 2);

+ 13 - 0
src/test/test_util.c

@@ -5541,6 +5541,18 @@ test_util_max_mem(void *arg)
   ;
 }
 
+static void
+test_util_dest_validation_edgecase(void *arg)
+{
+  (void)arg;
+
+  tt_assert(!string_is_valid_dest(NULL));
+  tt_assert(!string_is_valid_dest(""));
+
+  done:
+  return;
+}
+
 static void
 test_util_hostname_validation(void *arg)
 {
@@ -6222,6 +6234,7 @@ struct testcase_t util_tests[] = {
     &passthrough_setup, (void*)"1" },
   UTIL_TEST(max_mem, 0),
   UTIL_TEST(hostname_validation, 0),
+  UTIL_TEST(dest_validation_edgecase, 0),
   UTIL_TEST(ipv4_validation, 0),
   UTIL_TEST(writepid, 0),
   UTIL_TEST(get_avail_disk_space, 0),