Skip to content

🐛 fix(encoding): keep GBK alive past a pushback - #2487

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/detect-gbk-tld
Nov 18, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/detect-gbk-tld

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

When a GB18030 four-byte sequence fails on its third or fourth byte, the decoder pushes the unread bytes back. chardetng's GBK candidate takes the single-byte-extension penalty at that point and resets its decoder, so those bytes are never read again; turbohtml resumed at the pushed-back bytes, re-read them as a fresh lead plus an invalid byte, and could charge the penalty twice or drop the candidate. Under .cn, b'\xe26\x90\xff0\xa3\x89' detected as IBM866 where chardetng answers GBK.

Both of chardetng's GBK penalty rules (an invalid 0xFF, and a 0xA0/0xFD/0xFE third byte followed by ASCII or 0xFF) now resume after the byte that ends the sequence, as chardetng does.

Against the vendored chardetng oracle: 40,000 random GBK-shaped inputs, main matches 36,901 and this branch 36,923 (22 fixed); 30,000 inputs focused on the third-byte rule, main 29,904 and this branch 30,000 (96 fixed). No answer that matched chardetng changes. Pinned cases run one-shot and streamed at chunk sizes 1, 2, 3 and 16.

@gaborbernat gaborbernat added the bug Something isn't working label Nov 18, 2026
On a four-byte GB18030 prefix whose fourth byte is invalid, the decoder
pushes the rest of the sequence back, the fourth byte included. chardetng
applies a single-byte-extension penalty at that byte and resets its decoder,
so the pushed-back bytes are never read again. turbohtml resumed at them. A
0xFF read again after a lead killed the candidate, and an ASCII byte after a
0xA0 or 0xFE lead took the penalty twice.

    b"\xe26\x90\xff0\xa3\x89"            chardetng: GBK  before: IBM866
    <GBK text> 81 36 fe ff <GBK text>    chardetng: GBK  before: windows-1252

Both penalty rules now resume after the byte that ends the sequence. A lead
followed directly by a 0xFF stays dead, as chardetng scores it.
@gaborbernat
gaborbernat merged commit f3355b8 into tox-dev:main Nov 18, 2026
45 of 47 checks passed
@codspeed

codspeed Bot commented Nov 18, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 5.61%

2 warnings

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 1 benchmark with a significant change ran in a different software environment

Its base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the change might come from this difference rather than from the code. Open the benchmark to see what changed.

See affected benchmarks

⚡ 1 improved benchmark
✅ 579 untouched benchmarks
⏩ 91 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[serialize-named-small] 861.7 µs 815.9 µs +5.61%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:fix/detect-gbk-tld (cdd44bb) with main (0c3358e)

Open in CodSpeed

Footnotes

  1. 91 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

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant