Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash if Bad Polyline #208

Open
bcwhite-code opened this issue May 31, 2023 · 2 comments
Open

Crash if Bad Polyline #208

bcwhite-code opened this issue May 31, 2023 · 2 comments

Comments

@bcwhite-code
Copy link

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
Copy link
Author

bcwhite-code commented May 31, 2023

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
Copy link
Author

I found the offending svg line:

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

No idea what it is in the UI.

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

No branches or pull requests

1 participant