Skip to content

strip_whitespace=True raises IndexError on "( AS )" #885

Description

@andidukaj-beep

sqlparse.format(sql, strip_whitespace=True) raises an unhandled IndexError for the input "( AS )" (a parenthesis group that ends up with fewer than two tokens). A parsing/formatting call should either handle the input or raise a clear parse error, not fail with list index out of range.

Version: reproduced on current master (commit 60cdc64).

Minimal reproducer:

import sqlparse
sqlparse.format("( AS )", strip_whitespace=True)

Traceback:

Traceback (most recent call last):
  (reproducer above)
    import sqlparse; sqlparse.format('( AS )', strip_whitespace=True)
                     ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "sqlparse/__init__.py", line 60, in format
    return "".join(stack.run(sql, encoding))
           ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "sqlparse/engine/filter_stack.py", line 44, in run
    filter_.process(stmt)
    ~~~~~~~~~~~~~~~^^^^^^
  File "sqlparse/filters/others.py", line 126, in process
    [self.process(sgroup, depth + 1) for sgroup in stmt.get_sublists()]
     ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
  File "sqlparse/filters/others.py", line 127, in process
    self._stripws(stmt)
    ~~~~~~~~~~~~~^^^^^^
  File "sqlparse/filters/others.py", line 88, in _stripws
    func(tlist)
    ~~~~^^^^^^^
  File "sqlparse/filters/others.py", line 115, in _stripws_parenthesis
    while tlist.tokens[1].is_whitespace:
          ~~~~~~~~~~~~^^^
IndexError: list index out of range

Cause (from the traceback): _stripws_parenthesis accesses tlist.tokens[1] (and later tokens[-2]) without checking the group length; a parenthesis group with fewer than 2 tokens triggers the IndexError.

Expected: the input is parsed/formatted, or a clear parse error is raised — not an unhandled IndexError.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions