Raise TextFSMTemplateError for a Value line with no regex - #141
Open
arpitjain099 wants to merge 1 commit into
Open
Raise TextFSMTemplateError for a Value line with no regex#141arpitjain099 wants to merge 1 commit into
arpitjain099 wants to merge 1 commit into
Conversation
A Value line that has options and a name but no regex, such as 'Value Required beer', leaves self.regex empty. The bounds check that follows indexes it directly, so self.regex[0] raises IndexError: string index out of range. That escapes the TextFSMTemplateError handling in _ParseFSMVariables, so a malformed template surfaces a raw builtin from the TextFSM constructor instead of the library's own template error. Treat an empty regex as not contained within a '()' pair, which is what it is, and reuse the existing message. Regexes of length one already short-circuit on the first two comparisons, so the empty case was the only crash. Signed-off-by: Arpit Jain <arpitjain099@gmail.com>
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.
A
Valueline that carries options and a name but no regex, which is an easy typo to make when the parentheses get dropped, crashes with a rawIndexErrorinstead of the library's own template error.self.regexends up as"", and the check right after it doesself.regex[0]before anything validates that the string is non-empty. SinceIndexErroris notTextFSMTemplateError, it slips past the handler in_ParseFSMVariables, so callers that catch template errors to report a bad template get a builtin exception instead.An empty regex genuinely is not contained within a
()pair, so the existing check and message cover it once the emptiness is tested first. Regexes of length one such as(or)already short-circuit on the first two comparisons, so the empty string was the only input that reached[-2]on a too-short value.Added the missing-regex case to
testFSMValue(throughTextFSMValue.Parse) and to the malformed-variable block oftestParseFSMVariables(through the template path). Both fail withIndexErrorbefore the change. Full suite is 33 passed after it.