diff --git a/eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_common.cpp b/eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_common.cpp
index 57d5b3be5a713c7bedf41bbfa66c75dca5e02921..843eda5b1d5a1f657f862fba374579358edb8413 100644
--- a/eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_common.cpp
+++ b/eeschema/sch_io/kicad_sexpr/sch_io_kicad_sexpr_common.cpp
@@ -309,29 +309,42 @@ void formatPoly( OUTPUTFORMATTER* aFormatter, int aNestLevel, EDA_SHAPE* aPolyLi
 {
     int newLine = 0;
     int lineCount = 1;
+
     aFormatter->Print( aNestLevel, "(polyline%s\n",
                        aIsPrivate ? " private" : "" );
     aFormatter->Print( aNestLevel + 1, "(pts" );
 
-    for( const VECTOR2I& pt : aPolyLine->GetPolyShape().Outline( 0 ).CPoints() )
+    const SHAPE_POLY_SET& polySet = aPolyLine->GetPolyShape();
+
+
+    if( polySet.OutlineCount() == 0 )
     {
-        if( newLine == 4 || !ADVANCED_CFG::GetCfg().m_CompactSave )
-        {
-            aFormatter->Print( 0, "\n" );
-            aFormatter->Print( aNestLevel + 2, "(xy %s %s)",
-                               EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pt.x ).c_str(),
-                               EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pt.y ).c_str() );
-            newLine = 0;
-            lineCount += 1;
-        }
-        else
+        // If we've managed to get a polyline with no points, that's probably a bad thing,
+        // but at least don't dereference it and crash.
+        wxFAIL_MSG( "Polyline has no outline" );
+    }
+    else
+    {
+        for( const VECTOR2I& pt : polySet.Outline( 0 ).CPoints() )
         {
-            aFormatter->Print( 0, " (xy %s %s)",
-                               EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pt.x ).c_str(),
-                               EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pt.y ).c_str() );
+            if( newLine == 4 || !ADVANCED_CFG::GetCfg().m_CompactSave )
+            {
+                aFormatter->Print( 0, "\n" );
+                aFormatter->Print( aNestLevel + 2, "(xy %s %s)",
+                                EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pt.x ).c_str(),
+                                EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pt.y ).c_str() );
+                newLine = 0;
+                lineCount += 1;
+            }
+            else
+            {
+                aFormatter->Print( 0, " (xy %s %s)",
+                                EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pt.x ).c_str(),
+                                EDA_UNIT_UTILS::FormatInternalUnits( schIUScale, pt.y ).c_str() );
+            }
+
+            newLine += 1;
         }
-
-        newLine += 1;
     }
 
     if( lineCount == 1 )