From 9eb12e40e2afd0c82c13580947494f2363232720 Mon Sep 17 00:00:00 2001 From: Evan Burkey Date: Sun, 3 Dec 2023 17:52:55 -0800 Subject: [PATCH] even better strings --- src/string.c | 3 +++ tests/tests.c | 8 ++++++++ 2 files changed, 11 insertions(+) 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;