Skip to content

Commit 1fca212

Browse files
committed
fix: add stacker and maybe_grow on recursion guard
1 parent a4fa9e0 commit 1fca212

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ path = "src/lib.rs"
3838

3939
[features]
4040
default = ["std"]
41-
std = []
41+
std = ["stacker"]
4242
# Enable JSON output in the `cli` example:
4343
json_example = ["serde_json", "serde"]
4444
visitor = ["sqlparser_derive"]
@@ -48,10 +48,11 @@ bigdecimal = { version = "0.4.1", features = ["serde"], optional = true }
4848
log = "0.4"
4949
serde = { version = "1.0", features = ["derive"], optional = true }
5050
# serde_json is only used in examples/cli, but we have to put it outside
51-
# of dev-dependencies because of
51+
# dev-dependencies because of
5252
# https://github.com/rust-lang/cargo/issues/1596
5353
serde_json = { version = "1.0", optional = true }
5454
sqlparser_derive = { version = "0.2.0", path = "derive", optional = true }
55+
stacker = { version = "0.1.17", optional = true }
5556

5657
[dev-dependencies]
5758
simple_logger = "5.0"

src/parser/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ mod recursion {
6565

6666
use super::ParserError;
6767

68+
/// try to grow stack
69+
fn maybe_grow() {
70+
#[cfg(debug_assertions)]
71+
stacker::maybe_grow(512 << 10, 8 << 20, || {});
72+
#[cfg(not(debug_assertions))]
73+
stacker::maybe_grow(128 << 10, 2 << 20, || {});
74+
}
75+
6876
/// Tracks remaining recursion depth. This value is decremented on
6977
/// each call to [`RecursionCounter::try_decrease()`], when it reaches 0 an error will
7078
/// be returned.
@@ -93,10 +101,12 @@ mod recursion {
93101
/// remaining depth upon drop;
94102
pub fn try_decrease(&self) -> Result<DepthGuard, ParserError> {
95103
let old_value = self.remaining_depth.get();
96-
// ran out of space
104+
97105
if old_value == 0 {
98106
Err(ParserError::RecursionLimitExceeded)
99107
} else {
108+
maybe_grow();
109+
100110
self.remaining_depth.set(old_value - 1);
101111
Ok(DepthGuard::new(Rc::clone(&self.remaining_depth)))
102112
}

0 commit comments

Comments
 (0)