Skip to content

Don't raise IndexError on an unbalanced transform expression - #508

Merged
deeplook merged 1 commit into
deeplook:mainfrom
eeshsaxena:fix/unbalanced-transform-parens
Aug 17, 2026
Merged

Don't raise IndexError on an unbalanced transform expression#508
deeplook merged 1 commit into
deeplook:mainfrom
eeshsaxena:fix/unbalanced-transform-parens

Conversation

@eeshsaxena

Copy link
Copy Markdown

A transform attribute with an unmatched parenthesis crashes the converter:

from svglib.svglib import Svg2RlgAttributeConverter
Svg2RlgAttributeConverter().convertTransform('scale(2')
# IndexError: list index out of range

convertTransform collects the indices of all (/) characters and walks them two at a time as open/close pairs via range(0, len(brackets), 2), reading brackets[i + 1]. An unmatched parenthesis leaves an odd number of positions, so the last iteration indexes past the list. I step to len(brackets) - 1 so a final unmatched bracket is skipped; the transform is then reported as unparsable and ignored, matching how other invalid transforms are handled. Valid transforms are unaffected.

Extended the existing TestTransformAttrConverter mapping with unbalanced cases; it raises IndexError on main and passes with the change. Found it by fuzzing svg2rlg with mutated SVG.

convertTransform collected the positions of all parentheses and iterated them
two at a time as open/close pairs. A transform with an unmatched parenthesis
(e.g. 'scale(2') left an odd number of positions, so brackets[i + 1] indexed
past the list. Stop before a final unmatched bracket; such a transform is then
reported as unparsable and ignored, like other invalid transforms.
@deeplook

Copy link
Copy Markdown
Owner

Thanks for the fix and tests!

@deeplook
deeplook merged commit 34ec914 into deeplook:main Aug 17, 2026
16 checks passed
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.

2 participants