Skip to content

Commit b7cd371

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 dd805e9 commit b7cd371

File tree

4 files changed

+293
-5
lines changed

4 files changed

+293
-5
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"

examples/parse_select.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ use sqlparser::dialect::GenericDialect;
1616
use sqlparser::parser::*;
1717

1818
fn main() {
19-
let sql = "SELECT a, b, 123, myfunc(b) \
20-
FROM table_1 \
21-
WHERE a > b AND b < 100 \
22-
ORDER BY a DESC, b";
19+
let sql = "((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((";
2320

2421
let dialect = GenericDialect {};
2522

0 commit comments

Comments
 (0)