Skip to content

fix(capi): spans pointed past text that wasn't UTF-8 - #129

Merged
jamesconwaydev merged 2 commits into
mainfrom
capi-offsets
Nov 20, 2026
Merged

jamesconwaydev merged 2 commits into
mainfrom
capi-offsets

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Problem

  • C API positions. parsec_parse and parsec_parse_fragment read 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.
  • Offsets near SIZE_MAX. Offsets::to_source and to_decoded in parsec-html-encoding overflowed on a huge offset: a panic in debug builds (reported by the C API as PARSEC_ERR_PANIC) and a wrong answer in release. parsec_line_col passes the caller's offset straight to them.
  • Select callbacks. Nothing stopped a parsec_select callback from changing or freeing the document being walked. The header only asked callers not to.
  • Header gaps. It didn't say that parsec_string_free on 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.
  • Punycode. Encoding and decoding a label take time that grows with the square of its length. A host with tens of thousands of distinct characters, or a long xn-- label, took several seconds.

Change

  • With PARSEC_SPANS or PARSEC_ERRORS, a document parsed from text that wasn't all UTF-8 keeps an Offsets map back to the bytes passed in, as parsec_parse_bytes already does. The new Offsets::lossy_utf8 builds that map for String::from_utf8_lossy, which keeps a byte order mark as text. Valid UTF-8 costs nothing extra.
  • to_source and to_decoded saturate.
  • New status PARSEC_ERR_BUSY (15): any change from inside a parsec_select callback returns it (a create function returns PARSEC_NO_NODE). parsec_document_free from a callback frees the document once parsec_select returns. A drop guard keeps the count right if the walk panics.
  • Header preamble and parsec_string_free docs cover the points above. parsec.h defines PARSEC_VERSION, PARSEC_VERSION_MAJOR, _MINOR and _PATCH, through cbindgen's after_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_col with SIZE_MAX on a text and a bytes document; a select callback that tries to remove a node and create one gets PARSEC_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 the capi-checks job.
  • Unit tests: Offsets::lossy_utf8 with a byte order mark; saturation near usize::MAX; the header's version macros match CARGO_PKG_VERSION; every status has a message.
  • crates/parsec-url/tests/limits.rs: the long host and long xn-- label fail in well under a second; a label of exactly MAX_LABEL round-trips.
  • WPT URL: 4,565 of 4,565 pass (parsing 904, setters 284, origin 421, toascii 261, IDNA 2,675, IDNA-removed 20).
  • cargo clippy --workspace --all-targets -- -D warnings, cargo fmt --check, the header matches cbindgen, and it compiles as C++11.

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 foothillsdigital left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked b09e334:

  • Tests: parsec-capi, parsec-html-encoding and parsec-url pass 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 through doc_mut, so its busy check covers them all. That's as_mut() only in doc_mut and the byte-parser functions; the create functions at 1314 return PARSEC_NO_NODE through the same doc_mut failure.
  • The deferred free:
    • The for loop's select_iter borrow ends before _busy drops, so nothing reads the document after Busy::drop frees it.
    • A nested parsec_select counts 1 to 2 to 1 to 0, and only the last frees.
    • A second parsec_document_free from a callback just sets the flag again.
    • A panicking walk still unmarks through the drop guard.
  • 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 when from_utf8_lossy returned Cow::Owned, with spans or errors kept, so valid UTF-8 costs nothing.
  • Saturation: saturating_add in to_source and to_decoded fixes the parsec_line_col overflow without changing in-range answers.
  • Punycode: decode refuses before inserting past MAX_LABEL, and each input character is read once, so a 1 MiB xn-- label stays linear. encode refuses up front. 4,000 code points is far past DNS's 63 bytes.
  • The header: the_header_has_this_version keeps PARSEC_VERSION* in step with Cargo.toml, and the parsec_string_free wording is right.

Approving.

@jamesconwaydev
jamesconwaydev merged commit 94b6ab0 into main Nov 20, 2026
39 checks passed
@jamesconwaydev
jamesconwaydev deleted the capi-offsets branch November 20, 2026 18:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants