fix: prevent panic on multi-byte UTF-8 in log string slicing#251
Open
hansmrtn wants to merge 1 commit intoyfedoseev:mainfrom
Open
fix: prevent panic on multi-byte UTF-8 in log string slicing#251hansmrtn wants to merge 1 commit intoyfedoseev:mainfrom
hansmrtn wants to merge 1 commit intoyfedoseev:mainfrom
Conversation
Replace raw byte-offset string slicing (&s[..n], &s[n..]) with safe_prefix/safe_suffix helpers that respect UTF-8 char boundaries. Fixes 4 sites in text.rs (lines 2961, 2963, 5697) and xref.rs (lines 391, 421) that panic with 'byte index N is not a char boundary' when processing PDFs with multi-byte UTF-8 text and log level >= Debug.
|
Oh nice i ran into this too, i had a fix with |
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.
Description
This fixes the 5 byte-offset string slices in
log::debug!/log::trace!macros that panic withbyte index N is not a char boundaryon multi-byte UTF-8 text (CJK, emoji, dingbats, U+FFFD). This triggers in any app usingtracing-subscriber. I found this while working with maintenance manuals, causing my logs to fill with panics.Type of Change
Related Issues
N/A
Changes Made
safe_prefix/safe_suffixtopub(crate) utils(floor/ceil char boundary truncation)&s[..n]/&s[n..]attext.rs:2961,2963,5697andxref.rs:391,421catch_unwindproof of old panics, per-site regression, edge cases[Unreleased]entry to CHANGELOG.mdTesting
cargo test --all-featurescargo clippy -- -D warningscargo fmtNote, on my machine,
--all-featuresfails to link due to missing ONNX Runtime (ml/ocrfeatures). I did test with all linkable features:logging,parallel,debug-span-merging,rendering,signatures,barcodes.Python Bindings (if applicable)
N/A — no Python binding changes.
ruff formatruff checkDocumentation
Checklist
feat:,fix:,docs:)Screenshots (if applicable)
N/A
Additional Notes
The bug only manifests when
log::max_level() >= Debug, which happens automatically in apps usingtracing-subscriberwithtracing-log. ASCII-only PDFs are unaffected since byte offsets always land on char boundaries.Let me know if any changes or tests are needed or missed!