From 6b6d003f43cbbf01b40cedb0cc12ada2e81461f9 Mon Sep 17 00:00:00 2001 From: rl1987 Date: Wed, 21 Feb 2018 20:23:21 +0100 Subject: Don't explode on NULL or empty string --- src/common/util.c | 9 +++++++-- src/test/test_util.c | 13 +++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/common/util.c b/src/common/util.c index 1402462fb0..53e117f24c 100644 --- a/src/common/util.c +++ b/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); diff --git a/src/test/test_util.c b/src/test/test_util.c index ee9b16494c..c734426a5a 100644 --- a/src/test/test_util.c +++ b/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), -- cgit v1.2.3-54-g00ecf