Commit 2ea65898 authored by goer's avatar goer Committed by Peter Hutterer

modify macro streq/strneq for check one null pointer

Signed-off-by: goer's avatarweizhixiang <1138871845@qq.com>
Minor modifications applied by Peter Hutterer <peter.hutterer@who-t.net>
parent 53595cb2
Pipeline #260943 passed with stages
in 17 minutes and 58 seconds
......@@ -43,8 +43,23 @@
#include <xlocale.h>
#endif
#define streq(s1, s2) (strcmp((s1), (s2)) == 0)
#define strneq(s1, s2, n) (strncmp((s1), (s2), (n)) == 0)
static inline bool
streq(const char *str1, const char *str2)
{
/* one NULL, one not NULL is always false */
if (str1 && str2)
return strcmp(str1, str2) == 0;
return str1 == str2;
}
static inline bool
strneq(const char *str1, const char *str2, int n)
{
/* one NULL, one not NULL is always false */
if (str1 && str2)
return strncmp(str1, str2, n) == 0;
return str1 == str2;
}
static inline void *
zalloc(size_t size)
......
......@@ -1270,6 +1270,28 @@ START_TEST(strverscmp_test)
}
END_TEST
START_TEST(streq_test)
{
ck_assert(streq("", "") == true);
ck_assert(streq(NULL, NULL) == true);
ck_assert(streq("0.0.1", "") == false);
ck_assert(streq("foo", NULL) == false);
ck_assert(streq(NULL, "foo") == false);
ck_assert(streq("0.0.1", "0.0.1") == true);
}
END_TEST
START_TEST(strneq_test)
{
ck_assert(strneq("", "", 1) == true);
ck_assert(strneq(NULL, NULL, 1) == true);
ck_assert(strneq("0.0.1", "", 6) == false);
ck_assert(strneq("foo", NULL, 5) == false);
ck_assert(strneq(NULL, "foo", 5) == false);
ck_assert(strneq("0.0.1", "0.0.1", 6) == true);
}
END_TEST
static Suite *
litest_utils_suite(void)
{
......@@ -1311,6 +1333,8 @@ litest_utils_suite(void)
tcase_add_test(tc, list_test_insert);
tcase_add_test(tc, list_test_append);
tcase_add_test(tc, strverscmp_test);
tcase_add_test(tc, streq_test);
tcase_add_test(tc, strneq_test);
suite_add_tcase(s, tc);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment