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: stack overflow #751

Closed

Conversation

step-baby
Copy link
Contributor

@step-baby step-baby commented Dec 8, 2022

Closes #305

@coveralls
Copy link

coveralls commented Dec 8, 2022

Pull Request Test Coverage Report for Build 3689510617

  • 10 of 10 (100.0%) changed or added relevant lines in 2 files are covered.
  • 26 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.08%) to 86.197%

Files with Coverage Reduction New Missed Lines %
src/lib.rs 26 12.81%
Totals Coverage Status
Change from base Build 3641238188: -0.08%
Covered Lines: 12571
Relevant Lines: 14584

💛 - Coveralls

Copy link
Collaborator

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/sqlparser-rs/sqlparser-rs/pull/751/files?w=1

This PR Is very compelling to me as it does not require many intrusive changes. Thank you @step-baby

I will look into the proposed stacker dependency a bit prior to approving this PR, but it is looking good.

@AugustoFKL do you have any thoughts?

@alamb
Copy link
Collaborator

alamb commented Dec 8, 2022

Stacker looks quite impressive: https://crates.io/crates/stacker

Copy link
Contributor

@AugustoFKL AugustoFKL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alamb really awesome. Never thought about that.

Althought, stackoverflow is dependant on the current architecture support, right? A machine with 2 GB of RAM would have this problem more usually than one with 32GB. Am I missing something?

Not sure if the depth being static is perfect, but seems ok for now IMHO.

src/ast/mod.rs Outdated
} else {
write!(f, "EXPLAIN ")?;
}
stacker::maybe_grow(2 * 1024 * 1024, 5 * 1024 * 1024, || {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried about this indent change because it will conflict with every other PR.

So I pushed 853a890 as a way to make this diff smaller and shrink the merge conflict problem

@alamb
Copy link
Collaborator

alamb commented Dec 12, 2022

There is some issue building with no-std which I will try and resolve later this week

@alamb
Copy link
Collaborator

alamb commented Dec 13, 2022

Althought, stackoverflow is dependant on the current architecture support, right? A machine with 2 GB of RAM would have this problem more usually than one with 32GB. Am I missing something?

stacks are normally much smaller than available memory -- specifically I think the default is like 8MB or so on Linux.

Copy link
Collaborator

@alamb alamb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to check the stack size at each expr boundary or something -- I don't think this PR implements overflow checking in the way intended, sadly

@@ -161,7 +161,9 @@ impl<'a> Parser<'a> {
return parser.expected("end of statement", parser.peek_token());
}

let statement = parser.parse_statement()?;
let statement = stacker::maybe_grow(2 * 1024 * 1024, 5 * 1024 * 1024, || {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So looking at this code more carefully, I am not sure exactly how it is working -- specifically this call to maybe_grow grows the stack if there is less then 2MB remaining

In fact the test passes even if I remove the maybe_grow around this call (it fails if i remove the check on the impl Display)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/src/parser.rs b/src/parser.rs
index 5fd7af2..cff2281 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -161,9 +161,7 @@ impl<'a> Parser<'a> {
                 return parser.expected("end of statement", parser.peek_token());
             }
 
-            let statement = stacker::maybe_grow(2 * 1024 * 1024, 5 * 1024 * 1024, || {
-                parser.parse_statement()
-            })?;
+            let statement = parser.parse_statement()?;
             stmts.push(statement);
             expecting_statement_delimiter = true;
         }

And then

cd /Users/alamb/Software/sqlparser-rs/ && RUST_BACKTRACE=1 CARGO_TARGET_DIR=/Users/alamb/Software/target-df cargo test -- overflow
    Finished test [unoptimized + debuginfo] target(s) in 0.04s
     Running unittests src/lib.rs (/Users/alamb/Software/target-df/debug/deps/sqlparser-d27a7d1b89bd35ed)

running 1 test
test parser::tests::test_stack_overflow ... ok

🤔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe just need this

image

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@baoyachi
Copy link

Hi @alamb . Is there any better proposal? We urgently need this function.

@alamb
Copy link
Collaborator

alamb commented Dec 14, 2022

Hi @alamb . Is there any better proposal? We urgently need this function.

hi @baoyachi, I think the approach sketched out by @46bit in #501 is the best way to go. I will try and revive the code in that PR today.

Perhaps you can give it a look and see if it would work for your purposes when ready

@alamb
Copy link
Collaborator

alamb commented Dec 14, 2022

@baoyachi and @step-baby -- here is my alternate proposal: #764

@alamb
Copy link
Collaborator

alamb commented Dec 28, 2022

merged alternate proposal #764 - thanks for sparking this discussion @step-baby

@alamb alamb closed this Dec 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stack overflow when parsing SQL consisting of many imbalanced open parenthesis
6 participants