From 18f04da4ec38328e43b1c7acbb8a11b08c8b92c7 Mon Sep 17 00:00:00 2001 From: Patrick Pelissier Date: Sat, 10 Sep 2022 16:19:58 +0200 Subject: [PATCH] Robustness/fix: string_next won't advance if the end of string is reached. --- m-string.h | 4 +++- tests/test-mstring.c | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/m-string.h b/m-string.h index 4e77ed1c..9d80e90f 100644 --- a/m-string.h +++ b/m-string.h @@ -1787,8 +1787,10 @@ static inline bool m_string_end_p (m_string_it_t it) { M_STR1NG_IT_CONTRACT(it); - if (M_UNLIKELY (*it->ptr == 0)) + if (M_UNLIKELY (*it->ptr == 0)) { + it->next_ptr = 0; // For safety, don't advance iterator over end of string return true; + } m_str1ng_utf8_state_e state = M_STR1NG_UTF8_STARTING; m_string_unicode_t u = 0; const char *str = it->ptr; diff --git a/tests/test-mstring.c b/tests/test-mstring.c index 7c27808d..d5c53df8 100644 --- a/tests/test-mstring.c +++ b/tests/test-mstring.c @@ -85,6 +85,8 @@ static void test_utf8_it(void) string_init(s); string_it_end(it2, s); assert( string_it_get_pos(it2) == 0); + string_next(it2); + assert( string_it_get_pos(it2) == 0); for(string_it(it, s) ; !string_end_p(it); string_next(it)) { assert(0); // Shall not be called @@ -106,6 +108,9 @@ static void test_utf8_it(void) string_it_pos(it2, s, 0); assert( !string_end_p(it2) ); assert( string_get_cref(it2) == 45); + string_next(it); + assert( string_it_get_pos(it) == 9); + assert( string_end_p(it) ); string_set_str (s, "H€llo René Chaînôr¬"); assert(string_length_u(s) == 19);