Skip to content

stat: truncate string precision on a byte boundary to avoid a panic - #13752

Open
leeewee wants to merge 1 commit into
uutils:mainfrom
leeewee:stat-fix-precision-char-boundary
Open

stat: truncate string precision on a byte boundary to avoid a panic#13752
leeewee wants to merge 1 commit into
uutils:mainfrom
leeewee:stat-fix-precision-char-boundary

Conversation

@leeewee

@leeewee leeewee commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Fixes #13750

stat's print_str applied a %.P precision by slicing the &str value
(&s[..p]). When the precision P lands inside a multibyte UTF-8 character —
e.g. stat -c '%.9n' on a file whose name ends in a two-byte character — the
slice is not on a char boundary and stat panics and aborts (exit 134). GNU
stat truncates by bytes and exits 0.

$ : > "$(printf 'é')" && stat -c '%.1n' "$(printf 'é')"
thread 'main' panicked at src/uu/stat/src/stat.rs:409:50:
end byte index 1 is not a char boundary; it is inside 'é' (bytes 0..2 of string)

Fix

Route print_str through the existing write_padded_bytes helper — the same one
print_os_str already uses on Unix — so truncation and padding operate on bytes.
The output now matches GNU stat byte-for-byte (the partial byte is emitted, as
GNU does) instead of crashing.

Test

Adds test_precision_splits_multibyte_char_in_value, which asserts that a
precision splitting a multibyte character truncates on the byte and succeeds.

print_str applied a `%.P` precision by slicing the &str value (`&s[..p]`),
which panics when p lands inside a multibyte UTF-8 character (e.g. a file
name printed with %n). Route it through write_padded_bytes, as print_os_str
already does, so truncation and padding operate on bytes and match GNU
stat's byte truncation instead of crashing.
@codspeed-hq

codspeed-hq Bot commented Aug 5, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 18.29%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 346 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation df_with_path 571.4 µs 699.3 µs -18.29%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing leeewee:stat-fix-precision-char-boundary (0cb64bb) with main (21d4e96)

Open in CodSpeed

Footnotes

  1. 46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

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.

stat: str char-boundary panic on a %.P precision that truncates a multibyte string value mid-character

1 participant