Skip to content

Raise TextFSMTemplateError for a Value line with no regex - #141

Open
arpitjain099 wants to merge 1 commit into
google:masterfrom
arpitjain099:fix/value-missing-regex
Open

Raise TextFSMTemplateError for a Value line with no regex#141
arpitjain099 wants to merge 1 commit into
google:masterfrom
arpitjain099:fix/value-missing-regex

Conversation

@arpitjain099

Copy link
Copy Markdown

A Value line that carries options and a name but no regex, which is an easy typo to make when the parentheses get dropped, crashes with a raw IndexError instead of the library's own template error.

>>> import io, textfsm
>>> textfsm.TextFSM(io.StringIO("Value Required foo\n\nStart\n  ^$$ -> Record\n"))
IndexError: string index out of range

self.regex ends up as "", and the check right after it does self.regex[0] before anything validates that the string is non-empty. Since IndexError is not TextFSMTemplateError, 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 (through TextFSMValue.Parse) and to the malformed-variable block of testParseFSMVariables (through the template path). Both fail with IndexError before the change. Full suite is 33 passed after it.

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>
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.

1 participant