Skip to content

Raise ValueError for truncated path commands instead of a bare IndexError - #246

Open
eeshsaxena wants to merge 1 commit into
mathandy:masterfrom
eeshsaxena:fix/parse-path-truncated-commands
Open

Raise ValueError for truncated path commands instead of a bare IndexError#246
eeshsaxena wants to merge 1 commit into
mathandy:masterfrom
eeshsaxena:fix/parse-path-truncated-commands

Conversation

@eeshsaxena

Copy link
Copy Markdown

I was feeding some machine-generated d-strings through parse_path and hit a couple of confusing crashes when a string happened to be cut off mid-command.

Anything that ends before a command has all its numbers, like M 0 0 L, C 1 2, or a lone H, pops past the end of the token list in _parse_path and comes back as IndexError: pop from empty list, which doesn't tell you much about what was wrong with the path. I added a small per-command argument-count check right after a command is read, so a short command now raises a ValueError naming the command and how many values it expected, matching how the parser already reports other malformed input (the Z 100 200 case).

While testing that I also ran into a second one: a path that starts with a smooth command, e.g. S 1 1 2 2 or T 1 1, raises TypeError: 'in <string>' requires string as left operand, not NoneType. On the first command last_command is still None and the reflection check does last_command not in 'CS', and None in 'CS' throws. Per the SVG spec, when there's no preceding curve the first control point should just coincide with the current point, so I switched the two membership tests to tuples (('C', 'S') / ('Q', 'T')), which handles None and parses these as you'd expect.

Added tests for both under test_parsing.py; they fail on master (IndexError / TypeError) and pass with the change, and the rest of the parsing suite still passes.

A path string that ends mid-command (e.g. 'M 0 0 L', 'C 1 2', a bare 'H')
popped past the end of the token list in _parse_path, surfacing a bare
IndexError: pop from empty list. Add a per-command argument-count check so
these raise a descriptive ValueError like other malformed paths.

Also fix a leading smooth command (a path starting with S/s or T/t): the
reflection check compared last_command (None) with 'in "CS"', raising
TypeError. Compare against a tuple so a missing previous command is handled.
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