diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b2aa5c8b..c22d2ef91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Bugfixes: * Improved uptime detection on SunOS via kstat. (#2674, Uptime, SunOS) +* Fixed substring formatting returning the remaining string when a negative end index is before the start of the string. (General) # 2.70.0 diff --git a/src/common/impl/format.c b/src/common/impl/format.c index b3b8ab190..6a9aa8c53 100644 --- a/src/common/impl/format.c +++ b/src/common/impl/format.c @@ -552,7 +552,7 @@ static bool parseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, uint3 if (end < 0) { end = (int32_t) tempString.length + end; } - if ((uint32_t) end > tempString.length) { + if (end >= 0 && (uint32_t) end > tempString.length) { end = (int32_t) tempString.length; } if (end > start) { diff --git a/tests/format.c b/tests/format.c index 374fd48df..0858c967b 100644 --- a/tests/format.c +++ b/tests/format.c @@ -96,6 +96,7 @@ int main(void) { VERIFY("output({1~10,10})", "12345 67890", "output()"); VERIFY("output({1~10,5})", "12345 67890", "output()"); VERIFY("output({1~3,-3})", "12345 67890", "output(45 67)"); + VERIFY("output({1~0,-12})", "12345 67890", "output()"); VERIFY("output({1~-5,-3})", "12345 67890", "output(67)"); VERIFY("output({1~-0,10})", "12345 67890", "output(12345 6789)"); VERIFY("output({1~-3,-5})", "12345 67890", "output()");