Skip to content

fix(query): refuse statements too deep for the parser's stack - #128

Merged
venkat1701 merged 2 commits into
mainfrom
fix/parser-stack-depth
Nov 24, 2026
Merged

venkat1701 merged 2 commits into
mainfrom
fix/parser-stack-depth

Conversation

@venkat1701

@venkat1701 venkat1701 commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

CI has failed on every merge to main since 23 November, always on SecurityTest.extremeInputsFailCleanly. On the Linux runner (GraalVM CE 25.0.2), the statement with 10,000 nested brackets threw a StackOverflowError instead of the expected invalid-statement error. The parser stops at 256 levels of nesting, but each level takes about ten frames, and that runner ran out of stack before reaching the limit. It passed on macOS, which is why local builds never showed it.

Two changes:

  • The parser catches a stack overflow at its entry point and reports statement is nested too deeply to parse. Nothing is left half-done because all parser state lives on the parser instance, which is thrown away.
  • Arithmetic chains and call arguments now count towards the 256-level limit. 1+1+…+1 parses into a left-deep tree that the evaluator walks recursively, and nested call arguments skipped the counter. With 5,000 terms the evaluator ran out of stack on the server and the connection closed without a reply. Now the parser refuses the statement.

ParserDepthTest covers:

  • 250 nested brackets and 250 nested NOTs on a 64 KB stack either parse or are refused, and never throw StackOverflowError. Before the fix this test failed with the same StackOverflowError as CI.
  • Chains of 300 + or * terms, 300 nested abs( calls, and brackets mixed with a chain are refused.
  • A 200-term chain still parses.

Removing either of the two new counts makes the test fail.

Full build: engine 269, database 293, server 41, no failures.

Merge order for the memory stack: #128 (CI fix), #124, #125, #126, #127, #129 (online text index rebuild), #130 (starter kit). Retarget each to main once the one below it merges. A private preview image built from #129 merged with #128 is at ghcr.io/metacoglabs/hstore-memory:0.2.0-dec80f2.

A statement nested below the 256-level limit could still run the
parser out of stack on a JVM with smaller frames to spare. CI on
Linux hit this with 10,000 brackets and SecurityTest got a
StackOverflowError instead of an invalid-statement error. The parser
now turns a stack overflow into that error.
A chain like 1+1+...+1 parses into a left-deep tree that the
evaluator walks recursively, and arguments of nested calls were
parsed without going through the nesting counter. Both could still
exhaust the stack: 5,000 terms were enough to kill a server
connection without a reply. Each extra link of an arithmetic chain
and each call argument now counts towards the 256-level limit.
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.

1 participant