Skip to content
Closed
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
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/generic_2400_span_or_chain.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT * FROM t WHERE id = 0 OR id = 1 OR id = 2 OR id = 3
52 changes: 52 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ impl<T> Spanned for Parens<T> {
}

impl Spanned for Query {
#[cfg_attr(feature = "recursive-protection", recursive::recursive)]
fn span(&self) -> Span {
let Query {
with,
Expand Down Expand Up @@ -218,6 +219,7 @@ impl Spanned for Cte {
///
/// [SetExpr::Table] is not implemented.
impl Spanned for SetExpr {
#[cfg_attr(feature = "recursive-protection", recursive::recursive)]
fn span(&self) -> Span {
match self {
SetExpr::Select(select) => select.span(),
Expand Down Expand Up @@ -323,6 +325,7 @@ impl Spanned for Values {
/// - [Statement::Unload]
/// - [Statement::OptimizeTable]
impl Spanned for Statement {
#[cfg_attr(feature = "recursive-protection", recursive::recursive)]
fn span(&self) -> Span {
match self {
Statement::Analyze(analyze) => analyze.span(),
Expand Down Expand Up @@ -1559,6 +1562,7 @@ impl Spanned for AssignmentTarget {
/// - [Expr::Map] # DuckDB specific
/// - [Expr::Lambda]
impl Spanned for Expr {
#[cfg_attr(feature = "recursive-protection", recursive::recursive)]
fn span(&self) -> Span {
match self {
Expr::Identifier(ident) => ident.span,
Expand Down Expand Up @@ -1809,6 +1813,7 @@ impl Spanned for ObjectName {
}

impl Spanned for ObjectNamePart {
#[cfg_attr(feature = "recursive-protection", recursive::recursive)]
fn span(&self) -> Span {
match self {
ObjectNamePart::Identifier(ident) => ident.span,
Expand Down Expand Up @@ -1937,6 +1942,7 @@ impl Spanned for SelectItemQualifiedWildcardKind {
}

impl Spanned for SelectItem {
#[cfg_attr(feature = "recursive-protection", recursive::recursive)]
fn span(&self) -> Span {
match self {
SelectItem::UnnamedExpr(expr) => expr.span(),
Expand Down Expand Up @@ -2043,6 +2049,7 @@ impl Spanned for ReplaceSelectElement {
/// Missing spans:
/// - [TableFactor::JsonTable]
impl Spanned for TableFactor {
#[cfg_attr(feature = "recursive-protection", recursive::recursive)]
fn span(&self) -> Span {
match self {
TableFactor::Table {
Expand Down Expand Up @@ -2403,6 +2410,7 @@ impl Spanned for TableWithJoins {
}

impl Spanned for Select {
#[cfg_attr(feature = "recursive-protection", recursive::recursive)]
fn span(&self) -> Span {
let Select {
select_token,
Expand Down Expand Up @@ -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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you can't drop the expression without overflowing the stack, I don't think allowing "spanned" to work is of much value

If we really want to support OR chains with 200,000 items, I think we need to use a non tree representation (there is an open ticket for this)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Or maybe I am missing something 🤔

What do you think @LucaCappelletti94 ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

core::mem::forget(expr);
})
.unwrap()
.join()
.expect("span walk overflowed the small-stack thread");
}

#[test]
#[cfg(feature = "recursive-protection")]
fn test_deeply_nested_object_name_part_span_does_not_overflow() {
// Same check for the ObjectNamePart -> FunctionArgExpr::QualifiedWildcard cycle.
std::thread::Builder::new()
.stack_size(512 * 1024)
.spawn(|| {
let mut part = ObjectNamePart::Identifier(crate::ast::Ident::new("x"));
for _ in 0..200_000 {
part = ObjectNamePart::Function(crate::ast::ObjectNamePartFunction {
name: crate::ast::Ident::new("f"),
args: vec![FunctionArg::Unnamed(FunctionArgExpr::QualifiedWildcard(
ObjectName(vec![part]),
))],
});
}
let _ = part.span();
core::mem::forget(part);
})
.unwrap()
.join()
.expect("span walk overflowed the small-stack thread");
}
}
Loading