fix(file_ops): Korean/CJK UTF-8 files falsely detected as binary - #80957
Open
copybara-agent wants to merge 1 commit into
Open
fix(file_ops): Korean/CJK UTF-8 files falsely detected as binary#80957copybara-agent wants to merge 1 commit into
copybara-agent wants to merge 1 commit into
Conversation
head -c 1000 samples by byte count, so a 3-byte Korean character split at byte 999 causes the terminal decoder (errors='replace') to inject a spurious U+FFFD. _is_likely_binary() previously treated any U+FFFD as proof of non-UTF-8 content. Fix: when U+FFFD is found, verify the real file with python3 using strict UTF-8 decoding before declaring it binary. Valid UTF-8 files (including Korean/CJK markdown) now read correctly. Adds three regression tests: - test_cjk_utf8_byte_boundary_not_flagged_binary (the new case) - test_real_latin1_file_still_flagged_binary (guard against regression) - existing tests still pass (4/4)
Contributor
|
This was generated by AI during triage. Summary: Problems:
Solution: Checked against |
23 tasks
This was referenced Aug 12, 2026
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.
Problem
head -c 1000samples by byte count. A Korean/CJK character (3 bytes each) split at byte 999 causes the terminal decoder (errors='replace') to inject a spuriousU+FFFD._is_likely_binary()previously treated anyU+FFFDas proof of non-UTF-8 content, causing valid Korean markdown files to be rejected withBinary file — cannot display as text.High-byte ratio for Korean UTF-8 is ~71%, which also ruled out the non-printable ratio path.
Fix
When
U+FFFDis detected in the sample, verify the real file viapython3with strict UTF-8 decoding before declaring it binary. Valid UTF-8 files (Korean, CJK, etc.) now pass through correctly.Tests
Three regression tests added to
TestReadNonUtf8IsBinary:test_cjk_utf8_byte_boundary_not_flagged_binary— 400×'가' (1200 bytes); 1000 mod 3=1 guarantees a split → spurious FFFD → must still read as texttest_real_latin1_file_still_flagged_binary— real latin-1 file must still be caught