mathandy / svgpathtools

A collection of tools for manipulating and analyzing SVG Path objects and Bezier curves.
MIT License
557 stars 142 forks source link

Crash if Bad Polyline #208

Open bcwhite-code opened 1 year ago

bcwhite-code commented 1 year ago

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.

bcwhite-code commented 1 year ago

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="").

bcwhite-code commented 1 year ago

I found the offending svg line:

<polygon
   class="st21"
   points=""
   id="polygon476939" />

No idea what it is in the UI.