Skip to content

Panic when parsing fuzzed code #1768

Closed
Closed

Description

Describe the bug

The code should not panic.

To Reproduce

fn main() {
    let data = b"\n(\nx\n)\n";
    drop(boa::parse(data, true));
}

Expected behavior

It shouldn't panic, and it should parse correctly (as whatever it's meant to parse as, not sure.)

Build environment:

  • OS: Arch Linux
  • Target triple: x86_64-unknown-linux-gnu (I think, couldn't find how to print it and I don't think it's terribly important here)
  • Rustc version: rustc 1.59.0-nightly (cfa3fe5af 2021-12-31)

Activity

  1. ghost added
    A-Bug
    on Jan 2, 2022
  2. Razican commented on Jan 2, 2022

    @Razican
    Member

    One question, though. Why are you calling drop() on the parsed result? That could cause a double drop maybe?

  3. RageKnify commented on Jan 2, 2022

    @RageKnify
    Contributor

    I've tried to diagnose it and I'm pretty sure it's a bug in the lexer, I think its because there is only a \n after the closing parenthesis.
    It tries to peek for a token after the ) to check if it is =>, which would mean it is an arrow function. But in this case there is no other token ahead of it and while trying to fill the lexer's buffer something goes wrong.

  4. added
    C-Parser
    and removed
    C-Parser
    on Jan 2, 2022
  5. RageKnify commented on Jan 2, 2022

    @RageKnify
    Contributor

    The BufferedLexer's write_index is "overflowing"
    peeked contains ['\n', '(', '\n', 'x', '\n', ')', '\n']

    The line trying to peek is here

    TokenKind::Punctuator(Punctuator::CloseParen) => {
    // Need to check if the token after the close paren is an arrow, if so then this is an ArrowFunction
    // otherwise it is an expression of the form (b).
    if let Some(t) = cursor.peek(3)? {
    if t.kind() == &TokenKind::Punctuator(Punctuator::Arrow)

    I have the peeking visualized below:

    \n   (  \n   x  \n   )  \n  ?
    0    0   1   1   2   2  3   3
    

    This seems to be the max size we expected, the ? becomes None during the fill function, but right afterwards it increments write_index, which goes back to 0 and it fails the assertion:

    self.write_index = (self.write_index + 1) % PEEK_BUF_SIZE;
    debug_assert_ne!(
    self.read_index, self.write_index,
    "we reached the read index with the write index"
    );

  6. added this to the v0.14.0 milestone on Jan 31, 2022
  7. self-assigned this
    on Feb 5, 2022
  8. added a commit that references this issue on Feb 5, 2022
    83fd796
  9. linked a pull request that will close this issue[Merged by Bors] - Fixed #1768 #1820on Feb 5, 2022
  10. added a commit that references this issue on Feb 7, 2022
    339b862
  11. added a commit that references this issue on Feb 7, 2022
    4a11ca1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-Bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions