diff --git a/src/string.c b/src/string.c index f5b62ab..22bf91f 100644 --- a/src/string.c +++ b/src/string.c @@ -12,6 +12,9 @@ int find_substrings(const char* haystack, const char* needle, size_t *num_substr size_t sz_h = strlen(haystack); size_t sz_n = strlen(needle); + if ((int)sz_h - (int)sz_n < 0) { + return 0; + } *num_substrings = 0; for (size_t i = 0; i <= sz_h - sz_n; ++i) { diff --git a/tests/tests.c b/tests/tests.c index 5424fba..eed95c1 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -285,6 +285,14 @@ void test_string() { subs = NULL; find_substrings("test one two", "nope", &sub_sz, &subs); + assert(sub_sz == 0); + assert(subs == NULL); + free(subs); + subs = NULL; + + find_substrings("123", "nopes", &sub_sz, &subs); + assert(sub_sz == 0); + assert(subs == NULL); free(subs); subs = NULL;