Skip to content

Format: fixes substring with an end index outside of the string - #2695

Merged
CarterLi merged 1 commit into
fastfetch-cli:devfrom
arxhr007:fix/format-substring-out-of-range
Oct 31, 2026
Merged

CarterLi merged 1 commit into
fastfetch-cli:devfrom
arxhr007:fix/format-substring-out-of-range

Conversation

@arxhr007

Copy link
Copy Markdown
Contributor

Summary

A valid substring placeholder with an end index, {1~start,end}, is printed as is when the start index is outside of the string. The same placeholder without the end index prints nothing, and so do {1~10,10} and {1~10,5}, which are covered by the existing tests.

With 12345 67890 (11 characters):

Format Before After
{1~20} `` (empty) `` (empty)
{1~11,12} {1~11,12} `` (empty)
{1~20,30} {1~20,30} `` (empty)
{1~-50,3} {1~-50,3} `` (empty)
{1~-50,-1} {1~-50,-1} `` (empty)

The cause is that the ,end part was only parsed inside if (start >= 0 && start < length). When the start was outside, the parser pointer stayed on the ,, and the check for trailing characters then took the valid placeholder for an invalid one. The existing tests for invalid syntax ({1~-,1}, {1~0,,1}, {1~0,1,} and so on) still print the placeholder as is.

Related issue (required for new logos for new distros)

Not a logo. No issue, it is a small fix in the same code as #2669.

Changes

  • src/common/impl/format.c: parse the optional end index first and check the whole range before using it. A valid range outside of the string now gives an empty result.
  • tests/format.c: four cases for a start index outside of the string with an end index. The first fails on the old code.
  • CHANGELOG.md: bugfix entry.

Screenshots

Not a visual change.

Checklist

  • I have tested my changes locally.

Built with -DBUILD_TESTS=On -DENABLE_ASAN=On and -fsanitize=undefined; all 19 tests pass.

@CarterLi

Copy link
Copy Markdown
Member

Conflict

`{1~20,30}` printed the placeholder as is, because the `,end` part was only
checked when the start index was inside the string. `{1~20}` printed nothing.
Parse and check the whole range first, so that a valid range that lies outside
of the string gives an empty result.
@arxhr007
arxhr007 force-pushed the fix/format-substring-out-of-range branch from d0f7ded to 7f2faa7 Compare October 31, 2026 14:36
@arxhr007

Copy link
Copy Markdown
Contributor Author

Rebased onto dev. The conflict was with #2696: both touch the ~ substring parsing and the changelog. I kept the range check from this PR and made it use the new strToInt32(). The new cases in tests/format.c fail on current dev and pass with this change; all tests pass with ASan and UBSan.

@CarterLi
CarterLi merged commit d697197 into fastfetch-cli:dev Oct 31, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants