Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Bugfixes:
* Fixed substring formatting returning the remaining string when a negative end index is before the start of the string. (General)
* Fixed a skipped `{?}` / `{/}` conditional in a format string ending at the `{?}` / `{/}` of a conditional nested in it. (General)
* Fixed numbers in format strings that don't fit in 32 bits wrapping around to a valid argument index, constant index, substring index or length, e.g. `{4294967297}` being read as `{1}`, and a signed integer overflow for `{1:-2147483648}`. (General)
* Fixed substring formatting printing the placeholder as is, instead of nothing, when the start index is outside of the string and an end index is given. (General)

Logos:
* Added Tiny Core Linux (#2655)
Expand Down
23 changes: 15 additions & 8 deletions src/common/impl/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,15 +592,22 @@ static bool parseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, uint3

char* pEnd = nullptr;
int32_t start = strToInt32(pSep + 1, &pEnd);
if (start < 0) {
start = (int32_t) tempString.length + start;
bool hasEnd = *pEnd == ',';
int32_t end = 0;
if (hasEnd) {
end = strToInt32(pEnd + 1, &pEnd);
}
if (start >= 0 && (uint32_t) start < tempString.length) {
if (*pEnd == '\0') {
ffStrbufAppendNS(buffer, tempString.length - (uint32_t) start, &tempString.chars[start]);
} else if (*pEnd == ',') {
int32_t end = strToInt32(pEnd + 1, &pEnd);
if (!*pEnd) {

// Check the whole range before using it, so that a valid range that lies
// outside of the string gives an empty result instead of an invalid placeholder
if (!*pEnd) {
if (start < 0) {
start = (int32_t) tempString.length + start;
}
if (start >= 0 && (uint32_t) start < tempString.length) {
if (!hasEnd) {
ffStrbufAppendNS(buffer, tempString.length - (uint32_t) start, &tempString.chars[start]);
} else {
if (end < 0) {
end = (int32_t) tempString.length + end;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/format.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ int main(void) {
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()");
VERIFY("output({1~11,12})", "12345 67890", "output()"); // Same as {1~11}
VERIFY("output({1~20,30})", "12345 67890", "output()"); // Same as {1~20}
VERIFY("output({1~-50,3})", "12345 67890", "output()"); // Same as {1~-50}
VERIFY("output({1~-50,-1})", "12345 67890", "output()"); // Same as {1~-50}
VERIFY("output({1~})", "12345 67890", "output(12345 67890)"); // Same as {1~0}
VERIFY("output({1~-0})", "12345 67890", "output(12345 67890)"); // Same as {1~0}
VERIFY("output({1~,-1})", "12345 67890", "output(12345 6789)"); // Same as {1~0,-1}
Expand Down
Loading