diff options
author | Tobias Brunner <tobias@strongswan.org> | 2016-04-01 19:03:22 +0200 |
---|---|---|
committer | Tobias Brunner <tobias@strongswan.org> | 2016-04-04 10:43:46 +0200 |
commit | 85597f2983c81ef1a4522020c885f1431cb6b2aa (patch) | |
tree | 2473b5627ff96226dd680bccdbf741a5a768eeb4 /src | |
parent | 90c8cf6819a9d68ba945beb02a03ee52364c3a74 (diff) | |
download | strongswan-85597f2983c81ef1a4522020c885f1431cb6b2aa.tar.bz2 strongswan-85597f2983c81ef1a4522020c885f1431cb6b2aa.tar.xz |
string: Gracefully handle NULL in str*eq() macros
Diffstat (limited to 'src')
-rw-r--r-- | src/libstrongswan/tests/suites/test_utils.c | 78 | ||||
-rw-r--r-- | src/libstrongswan/utils/utils/string.h | 8 |
2 files changed, 82 insertions, 4 deletions
diff --git a/src/libstrongswan/tests/suites/test_utils.c b/src/libstrongswan/tests/suites/test_utils.c index 504562615..1eb3c8bc3 100644 --- a/src/libstrongswan/tests/suites/test_utils.c +++ b/src/libstrongswan/tests/suites/test_utils.c @@ -197,6 +197,82 @@ START_TEST(test_round) END_TEST /******************************************************************************* + * streq + */ + +static struct { + char *a; + char *b; + bool eq; + bool case_eq; +} streq_data[] = { + {NULL, NULL, TRUE, TRUE}, + {NULL, "", FALSE, FALSE}, + {"", NULL, FALSE, FALSE}, + {"abc", "", FALSE, FALSE}, + {"abc", "abc", TRUE, TRUE}, + {"abc", "ABC", FALSE, TRUE}, +}; + +START_TEST(test_streq) +{ + bool eq; + + ck_assert(streq(streq_data[_i].a, streq_data[_i].a)); + ck_assert(streq(streq_data[_i].b, streq_data[_i].b)); + eq = streq(streq_data[_i].a, streq_data[_i].b); + ck_assert(eq == streq_data[_i].eq); + + ck_assert(strcaseeq(streq_data[_i].a, streq_data[_i].a)); + ck_assert(strcaseeq(streq_data[_i].b, streq_data[_i].b)); + eq = strcaseeq(streq_data[_i].a, streq_data[_i].b); + ck_assert(eq == streq_data[_i].case_eq); +} +END_TEST + +/******************************************************************************* + * strneq + */ + +static struct { + char *a; + char *b; + size_t n; + bool eq; + bool case_eq; +} strneq_data[] = { + {NULL, NULL, 0, TRUE, TRUE}, + {NULL, NULL, 10, TRUE, TRUE}, + {NULL, "", 0, FALSE, FALSE}, + {"", NULL, 0, FALSE, FALSE}, + {"abc", "", 0, TRUE, TRUE}, + {"abc", "", 1, FALSE, FALSE}, + {"abc", "ab", 1, TRUE, TRUE}, + {"abc", "ab", 2, TRUE, TRUE}, + {"abc", "ab", 3, FALSE, FALSE}, + {"abc", "abc", 3, TRUE, TRUE}, + {"abc", "abc", 4, TRUE, TRUE}, + {"abc", "abC", 2, TRUE, TRUE}, + {"abc", "abC", 3, FALSE, TRUE}, +}; + +START_TEST(test_strneq) +{ + bool eq; + + ck_assert(strneq(strneq_data[_i].a, strneq_data[_i].a, strneq_data[_i].n)); + ck_assert(strneq(strneq_data[_i].b, strneq_data[_i].b, strneq_data[_i].n)); + eq = strneq(strneq_data[_i].a, strneq_data[_i].b, strneq_data[_i].n); + ck_assert(eq == strneq_data[_i].eq); + + ck_assert(strncaseeq(strneq_data[_i].a, strneq_data[_i].a, strneq_data[_i].n)); + ck_assert(strncaseeq(strneq_data[_i].b, strneq_data[_i].b, strneq_data[_i].n)); + eq = strncaseeq(strneq_data[_i].a, strneq_data[_i].b, strneq_data[_i].n); + ck_assert(eq == strneq_data[_i].case_eq); +} +END_TEST + +/******************************************************************************* * strpfx */ @@ -848,6 +924,8 @@ Suite *utils_suite_create() suite_add_tcase(s, tc); tc = tcase_create("string helper"); + tcase_add_loop_test(tc, test_streq, 0, countof(streq_data)); + tcase_add_loop_test(tc, test_strneq, 0, countof(strneq_data)); tcase_add_loop_test(tc, test_strpfx, 0, countof(strpfx_data)); suite_add_tcase(s, tc); diff --git a/src/libstrongswan/utils/utils/string.h b/src/libstrongswan/utils/utils/string.h index 60eaaae22..562516b91 100644 --- a/src/libstrongswan/utils/utils/string.h +++ b/src/libstrongswan/utils/utils/string.h @@ -27,7 +27,7 @@ */ static inline bool streq(const char *x, const char *y) { - return strcmp(x, y) == 0; + return (x == y) || (x && y && strcmp(x, y) == 0); } /** @@ -35,7 +35,7 @@ static inline bool streq(const char *x, const char *y) */ static inline bool strneq(const char *x, const char *y, size_t len) { - return strncmp(x, y, len) == 0; + return (x == y) || (x && y && strncmp(x, y, len) == 0); } /** @@ -51,7 +51,7 @@ static inline bool strpfx(const char *x, const char *prefix) */ static inline bool strcaseeq(const char *x, const char *y) { - return strcasecmp(x, y) == 0; + return (x == y) || (x && y && strcasecmp(x, y) == 0); } /** @@ -59,7 +59,7 @@ static inline bool strcaseeq(const char *x, const char *y) */ static inline bool strncaseeq(const char *x, const char *y, size_t len) { - return strncasecmp(x, y, len) == 0; + return (x == y) || (x && y && strncasecmp(x, y, len) == 0); } /** |