Skip to content

Guard case/parenthesis alignment against malformed input - #887

Open
afonsojanu wants to merge 1 commit into
andialbrecht:masterfrom
afonsojanu:fix/case-parenthesis-malformed-input-crashes
Open

Guard case/parenthesis alignment against malformed input#887
afonsojanu wants to merge 1 commit into
andialbrecht:masterfrom
afonsojanu:fix/case-parenthesis-malformed-input-crashes

Conversation

@afonsojanu

Copy link
Copy Markdown

Two separate crash reports (#885, #886) both trace back to the same kind of problem: the aligned-indent and strip-whitespace filters assume a well-formed token tree, and a couple of malformed inputs break that assumption in slightly different ways.

For strip_whitespace=True on ( AS ) (#885): parsing this collapses the whole parenthesis body into a single nested group, so the Parenthesis token list ends up with only one direct child instead of the usual opening paren / content / closing paren shape. _stripws_parenthesis indexed straight into tokens[1], which doesn't exist there, raising IndexError.

For reindent_aligned=True on a malformed CASE ... END (#886): when END isn't a direct child of the Case token list (it can end up nested a couple of levels down inside a sibling group), token_next_by can't find it and returns None. That None then gets passed straight into insert_before, which tries to look up its position in the token list and raises ValueError: None is not in list.

Fixed both by having the filters check their assumptions instead of trusting them: _stripws_parenthesis only pops surrounding whitespace when there's actually room for it, and _process_case bails out early if it can't locate a real END token to align against. Neither fix changes behavior for well-formed SQL, only what happens on inputs that don't have a proper structure to begin with.

Added a regression test for each, using the exact reproducers from both issues, confirmed they fail on master and pass with the fix. Ran the full suite locally, 508 passed (plus the 2 pre-existing xfails and 1 xpass, untouched by this change).

Closes #885, closes #886

reindent_aligned=True and strip_whitespace=True could both crash on
certain malformed SQL where grouping doesn't produce the shape these
filters expect.

For CASE without a matching END as a direct child (it can end up nested
inside a sibling group instead), _process_case would grab a None token
and hand it to insert_before, which blew up with ValueError: None is
not in list.

For a parenthesis whose contents collapse into a single nested group
during parsing (e.g. "( AS )"), _stripws_parenthesis assumed at least
two direct children and raised IndexError on tokens[1].

Both filters now bail out gracefully instead of assuming a well-formed
tree, with regression tests for each case using the exact reproducers
from the two issues.
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.

reindent_aligned=True raises ValueError (token_index) on a malformed CASE input strip_whitespace=True raises IndexError on "( AS )"

1 participant