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

Stack overflow when parsing an expression #1000

Open
orf opened this issue Aug 26, 2023 · 3 comments
Open

Stack overflow when parsing an expression #1000

orf opened this issue Aug 26, 2023 · 3 comments

Comments

@orf
Copy link
Contributor

orf commented Aug 26, 2023

Below I've attached a real file that has been published on PyPI. It contains an impressively gigantic expression.

huge.txt

Parsing this file with the current libcst master branch causes a stack overflow. After some debugging, this appears to be caused by the cache_left_rec macro cloning the expression (here), which in this case causes a gigantic recursive clone of the expression, eventually overflowing the stack.

Screenshot 2023-08-26 at 12 40 04
@zsol
Copy link
Member

zsol commented Aug 26, 2023

FWIW CPython also chokes on this input:

❯ py -m py_compile 'huge.txt'
Sorry: RecursionError: maximum recursion depth exceeded during compilation

@orf
Copy link
Contributor Author

orf commented Aug 26, 2023

Yeah, it's definitely a pathalogical file. What's annoying is that this brings down the entire process in Rust and there isn't really a way to stop that from happening, so you sort of need to heuristically detect if a given file will stack overflow or not based on the tokens.

I thought I'd add this issue anyway.

@zsol
Copy link
Member

zsol commented Aug 26, 2023

Fair enough. If anyone's interested in tackling this, here are a bunch of pointers:

There's best-effort recursion depth checking that's defined here:

const MAX_RECURSION_DEPTH: usize = 3000;

It's checked in a couple of places, but we could definitely add more checks.

More context in #619, another example in #980

A much more thorough attempt is in the PR attached to kevinmehall/rust-peg#282

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

2 participants