-
Notifications
You must be signed in to change notification settings - Fork 767
Apply recursion limit without std #2465
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
Open
LucaCappelletti94
wants to merge
1
commit into
apache:main
Choose a base branch
from
LucaCappelletti94:no-std-recursion-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+117
−47
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,46 +72,25 @@ macro_rules! parser_err { | |
| mod alter; | ||
| mod merge; | ||
|
|
||
| #[cfg(feature = "std")] | ||
| /// Implementation [`RecursionCounter`] if std is available | ||
| mod recursion { | ||
| use std::cell::Cell; | ||
| use std::rc::Rc; | ||
| use alloc::rc::Rc; | ||
| use core::cell::Cell; | ||
|
|
||
| use super::ParserError; | ||
|
|
||
| /// Tracks remaining recursion depth. This value is decremented on | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why remove all these comments? |
||
| /// each call to [`RecursionCounter::try_decrease()`], when it reaches 0 an error will | ||
| /// be returned. | ||
| /// | ||
| /// Note: Uses an [`std::rc::Rc`] and [`std::cell::Cell`] in order to satisfy the Rust | ||
| /// borrow checker so the automatic [`DepthGuard`] decrement a | ||
| /// reference to the counter. | ||
| /// | ||
| /// Note: when "recursive-protection" feature is enabled, this crate uses additional stack overflow protection | ||
| /// for some of its recursive methods. See [`recursive::recursive`] for more information. | ||
| pub(crate) struct RecursionCounter { | ||
| remaining_depth: Rc<Cell<usize>>, | ||
| } | ||
|
|
||
| impl RecursionCounter { | ||
| /// Creates a [`RecursionCounter`] with the specified maximum | ||
| /// depth | ||
| pub fn new(remaining_depth: usize) -> Self { | ||
| Self { | ||
| remaining_depth: Rc::new(remaining_depth.into()), | ||
| } | ||
| } | ||
|
|
||
| /// Decreases the remaining depth by 1. | ||
| /// | ||
| /// Returns [`Err`] if the remaining depth falls to 0. | ||
| /// | ||
| /// Returns a [`DepthGuard`] which will adds 1 to the | ||
| /// 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 { | ||
|
|
@@ -121,7 +100,6 @@ mod recursion { | |
| } | ||
| } | ||
|
|
||
| /// Guard that increases the remaining depth by 1 on drop | ||
| pub struct DepthGuard { | ||
| remaining_depth: Rc<Cell<usize>>, | ||
| } | ||
|
|
@@ -131,33 +109,13 @@ mod recursion { | |
| Self { remaining_depth } | ||
| } | ||
| } | ||
|
|
||
| impl Drop for DepthGuard { | ||
| fn drop(&mut self) { | ||
| let old_value = self.remaining_depth.get(); | ||
| self.remaining_depth.set(old_value + 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(not(feature = "std"))] | ||
| mod recursion { | ||
| /// Implementation [`RecursionCounter`] if std is NOT available (and does not | ||
| /// guard against stack overflow). | ||
| /// | ||
| /// Has the same API as the std [`RecursionCounter`] implementation | ||
| /// but does not actually limit stack depth. | ||
| pub(crate) struct RecursionCounter {} | ||
|
|
||
| impl RecursionCounter { | ||
| pub fn new(_remaining_depth: usize) -> Self { | ||
| Self {} | ||
| } | ||
| pub fn try_decrease(&self) -> Result<DepthGuard, super::ParserError> { | ||
| Ok(DepthGuard {}) | ||
| self.remaining_depth.set(old_value.saturating_add(1)); | ||
| } | ||
| } | ||
|
|
||
| pub struct DepthGuard {} | ||
| } | ||
|
|
||
| #[derive(PartialEq, Eq)] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #![cfg(not(feature = "std"))] | ||
|
|
||
| use sqlparser::dialect::GenericDialect; | ||
| use sqlparser::parser::{Parser, ParserError}; | ||
|
|
||
| #[test] | ||
| fn with_recursion_limit_applies_without_default_features() { | ||
| let dialect = GenericDialect {}; | ||
| let result = Parser::new(&dialect) | ||
| .with_recursion_limit(1) | ||
| .try_with_sql("SELECT * FROM foo WHERE (a OR (b OR (c OR d)))") | ||
| .unwrap() | ||
| .parse_statements(); | ||
|
|
||
| assert_eq!(result, Err(ParserError::RecursionLimitExceeded)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn default_recursion_limit_applies_without_default_features() { | ||
| let dialect = GenericDialect {}; | ||
| let sql = format!( | ||
| "SELECT * FROM t WHERE {}a = 1{}", | ||
| "(".repeat(200), | ||
| ")".repeat(200) | ||
| ); | ||
|
|
||
| let result = Parser::parse_sql(&dialect, &sql); | ||
|
|
||
| assert_eq!(result, Err(ParserError::RecursionLimitExceeded)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deeply_nested_not_returns_error_without_default_features() { | ||
| let dialect = GenericDialect {}; | ||
| let sql = format!("SELECT * FROM t WHERE {}a", "NOT ".repeat(1024)); | ||
|
|
||
| let result = Parser::parse_sql(&dialect, &sql); | ||
|
|
||
| assert!(result.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn valid_nested_queries_parse_without_default_features() { | ||
| let dialect = GenericDialect {}; | ||
|
|
||
| let result = Parser::parse_sql(&dialect, "SELECT 1 + (2 + 3)"); | ||
|
|
||
| assert!(result.is_ok()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn recursion_budget_restores_between_statements_without_default_features() { | ||
| let dialect = GenericDialect {}; | ||
| let statements = Parser::new(&dialect) | ||
| .with_recursion_limit(4) | ||
| .try_with_sql("SELECT 1; SELECT 2; SELECT 3") | ||
| .unwrap() | ||
| .parse_statements() | ||
| .unwrap(); | ||
|
|
||
| assert_eq!(statements.len(), 3); | ||
| } | ||
|
|
||
| #[test] | ||
| fn deeply_nested_intervals_hit_recursion_limit_without_default_features() { | ||
| let dialect = GenericDialect {}; | ||
| let sql = format!("SELECT {}1", "INTERVAL ".repeat(1000)); | ||
|
|
||
| let result = Parser::parse_sql(&dialect, &sql); | ||
|
|
||
| assert_eq!(result, Err(ParserError::RecursionLimitExceeded)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested_queries_hit_recursion_limit_without_default_features() { | ||
| let dialect = GenericDialect {}; | ||
| let sql = format!( | ||
| "{}SELECT 1{}", | ||
| "SELECT 1 WHERE 1 IN (".repeat(100), | ||
| ")".repeat(100) | ||
| ); | ||
|
|
||
| let result = Parser::parse_sql(&dialect, &sql); | ||
|
|
||
| assert_eq!(result, Err(ParserError::RecursionLimitExceeded)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn nested_table_factors_hit_recursion_limit_without_default_features() { | ||
| let dialect = GenericDialect {}; | ||
| let sql = format!("SELECT * FROM {}t{}", "(".repeat(100), ")".repeat(100)); | ||
|
|
||
| let result = Parser::parse_sql(&dialect, &sql); | ||
|
|
||
| assert_eq!(result, Err(ParserError::RecursionLimitExceeded)); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍