Skip to content

indentation: Don't crash on a closing flow token without an opening one#816

Open
sarathfrancis90 wants to merge 1 commit into
adrienverge:masterfrom
sarathfrancis90:fix/indentation-unmatched-flow-end
Open

indentation: Don't crash on a closing flow token without an opening one#816
sarathfrancis90 wants to merge 1 commit into
adrienverge:masterfrom
sarathfrancis90:fix/indentation-unmatched-flow-end

Conversation

@sarathfrancis90

Copy link
Copy Markdown
Contributor

Running yamllint on some invalid YAML crashes instead of reporting the syntax error:

$ printf 'data:\n  config.alloy: |\n\n  }\n' | yamllint -
...
  File "yamllint/rules/indentation.py", line 343, in _check
    if expected < 0:
TypeError: '<' not supported between instances of 'NoneType' and 'int'

A stray } (or ]) makes PyYAML emit a closing flow token with no matching opening one. When that token is first on its line, the rule tries to align it with the line that opened the collection and reads line_indent from the parent on its stack — but here the parent is a block/root node whose line_indent is None, so expected becomes None and the expected < 0 comparison blows up.

I skip the indentation check when expected is None, which only happens in this malformed case, so well-formed flows are unchanged and the syntax error is reported as usual.

Fixes #771.

Linting some invalid YAML crashes the indentation rule:

    $ printf 'data:\n  config.alloy: |\n\n  }\n' | yamllint -
    …
    File "yamllint/rules/indentation.py", line 343, in _check
        if expected < 0:
    TypeError: '<' not supported between instances of 'NoneType' and 'int'

When a closing flow token ('}' or ']') is the first token on its line,
the rule expects it to align with the line that opened the collection and
reads that position from the matching parent's line_indent. For invalid
YAML, PyYAML can emit a FlowMapping/FlowSequenceEndToken with no matching
opening token, so the parent on the stack is a block (or root) node whose
line_indent is None. expected then becomes None and the following
'expected < 0' comparison raises a TypeError, taking down the whole run.

Skip the indentation check when expected is None and let the syntax error
be reported as usual. expected is only ever None in this malformed case,
so well-formed flows are unaffected.

Fixes adrienverge#771.
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.

TypeError in indentation rule when parsing invalid YAML file

1 participant