svgpathtools
svgpathtools copied to clipboard
Crash if Bad Polyline
Got this error today trying to read a huge (33MiB) SVG Inkscape export.
File "/home/bcwhite/.local/lib/python3.9/site-packages/svgpathtools/svg_to_paths.py", line 253, in svg2paths2
return svg2paths(svg_file_location=svg_file_location,
File "/home/bcwhite/.local/lib/python3.9/site-packages/svgpathtools/svg_to_paths.py", line 207, in svg2paths
d_strings += [polygon2pathd(pg) for pg in pgons]
File "/home/bcwhite/.local/lib/python3.9/site-packages/svgpathtools/svg_to_paths.py", line 207, in <listcomp>
d_strings += [polygon2pathd(pg) for pg in pgons]
File "/home/bcwhite/.local/lib/python3.9/site-packages/svgpathtools/svg_to_paths.py", line 86, in polygon2pathd
return polyline2pathd(polyline, True)
File "/home/bcwhite/.local/lib/python3.9/site-packages/svgpathtools/svg_to_paths.py", line 65, in polyline2pathd
closed = (float(points[0][0]) == float(points[-1][0]) and
IndexError: list index out of range
I've no idea on what path it choked but you might want to consider putting a guard on the points array that is returned to make sure that it actually contains something.
Here's the fix I'm running locally:
diff --git a/svgpathtools/svg_to_paths.py b/svgpathtools/svg_to_paths.py
index 6211e6f..108cd98 100644
--- a/svgpathtools/svg_to_paths.py
+++ b/svgpathtools/svg_to_paths.py
@@ -62,6 +62,7 @@ def polyline2pathd(polyline, is_polygon=False):
else:
points = COORD_PAIR_TMPLT.findall(polyline.get('points', ''))
+ if not points or len(points) < 2: return ""
closed = (float(points[0][0]) == float(points[-1][0]) and
float(points[0][1]) == float(points[-1][1]))
I don't know why Inkscape is writing empty polygon strings (points="").
I found the offending svg line:
<polygon
class="st21"
points=""
id="polygon476939" />
No idea what it is in the UI.