Repository navigation
fix(capi): spans pointed past text that wasn't UTF-8 - #129
Merged
Merged
Conversation
Encoding and decoding a label take time that grows with the square of its length, and nothing bounded it: a host of tens of thousands of distinct characters, or a long `xn--` label, took several seconds. A label over punycode::MAX_LABEL (4,000 code points) now fails, as one whose arithmetic overflows does. DNS labels are 63 bytes at most. The WPT URL suites pass as before, all 4,565 cases.
parsec_parse reads bytes that aren't UTF-8 as U+FFFD, which takes three bytes where the bad byte took one, and spans and error offsets counted in that copy. They could point at the wrong bytes, or past the end of the caller's buffer. With PARSEC_SPANS or PARSEC_ERRORS the document now keeps offsets back to the bytes passed in, as parsec_parse_bytes does, from Offsets::lossy_utf8, which counts a byte order mark as text. Offsets::to_source and to_decoded overflowed on an offset near SIZE_MAX, a panic in debug builds and a wrong answer in release; they saturate. A parsec_select callback could change or free the document it was walking. A change from there now fails with the new PARSEC_ERR_BUSY, and parsec_document_free from there frees it when parsec_select returns. The header says that freeing a borrowed string, or one twice, is undefined behaviour, and that running out of stack or memory ends the process; and it defines PARSEC_VERSION and its parts, which a test keeps in step with Cargo.toml.
foothillsdigital
approved these changes
Nov 20, 2026
foothillsdigital
left a comment
Collaborator
There was a problem hiding this comment.
Checked b09e334:
- Tests:
parsec-capi,parsec-html-encodingandparsec-urlpass locally, including the C program, once the html5lib submodule is checked out. All 38 CI checks pass. PARSEC_ERR_BUSY: every function that mutates a document goes throughdoc_mut, so its busy check covers them all. That'sas_mut()only indoc_mutand the byte-parser functions; the create functions at 1314 returnPARSEC_NO_NODEthrough the samedoc_mutfailure.- The deferred free:
- The
forloop'sselect_iterborrow ends before_busydrops, so nothing reads the document afterBusy::dropfrees it. - A nested
parsec_selectcounts 1 to 2 to 1 to 0, and only the last frees. - A second
parsec_document_freefrom a callback just sets the flag again. - A panicking walk still unmarks through the drop guard.
- The
Offsets::lossy_utf8: a BOM stays three bytes of U+FEFF, so it maps as identity, and only replaced runs add points. The map is built only whenfrom_utf8_lossyreturnedCow::Owned, with spans or errors kept, so valid UTF-8 costs nothing.- Saturation:
saturating_addinto_sourceandto_decodedfixes theparsec_line_coloverflow without changing in-range answers. - Punycode:
decoderefuses before inserting pastMAX_LABEL, and each input character is read once, so a 1 MiBxn--label stays linear.encoderefuses up front. 4,000 code points is far past DNS's 63 bytes. - The header:
the_header_has_this_versionkeepsPARSEC_VERSION*in step withCargo.toml, and theparsec_string_freewording is right.
Approving.
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
parsec_parseandparsec_parse_fragmentread bytes that aren't UTF-8 as U+FFFD, which is three bytes where the bad byte was one. Spans and error offsets were counted in that copy, so they could name the wrong bytes of the caller's buffer, or bytes past its end.SIZE_MAX.Offsets::to_sourceandto_decodedin parsec-html-encoding overflowed on a huge offset: a panic in debug builds (reported by the C API asPARSEC_ERR_PANIC) and a wrong answer in release.parsec_line_colpasses the caller's offset straight to them.parsec_selectcallback from changing or freeing the document being walked. The header only asked callers not to.parsec_string_freeon a borrowed string, or twice, is undefined behaviour. It said nothing panics into C, which is true, but read as if nothing could end the process. And there was no version macro to test at compile time.xn--label, took several seconds.Change
PARSEC_SPANSorPARSEC_ERRORS, a document parsed from text that wasn't all UTF-8 keeps anOffsetsmap back to the bytes passed in, asparsec_parse_bytesalready does. The newOffsets::lossy_utf8builds that map forString::from_utf8_lossy, which keeps a byte order mark as text. Valid UTF-8 costs nothing extra.to_sourceandto_decodedsaturate.PARSEC_ERR_BUSY(15): any change from inside aparsec_selectcallback returns it (a create function returnsPARSEC_NO_NODE).parsec_document_freefrom a callback frees the document onceparsec_selectreturns. A drop guard keeps the count right if the walk panics.parsec_string_freedocs cover the points above.parsec.hdefinesPARSEC_VERSION,PARSEC_VERSION_MAJOR,_MINORand_PATCH, through cbindgen'safter_includes.punycode::MAX_LABEL(4,000 code points): a longer label fails to encode, and decoding stops when the output passes it, as an overflow does. DNS labels are 63 bytes at most.Tests
tests/capi.c: spans, attribute spans, end spans and every error offset for input that starts with invalid bytes, checked against the caller's buffer; the same for a fragment and a document that start with a byte order mark;parsec_line_colwithSIZE_MAXon a text and a bytes document; a select callback that tries to remove a node and create one getsPARSEC_ERR_BUSY, then edits work after it returns; a callback that frees the document. All of it passes under AddressSanitizer and UBSan locally, as in thecapi-checksjob.Offsets::lossy_utf8with a byte order mark; saturation nearusize::MAX; the header's version macros matchCARGO_PKG_VERSION; every status has a message.crates/parsec-url/tests/limits.rs: the long host and longxn--label fail in well under a second; a label of exactlyMAX_LABELround-trips.cargo clippy --workspace --all-targets -- -D warnings,cargo fmt --check, the header matches cbindgen, and it compiles as C++11.