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

fix: add stacker and maybe_grow on recursion guard #1468

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ path = "src/lib.rs"

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

[dev-dependencies]
simple_logger = "5.0"
Expand Down
12 changes: 11 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ mod recursion {

use super::ParserError;

/// try to grow stack
fn maybe_grow() {
#[cfg(debug_assertions)]
stacker::maybe_grow(512 << 10, 8 << 20, || {});
#[cfg(not(debug_assertions))]
stacker::maybe_grow(128 << 10, 2 << 20, || {});
}

/// Tracks remaining recursion depth. This value is decremented on
/// each call to [`RecursionCounter::try_decrease()`], when it reaches 0 an error will
/// be returned.
Expand Down Expand Up @@ -93,10 +101,12 @@ mod recursion {
/// remaining depth upon drop;
pub fn try_decrease(&self) -> Result<DepthGuard, ParserError> {
let old_value = self.remaining_depth.get();
// ran out of space

if old_value == 0 {
Err(ParserError::RecursionLimitExceeded)
} else {
maybe_grow();

self.remaining_depth.set(old_value - 1);
Ok(DepthGuard::new(Rc::clone(&self.remaining_depth)))
}
Expand Down
Loading