indentation: Don't crash on a closing flow token without an opening one#816
Open
sarathfrancis90 wants to merge 1 commit into
Open
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Running yamllint on some invalid YAML crashes instead of reporting the syntax error:
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 readsline_indentfrom the parent on its stack — but here the parent is a block/root node whoseline_indentisNone, soexpectedbecomesNoneand theexpected < 0comparison blows up.I skip the indentation check when
expectedisNone, which only happens in this malformed case, so well-formed flows are unchanged and the syntax error is reported as usual.Fixes #771.