Repository navigation
Make Parser::slice and slice_from sound - #464
samiarif02 wants to merge 1 commit into
Conversation
`Parser::slice` and `Parser::slice_from` are safe, public methods but called `get_unchecked` on the input with a caller-supplied range. Safe code could trigger undefined behavior, e.g. by passing an inverted range `p1..p0`, by reusing a `SourcePosition` after `reset`, or by passing a position from a parser over a longer input (the `debug_assert!`s are gone in release builds). Make the public `slice`/`slice_from` use checked string indexing. Keep the unchecked path for the hot internal tokenizer callers as `pub(crate)` `slice_unchecked`/`slice_from_unchecked`, so there is no bounds/char-boundary overhead where positions are known to be valid. Adds regression tests: a valid range returns the expected substring, and an inverted range now panics instead of invoking UB. Fixes servo#459 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01FwN6idBD37mgcgcdANVSjb
emilio
left a comment
There was a problem hiding this comment.
We also use these APIs in hot paths in Gecko, but hopefully not hot enough for the bounds-checks to be an issue.
| /// https://github.com/servo/rust-cssparser/issues/459 | ||
| /// `Parser::slice` is a safe, public method, so it must not be able to trigger | ||
| /// undefined behavior. A valid range returns the expected substring. | ||
| #[test] | ||
| fn parser_slice_valid_range() { | ||
| let mut parser = Parser::new("abc def"); | ||
| let start = parser.position(); | ||
| parser.next().unwrap(); // consume `abc` | ||
| let end = parser.position(); | ||
| assert_eq!(parser.slice(start..end), "abc"); | ||
| // `slice_from` goes from the given position to the current one. | ||
| assert_eq!(parser.slice_from(start), "abc"); | ||
| } |
There was a problem hiding this comment.
I don't think this test is useful?
| /// An inverted range used to reach `get_unchecked` and was undefined behavior | ||
| /// from safe code. It now goes through checked string indexing, so it panics | ||
| /// instead of invoking UB. | ||
| #[test] |
There was a problem hiding this comment.
The "used to" part of the comment also seems useless.
|
Not sure what the AI policy is for this crate, btw. |
|
@SimonSapin @jdm do you know? ^ |
|
@emilio There’s an argument that being under https://github.com/servo/ means Servo’s policy applies if nothing more specific says otherwise. But really I think that’s up to us (specifically you and me) to decide. I would like to explicitly adopt Servo’s policy (and while we’re at it, Code of Conduct). What do you think? |
|
I'm totally cool with the code of conduct of course. In terms of AI policy, I strongly agree with communications having to be human-written. I'd personally be ok with AI-generated code as long as the author is accountable for it / understands it (and defends it as needed with human words)... That said, I wouldn't object to plain banning AI-generated code I guess, it is a bit different from what we do in Firefox, but... |
|
Alright, let’s make it explicit: #465 @samiarif02 I’m closing this PR according to the policy at https://book.servo.org/policy/ai-usage.html. If you’re interested feel free to write a fix yourself and submit another PR. |
Fixes #459.
Parser::sliceandParser::slice_fromare safe, public methods, but they calledself.input.get_unchecked(range.start.0..range.end.0)with a caller-supplied range. Safe code could therefore trigger undefined behavior:p1..p0(bothis_char_boundarydebug-asserts still pass, andend - startunderflows inget_unchecked);resetand then callingslice_fromwith a later position;SourcePositionfrom a parser over a longer input (thedebug_assert!s are compiled out in release).Fix: the public
slice/slice_fromnow use checked string indexing (&self.input[..]). To keep @emilio's point about not adding codepoint-boundary overhead on the hot tokenizer paths, the unchecked implementation is retained aspub(crate)slice_unchecked/slice_from_unchecked, and the internal callers (which always pass valid, ordered positions for the current input) use those.Tests:
parser_slice_valid_rangechecks a valid range still returns the expected substring;parser_slice_inverted_range_panics_instead_of_ubchecks an inverted range now panics (safe) instead of invoking UB. Full lib suite: 48 passed.Note on repro case 2 (
reset+slice_from): I left it out of the test because it needs a bit more setup to construct safely, but the checked indexing covers it the same way — any out-of-order or out-of-range position now panics instead of being UB. Happy to adjust the internal/public split if you'd prefer a different boundary.🤖 Generated with Claude Code