Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent crash on integer overflow... by removing LALRPOP match #4

Open
roguh opened this issue Aug 9, 2017 · 0 comments
Open

Prevent crash on integer overflow... by removing LALRPOP match #4

roguh opened this issue Aug 9, 2017 · 0 comments
Assignees

Comments

@roguh
Copy link

roguh commented Aug 9, 2017

Replicate

Try to parse an overflowing integer (also works with any input that makes from_str fail).

nitro>>> 11111111111111111111111
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: ParseIntError { kind: Overflow }'...

Fix

The action code must be able to return a ParseError when i64::from_str(<>) fails.
This is possible using LALRPOP; however, it is impossible to return a ParseError::User with a useful error message AND use the match keyword. This is because "match and extern definitions are mutually exclusive," as reported by LALRPOP.

With match, but without extern and a custom ParseError::User type:

match {...}

pub Int: i64 = {
    INT =>? match i64::from_str(<>) {
        Ok(i) => Ok(i),

        // Don't panic on overflow or other parse error
        Err(e) => Err(ParseError::User { error: () })
      }
};

With extern, but without match:

extern {
  Error = useful ParseError::User type
}

pub Int: i64 = {
    INT =>? match i64::from_str(<>) {
        Ok(i) => Ok(i),

        // Don't panic on overflow or other parse error
        Err(e) => Err(ParseError::User { error: /*useful info (your integer's too big!)*/ })
      }
};

See how LALRPOP parses itself:
https://github.com/nikomatsakis/lalrpop/blob/f934229ed76f123a1d4c190edd4d05cf5cc4ff2d/lalrpop/src/p

Simple example of using extern { Error = /*custom parse error type*/ }
https://github.com/nikomatsakis/lalrpop/blob/master/lalrpop-test/src/error.lalrpop

@roguh roguh self-assigned this Aug 9, 2017
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

No branches or pull requests

1 participant