stat: truncate string precision on a byte boundary to avoid a panic - #13752
Open
leeewee wants to merge 1 commit into
Open
stat: truncate string precision on a byte boundary to avoid a panic#13752leeewee wants to merge 1 commit into
leeewee wants to merge 1 commit into
Conversation
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.
Merging this PR will degrade performance by 18.29%
|
| 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)
Footnotes
-
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. ↩
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #13750
stat'sprint_strapplied a%.Pprecision by slicing the&strvalue(
&s[..p]). When the precisionPlands inside a multibyte UTF-8 character —e.g.
stat -c '%.9n'on a file whose name ends in a two-byte character — theslice is not on a char boundary and
statpanics and aborts (exit 134). GNUstattruncates by bytes and exits 0.Fix
Route
print_strthrough the existingwrite_padded_byteshelper — the same oneprint_os_stralready uses on Unix — so truncation and padding operate on bytes.The output now matches GNU
statbyte-for-byte (the partial byte is emitted, asGNU does) instead of crashing.
Test
Adds
test_precision_splits_multibyte_char_in_value, which asserts that aprecision splitting a multibyte character truncates on the byte and succeeds.