LucaCappelletti94 commented on code in PR #2400:
URL: 
https://github.com/apache/datafusion-sqlparser-rs/pull/2400#discussion_r4423955369


##########
src/ast/spans.rs:
##########
@@ -3423,4 +3431,48 @@ SETTINGS update_parallel_mode = 'auto'"#;
             assert_eq!(test.get_source(expr.span()), expected, "{sql}");
         }
     }
+
+    #[test]
+    #[cfg(feature = "recursive-protection")]
+    fn test_deeply_nested_expr_span_does_not_overflow() {
+        // 200k nested nodes on a 512 KiB stack overflow unless the walk grows 
the stack.
+        std::thread::Builder::new()
+            .stack_size(512 * 1024)
+            .spawn(|| {
+                let mut expr = Expr::Identifier(crate::ast::Ident::new("x"));
+                for _ in 0..200_000 {
+                    expr = Expr::Nested(Box::new(expr));
+                }
+                let _ = expr.span();
+                // Dropping the chain recurses too; leak it so the test 
measures only the span walk.

Review Comment:
   Dropping a deep `OR` chain recurses too, but its frames are far smaller than 
those of `span()`. On a 2 MiB thread in release, `span()` on main overflows at 
about 2,400 `OR` terms, while dropping the same tree only overflows at about 
32,000. In debug the figures are 313 against about 21,000. Of course all of 
these inputs are only adversarial stuff.
   
   I agree with you that a different representation is the long term solution, 
and I think it is also worth adding that most of the spans current 
implementations are different flavours of wrong, so a refactoring regarding 
them will need to take place at some point in the future.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to