Skip to content

Commit dc4127c

Browse files
committed
Add a recursion limit to prevent stack overflows
Until now, it's been able to trigger a stack overflow crash by providing a string with excessive recursion. For instance a string of 1000 left brackets causes the parser to recurse down 1000 times, and overflow the stack. This commit adds protection against excessive recursion. It adds a field to `Parser` for tracking the current recursion depth. Every function that returns a `Result` gains a recursion depth check. This isn't quite every method on the `Parser`, but it's the vast majority. An alternative implemention would be to only protect against AST recursions, rather than recursive function calls in `Parser`. That isn't as easy to implement because the parser is so large.
1 parent 3f1c642 commit dc4127c

File tree

3 files changed

+299
-1
lines changed

3 files changed

+299
-1
lines changed

Cargo.toml

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ path = "src/lib.rs"
2020

2121
[features]
2222
default = ["std"]
23-
std = []
23+
std = ["scopeguard"]
2424
# Enable JSON output in the `cli` example:
2525
json_example = ["serde_json", "serde"]
2626

@@ -32,6 +32,7 @@ serde = { version = "1.0", features = ["derive"], optional = true }
3232
# of dev-dependencies because of
3333
# https://github.com/rust-lang/cargo/issues/1596
3434
serde_json = { version = "1.0", optional = true }
35+
scopeguard = { version = "1.1.0", optional = true }
3536

3637
[dev-dependencies]
3738
simple_logger = "2.1"

0 commit comments

Comments
 (0)