From: Richard Russon Date: Sun, 26 May 2019 15:02:23 +0000 (+0100) Subject: test: improve test_mutt_str_inline_replace() X-Git-Tag: 2019-10-25~187^2~32 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eab887fb313f8ac0cb7a37a10cef2dec0475c0bb;p=neomutt test: improve test_mutt_str_inline_replace() --- diff --git a/mutt/string.c b/mutt/string.c index 203007e20..d372fa87e 100644 --- a/mutt/string.c +++ b/mutt/string.c @@ -1115,6 +1115,9 @@ bool mutt_str_inline_replace(char *buf, size_t buflen, size_t xlen, const char * size_t slen = mutt_str_strlen(buf + xlen); size_t rlen = mutt_str_strlen(rstr); + if ((slen + rlen) >= buflen) + return false; + memmove(buf + rlen, buf + xlen, slen + 1); memmove(buf, rstr, rlen); diff --git a/test/string/mutt_str_inline_replace.c b/test/string/mutt_str_inline_replace.c index 1c275e738..8fcebd148 100644 --- a/test/string/mutt_str_inline_replace.c +++ b/test/string/mutt_str_inline_replace.c @@ -25,6 +25,15 @@ #include "config.h" #include "mutt/mutt.h" +struct InlineReplaceTest +{ + const char *initial; + int replace_len; + const char *replace; + const char *expected; + bool success; +}; + void test_mutt_str_inline_replace(void) { // bool mutt_str_inline_replace(char *buf, size_t buflen, size_t xlen, const char *rstr); @@ -37,4 +46,36 @@ void test_mutt_str_inline_replace(void) char buf[32] = "banana"; TEST_CHECK(mutt_str_inline_replace(buf, sizeof(buf), 2, NULL) == false); } + + // clang-format off + struct InlineReplaceTest replace_tests[] = + { + { "XXXXbanana", 4, "", "banana", true, }, + { "XXXXbanana", 4, "OO", "OObanana", true, }, + { "XXXXbanana", 4, "OOOO", "OOOObanana", true, }, + { "XXXXbanana", 4, "OOOOOO", "OOOOOObanana", true, }, + { "XXXXbanana", 4, "OOOOOOO", "OOOOOOObanana", true, }, + { "XXXXbanana", 4, "OOOOOOOO", "OOOOOOOObanan", false, }, + { "XXXXbanana", 4, "OOOOOOOOO", "OOOOOOOOObana", false, }, + }; + // clang-format on + + { + char buf[14]; + for (size_t i = 0; i < mutt_array_size(replace_tests); i++) + { + struct InlineReplaceTest *t = &replace_tests[i]; + TEST_CASE_("'%s', %d, '%s'", t->initial, t->replace_len, t->replace); + + memset(buf, 0, sizeof(buf)); + + strncpy(buf, t->initial, sizeof(buf)); + bool result = mutt_str_inline_replace(buf, sizeof(buf), t->replace_len, t->replace); + TEST_CHECK(result == t->success); + if (result) + TEST_CHECK(strcmp(buf, t->expected) == 0); + else + TEST_CHECK(strcmp(buf, t->initial) == 0); + } + } }