67c457
From faec595a1721a2496e9c258917facbb564f85854 Mon Sep 17 00:00:00 2001
67c457
From: rpm-build <rpm-build>
67c457
Date: Wed, 13 May 2020 17:53:13 -0500
67c457
Subject: [PATCH] CVE-2019-20446.patch
67c457
67c457
---
67c457
 librsvg/rsvg-base.c                           |  90 +++++++++---
67c457
 librsvg/rsvg-private.h                        |   5 +-
67c457
 rsvg_internals/src/drawing_ctx.rs             |  23 ++--
67c457
 rsvg_internals/src/structure.rs               |  21 ++-
67c457
 tests/errors.c                                |  52 ++++++-
67c457
 .../errors/308-doubly-recursive-use.svg       |  13 ++
67c457
 tests/fixtures/errors/308-recursive-use.svg   |   9 ++
67c457
 tests/fixtures/errors/308-use-self-ref.svg    |   7 +
67c457
 .../errors/515-pattern-billion-laughs.svg     | 130 ++++++++++++++++++
67c457
 .../errors/515-too-many-elements.svgz         | Bin 0 -> 40811 bytes
67c457
 10 files changed, 310 insertions(+), 40 deletions(-)
67c457
 create mode 100644 tests/fixtures/errors/308-doubly-recursive-use.svg
67c457
 create mode 100644 tests/fixtures/errors/308-recursive-use.svg
67c457
 create mode 100644 tests/fixtures/errors/308-use-self-ref.svg
67c457
 create mode 100644 tests/fixtures/errors/515-pattern-billion-laughs.svg
67c457
 create mode 100644 tests/fixtures/errors/515-too-many-elements.svgz
67c457
67c457
diff --git a/librsvg/rsvg-base.c b/librsvg/rsvg-base.c
67c457
index dbad819..af3d43c 100644
67c457
--- a/librsvg/rsvg-base.c
67c457
+++ b/librsvg/rsvg-base.c
67c457
@@ -431,12 +431,29 @@ node_set_atts (RsvgNode * node, RsvgHandle *handle, const NodeCreator *creator,
67c457
     }
67c457
 }
67c457
 
67c457
+static gboolean
67c457
+loading_limits_exceeded (RsvgHandle *handle)
67c457
+{
67c457
+    /* This is a mitigation for SVG files which create millions of elements
67c457
+     * in an attempt to exhaust memory.  We don't allow loading more than
67c457
+     * this number of elements during the initial streaming load process.
67c457
+     */
67c457
+    return handle->priv->num_loaded_elements > 200000;
67c457
+}
67c457
+
67c457
 static void
67c457
 rsvg_standard_element_start (RsvgHandle *handle, const char *name, RsvgPropertyBag * atts)
67c457
 {
67c457
     const NodeCreator *creator;
67c457
     RsvgNode *newnode = NULL;
67c457
 
67c457
+    if (loading_limits_exceeded (handle)) {
67c457
+        g_set_error (handle->priv->error, RSVG_ERROR, 0, "instancing limit");
67c457
+
67c457
+        xmlStopParser (handle->priv->ctxt);
67c457
+        return;
67c457
+    }
67c457
+
67c457
     creator = get_node_creator_for_element_name (name);
67c457
     g_assert (creator != NULL && creator->create_fn != NULL);
67c457
 
67c457
@@ -456,6 +473,7 @@ rsvg_standard_element_start (RsvgHandle *handle, const char *name, RsvgPropertyB
67c457
         handle->priv->treebase = rsvg_node_ref (newnode);
67c457
     }
67c457
 
67c457
+    handle->priv->num_loaded_elements += 1;
67c457
     handle->priv->currentnode = rsvg_node_ref (newnode);
67c457
 
67c457
     node_set_atts (newnode, handle, creator, atts);
67c457
@@ -1641,6 +1659,52 @@ rsvg_push_discrete_layer (RsvgDrawingCtx * ctx)
67c457
     ctx->render->push_discrete_layer (ctx);
67c457
 }
67c457
 
67c457
+void
67c457
+rsvg_drawing_ctx_increase_num_elements_acquired (RsvgDrawingCtx *draw_ctx)
67c457
+{
67c457
+    draw_ctx->num_elements_acquired++;
67c457
+}
67c457
+
67c457
+/* This is a mitigation for the security-related bugs:
67c457
+ * https://gitlab.gnome.org/GNOME/librsvg/issues/323
67c457
+ * https://gitlab.gnome.org/GNOME/librsvg/issues/515
67c457
+ *
67c457
+ * Imagine the XML [billion laughs attack], but done in SVG's terms:
67c457
+ *
67c457
+ * - #323 above creates deeply nested groups of `<use>` elements.
67c457
+ * The first one references the second one ten times, the second one
67c457
+ * references the third one ten times, and so on.  In the file given,
67c457
+ * this causes 10^17 objects to be rendered.  While this does not
67c457
+ * exhaust memory, it would take a really long time.
67c457
+ *
67c457
+ * - #515 has deeply nested references of `<pattern>` elements.  Each
67c457
+ * object inside each pattern has an attribute
67c457
+ * fill="url(#next_pattern)", so the number of final rendered objects
67c457
+ * grows exponentially.
67c457
+ *
67c457
+ * We deal with both cases by placing a limit on how many references
67c457
+ * will be resolved during the SVG rendering process, that is,
67c457
+ * how many `url(#foo)` will be resolved.
67c457
+ *
67c457
+ * [billion laughs attack]: https://bitbucket.org/tiran/defusedxml
67c457
+ */
67c457
+static gboolean
67c457
+limits_exceeded (RsvgDrawingCtx *draw_ctx)
67c457
+{
67c457
+    return draw_ctx->num_elements_acquired > 500000;
67c457
+}
67c457
+
67c457
+RsvgNode *
67c457
+rsvg_drawing_ctx_acquire_node_ref (RsvgDrawingCtx * ctx, RsvgNode *node)
67c457
+{
67c457
+  if (g_slist_find (ctx->acquired_nodes, node))
67c457
+    return NULL;
67c457
+
67c457
+  ctx->acquired_nodes = g_slist_prepend (ctx->acquired_nodes, node);
67c457
+
67c457
+  return node;
67c457
+}
67c457
+
67c457
 /*
67c457
  * rsvg_drawing_ctx_acquire_node:
67c457
  * @ctx: The drawing context in use
67c457
@@ -1668,16 +1732,15 @@ rsvg_drawing_ctx_acquire_node (RsvgDrawingCtx * ctx, const char *url)
67c457
   if (url == NULL)
67c457
       return NULL;
67c457
 
67c457
+  rsvg_drawing_ctx_increase_num_elements_acquired (ctx);
67c457
+  if (limits_exceeded (ctx))
67c457
+      return NULL;
67c457
+
67c457
   node = rsvg_defs_lookup (ctx->defs, url);
67c457
   if (node == NULL)
67c457
     return NULL;
67c457
 
67c457
-  if (g_slist_find (ctx->acquired_nodes, node))
67c457
-    return NULL;
67c457
-
67c457
-  ctx->acquired_nodes = g_slist_prepend (ctx->acquired_nodes, node);
67c457
-
67c457
-  return node;
67c457
+  return rsvg_drawing_ctx_acquire_node_ref (ctx, node);
67c457
 }
67c457
 
67c457
 /**
67c457
@@ -1734,18 +1797,9 @@ rsvg_drawing_ctx_release_node (RsvgDrawingCtx * ctx, RsvgNode *node)
67c457
   if (node == NULL)
67c457
     return;
67c457
 
67c457
-  g_return_if_fail (ctx->acquired_nodes != NULL);
67c457
-  g_return_if_fail (ctx->acquired_nodes->data == node);
67c457
-
67c457
   ctx->acquired_nodes = g_slist_remove (ctx->acquired_nodes, node);
67c457
 }
67c457
 
67c457
-void
67c457
-rsvg_drawing_ctx_increase_num_elements_rendered_through_use (RsvgDrawingCtx *draw_ctx)
67c457
-{
67c457
-    draw_ctx->num_elements_rendered_through_use++;
67c457
-}
67c457
-
67c457
 void
67c457
 rsvg_drawing_ctx_add_node_and_ancestors_to_stack (RsvgDrawingCtx *draw_ctx, RsvgNode *node)
67c457
 {
67c457
@@ -1759,12 +1813,6 @@ rsvg_drawing_ctx_add_node_and_ancestors_to_stack (RsvgDrawingCtx *draw_ctx, Rsvg
67c457
     }
67c457
 }
67c457
 
67c457
-static gboolean
67c457
-limits_exceeded (RsvgDrawingCtx *draw_ctx)
67c457
-{
67c457
-    return draw_ctx->num_elements_rendered_through_use > 500000;
67c457
-}
67c457
-
67c457
 gboolean
67c457
 rsvg_drawing_ctx_draw_node_from_stack (RsvgDrawingCtx *ctx, RsvgNode *node, int dominate)
67c457
 {
67c457
diff --git a/librsvg/rsvg-private.h b/librsvg/rsvg-private.h
67c457
index aeec8d5..06f4c2b 100644
67c457
--- a/librsvg/rsvg-private.h
67c457
+++ b/librsvg/rsvg-private.h
67c457
@@ -164,6 +164,7 @@ struct RsvgHandlePrivate {
67c457
      */
67c457
     RsvgSaxHandler *handler;
67c457
     int handler_nest;
67c457
+    gsize num_loaded_elements;
67c457
 
67c457
     GHashTable *entities;       /* g_malloc'd string -> xmlEntityPtr */
67c457
 
67c457
@@ -200,7 +201,7 @@ struct RsvgDrawingCtx {
67c457
     RsvgState *state;
67c457
     GError **error;
67c457
     RsvgDefs *defs;
67c457
-    gsize num_elements_rendered_through_use;
67c457
+    gsize num_elements_acquired;
67c457
     PangoContext *pango_context;
67c457
     double dpi_x, dpi_y;
67c457
     RsvgViewBox vb;
67c457
@@ -502,6 +503,8 @@ RsvgNode *rsvg_drawing_ctx_acquire_node         (RsvgDrawingCtx * ctx, const cha
67c457
 G_GNUC_INTERNAL
67c457
 RsvgNode *rsvg_drawing_ctx_acquire_node_of_type (RsvgDrawingCtx * ctx, const char *url, RsvgNodeType type);
67c457
 G_GNUC_INTERNAL
67c457
+RsvgNode *rsvg_drawing_ctx_acquire_node_ref     (RsvgDrawingCtx * ctx, RsvgNode *node);
67c457
+G_GNUC_INTERNAL
67c457
 void rsvg_drawing_ctx_release_node              (RsvgDrawingCtx * ctx, RsvgNode *node);
67c457
 
67c457
 G_GNUC_INTERNAL
67c457
diff --git a/rsvg_internals/src/drawing_ctx.rs b/rsvg_internals/src/drawing_ctx.rs
67c457
index 79f0c9f..631b073 100644
67c457
--- a/rsvg_internals/src/drawing_ctx.rs
67c457
+++ b/rsvg_internals/src/drawing_ctx.rs
67c457
@@ -32,6 +32,11 @@ extern "C" {
67c457
 
67c457
     fn rsvg_drawing_ctx_pop_view_box(draw_ctx: *const RsvgDrawingCtx);
67c457
 
67c457
+    fn rsvg_drawing_ctx_acquire_node_ref(
67c457
+        draw_ctx: *const RsvgDrawingCtx,
67c457
+        node: *const RsvgNode,
67c457
+    ) -> *mut RsvgNode;
67c457
+
67c457
     fn rsvg_drawing_ctx_acquire_node(
67c457
         draw_ctx: *const RsvgDrawingCtx,
67c457
         url: *const libc::c_char,
67c457
@@ -45,8 +50,6 @@ extern "C" {
67c457
 
67c457
     fn rsvg_drawing_ctx_release_node(draw_ctx: *const RsvgDrawingCtx, node: *mut RsvgNode);
67c457
 
67c457
-    fn rsvg_drawing_ctx_increase_num_elements_rendered_through_use(draw_ctx: *const RsvgDrawingCtx);
67c457
-
67c457
     fn rsvg_drawing_ctx_get_current_state_affine(draw_ctx: *const RsvgDrawingCtx) -> cairo::Matrix;
67c457
 
67c457
     fn rsvg_drawing_ctx_set_current_state_affine(
67c457
@@ -149,6 +152,16 @@ pub fn pop_view_box(draw_ctx: *const RsvgDrawingCtx) {
67c457
     }
67c457
 }
67c457
 
67c457
+pub fn acquire_node_ref(draw_ctx: *const RsvgDrawingCtx, node: *const RsvgNode) -> Option<AcquiredNode> {
67c457
+    let raw_node = unsafe { rsvg_drawing_ctx_acquire_node_ref(draw_ctx, node) };
67c457
+
67c457
+    if raw_node.is_null() {
67c457
+        None
67c457
+    } else {
67c457
+        Some(AcquiredNode(draw_ctx, raw_node))
67c457
+    }
67c457
+}
67c457
+
67c457
 pub fn get_acquired_node(draw_ctx: *const RsvgDrawingCtx, url: &str) -> Option<AcquiredNode> {
67c457
     let raw_node = unsafe { rsvg_drawing_ctx_acquire_node(draw_ctx, str::to_glib_none(url).0) };
67c457
 
67c457
@@ -290,12 +303,6 @@ pub fn state_pop(draw_ctx: *const RsvgDrawingCtx) {
67c457
     }
67c457
 }
67c457
 
67c457
-pub fn increase_num_elements_rendered_through_use(draw_ctx: *const RsvgDrawingCtx) {
67c457
-    unsafe {
67c457
-        rsvg_drawing_ctx_increase_num_elements_rendered_through_use(draw_ctx);
67c457
-    }
67c457
-}
67c457
-
67c457
 pub struct AcquiredNode(*const RsvgDrawingCtx, *mut RsvgNode);
67c457
 
67c457
 impl Drop for AcquiredNode {
67c457
diff --git a/rsvg_internals/src/structure.rs b/rsvg_internals/src/structure.rs
67c457
index 71c9ff0..e4234ae 100644
67c457
--- a/rsvg_internals/src/structure.rs
67c457
+++ b/rsvg_internals/src/structure.rs
67c457
@@ -278,6 +278,20 @@ impl NodeTrait for NodeUse {
67c457
             return;
67c457
         }
67c457
 
67c457
+        // <use> is an element that is used directly, unlike
67c457
+        // <pattern>, which is used through a fill="url(#...)"
67c457
+        // reference.  However, <use> will always reference another
67c457
+        // element, potentially itself or an ancestor of itself (or
67c457
+        // another <use> which references the first one, etc.).  So,
67c457
+        // we acquire the <use> element itself so that circular
67c457
+        // references can be caught.
67c457
+        let self_box = box_node(node.clone());
67c457
+        let self_acquired = drawing_ctx::acquire_node_ref(draw_ctx, self_box);
67c457
+        rsvg_node_unref(self_box);
67c457
+        if self_acquired.is_none() {
67c457
+            return;
67c457
+        }
67c457
+
67c457
         let child = if let Some(acquired) =
67c457
             drawing_ctx::get_acquired_node(draw_ctx, link.as_ref().unwrap())
67c457
         {
67c457
@@ -286,13 +300,6 @@ impl NodeTrait for NodeUse {
67c457
             return;
67c457
         };
67c457
 
67c457
-        if Node::is_ancestor(node.clone(), child.clone()) {
67c457
-            // or, if we're <use>'ing ourselves
67c457
-            return;
67c457
-        }
67c457
-
67c457
-        drawing_ctx::increase_num_elements_rendered_through_use(draw_ctx);
67c457
-
67c457
         let nx = self.x.get().normalize(draw_ctx);
67c457
         let ny = self.y.get().normalize(draw_ctx);
67c457
 
67c457
diff --git a/tests/errors.c b/tests/errors.c
67c457
index f370d60..ab5898a 100644
67c457
--- a/tests/errors.c
67c457
+++ b/tests/errors.c
67c457
@@ -22,10 +22,29 @@ get_test_filename (const char *basename) {
67c457
                              basename,
67c457
                              NULL);
67c457
 }
67c457
+
67c457
+static void
67c457
+test_loading_error (gconstpointer data)
67c457
+{
67c457
+    const char *basename = data;
67c457
+    char *filename = get_test_filename (basename);
67c457
+    RsvgHandle *handle;
67c457
+    GError *error = NULL;
67c457
+
67c457
+    handle = rsvg_handle_new_from_file (filename, &error);
67c457
+    g_free (filename);
67c457
+
67c457
+    g_assert (handle == NULL);
67c457
+    g_assert (g_error_matches (error, RSVG_ERROR, RSVG_ERROR_FAILED));
67c457
+
67c457
+    g_error_free (error);
67c457
+}
67c457
+
67c457
 static void
67c457
-test_instancing_limit (void)
67c457
+test_instancing_limit (gconstpointer data)
67c457
 {
67c457
-    char *filename = get_test_filename ("323-nested-use.svg");
67c457
+    const char *basename = data;
67c457
+    char *filename = get_test_filename (basename);
67c457
     RsvgHandle *handle;
67c457
     GError *error = NULL;
67c457
     cairo_surface_t *surf;
67c457
@@ -49,7 +68,34 @@ main (int argc, char **argv)
67c457
 {
67c457
     g_test_init (&argc, &argv, NULL);
67c457
 
67c457
-    g_test_add_func ("/errors/instancing_limit", test_instancing_limit);
67c457
+    g_test_add_data_func_full ("/errors/instancing_limit/323-nested-use.svg",
67c457
+                               "323-nested-use.svg",
67c457
+                               test_instancing_limit,
67c457
+                               NULL);
67c457
+
67c457
+    g_test_add_data_func_full ("/errors/instancing_limit/515-pattern-billion-laughs.svg",
67c457
+                               "515-pattern-billion-laughs.svg",
67c457
+                               test_instancing_limit,
67c457
+                               NULL);
67c457
+
67c457
+    g_test_add_data_func_full ("/errors/instancing_limit/308-use-self-ref.svg",
67c457
+                               "308-use-self-ref.svg",
67c457
+                               test_instancing_limit,
67c457
+                               NULL);
67c457
+    g_test_add_data_func_full ("/errors/instancing_limit/308-recursive-use.svg",
67c457
+                               "308-recursive-use.svg",
67c457
+                               test_instancing_limit,
67c457
+                               NULL);
67c457
+    g_test_add_data_func_full ("/errors/instancing_limit/308-doubly-recursive-use.svg",
67c457
+                               "308-doubly-recursive-use.svg",
67c457
+                               test_instancing_limit,
67c457
+                               NULL);
67c457
+
67c457
+    g_test_add_data_func_full ("/errors/515-too-many-elements.svgz",
67c457
+                               "515-too-many-elements.svgz",
67c457
+                               test_loading_error,
67c457
+                               NULL);
67c457
+
67c457
 
67c457
     return g_test_run ();
67c457
 }
67c457
diff --git a/tests/fixtures/errors/308-doubly-recursive-use.svg b/tests/fixtures/errors/308-doubly-recursive-use.svg
67c457
new file mode 100644
67c457
index 0000000..9b248a6
67c457
--- /dev/null
67c457
+++ b/tests/fixtures/errors/308-doubly-recursive-use.svg
67c457
@@ -0,0 +1,13 @@
67c457
+<svg>
67c457
+  <defs>
67c457
+    <g id="one">
67c457
+      <use xlink:href="#two"/>
67c457
+    </g>
67c457
+
67c457
+    <g id="two">
67c457
+      <use xlink:href="#one"/>
67c457
+    </g>
67c457
+  </defs>
67c457
+
67c457
+  <use xlink:href="#one"/>
67c457
+</svg>
67c457
diff --git a/tests/fixtures/errors/308-recursive-use.svg b/tests/fixtures/errors/308-recursive-use.svg
67c457
new file mode 100644
67c457
index 0000000..f5d00bf
67c457
--- /dev/null
67c457
+++ b/tests/fixtures/errors/308-recursive-use.svg
67c457
@@ -0,0 +1,9 @@
67c457
+<svg>
67c457
+  <defs>
67c457
+    <g id="one">
67c457
+      <use xlink:href="#one"/>
67c457
+    </g>
67c457
+  </defs>
67c457
+
67c457
+  <use xlink:href="#one"/>
67c457
+</svg>
67c457
diff --git a/tests/fixtures/errors/308-use-self-ref.svg b/tests/fixtures/errors/308-use-self-ref.svg
67c457
new file mode 100644
67c457
index 0000000..dbf14c5
67c457
--- /dev/null
67c457
+++ b/tests/fixtures/errors/308-use-self-ref.svg
67c457
@@ -0,0 +1,7 @@
67c457
+<svg>
67c457
+  <defs>
67c457
+    <use id="one" xlink:href="#one"/>
67c457
+  </defs>
67c457
+
67c457
+  <use xlink:href="#one"/>
67c457
+</svg>
67c457
diff --git a/tests/fixtures/errors/515-pattern-billion-laughs.svg b/tests/fixtures/errors/515-pattern-billion-laughs.svg
67c457
new file mode 100644
67c457
index 0000000..a306960
67c457
--- /dev/null
67c457
+++ b/tests/fixtures/errors/515-pattern-billion-laughs.svg
67c457
@@ -0,0 +1,130 @@
67c457
+
67c457
+
67c457
+  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
67c457
+
67c457
+     xmlns="http://www.w3.org/2000/svg">
67c457
+  <defs>
67c457
+    <pattern id="z" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(10,10)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="url(#i)" stroke="yellow"/>
67c457
+    </pattern>
67c457
+
67c457
+	<pattern id="i" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="url(#h)" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="url(#h)" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="url(#h)" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="url(#h)" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="url(#h)" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="url(#h)" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="url(#h)" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="url(#h)" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="url(#h)" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="url(#h)" stroke="cyan" />
67c457
+    </pattern>
67c457
+
67c457
+	<pattern id="h" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="url(#g)" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="url(#g)" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="url(#g)" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="url(#g)" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="url(#g)" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="url(#g)" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="url(#g)" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="url(#g)" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="url(#g)" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="url(#g)" stroke="cyan" />
67c457
+    </pattern>
67c457
+
67c457
+	<pattern id="g" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="url(#f)" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="url(#f)" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="url(#f)" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="url(#f)" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="url(#f)" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="url(#f)" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="url(#f)" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="url(#f)" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="url(#f)" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="url(#f)" stroke="cyan" />
67c457
+    </pattern>
67c457
+
67c457
+	<pattern id="f" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="url(#e)" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="url(#e)" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="url(#e)" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="url(#e)" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="url(#e)" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="url(#e)" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="url(#e)" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="url(#e)" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="url(#e)" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="url(#e)" stroke="cyan" />
67c457
+    </pattern>
67c457
+
67c457
+	<pattern id="e" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="url(#d)" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="url(#d)" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="url(#d)" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="url(#d)" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="url(#d)" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="url(#d)" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="url(#d)" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="url(#d)" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="url(#d)" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="url(#d)" stroke="cyan" />
67c457
+    </pattern>
67c457
+
67c457
+    <pattern id="d" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="url(#c)" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="url(#c)" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="url(#c)" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="url(#c)" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="url(#c)" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="url(#c)" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="url(#c)" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="url(#c)" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="url(#c)" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="url(#c)" stroke="cyan" />
67c457
+    </pattern>
67c457
+    <pattern id="c" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="url(#b)" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="url(#b)" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="url(#b)" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="url(#b)" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="url(#b)" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="url(#b)" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="url(#b)" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="url(#b)" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="url(#b)" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="url(#b)" stroke="cyan" />
67c457
+    </pattern>
67c457
+    <pattern id="b" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+	  <rect x="0" y="0" width="20" height="20" fill="url(#a)" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="url(#a)" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="url(#a)" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="url(#a)" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="url(#a)" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="url(#a)" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="url(#a)" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="url(#a)" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="url(#a)" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="url(#a)" stroke="cyan" />
67c457
+
67c457
+    </pattern>
67c457
+    <pattern id="a" x="0" y="0" width="20" height="20" patternUnits="userSpaceOnUse" patternTransform="scale(0.5,0.5)">
67c457
+      <rect x="0" y="0" width="20" height="20" fill="none" stroke="green" />
67c457
+	  <rect x="1" y="1" width="20" height="20" fill="none" stroke="brown" />
67c457
+	  <rect x="2" y="2" width="20" height="20" fill="none" stroke="pink" />
67c457
+	  <rect x="3" y="3" width="20" height="20" fill="none" stroke="grey" />
67c457
+	  <rect x="4" y="3" width="20" height="20" fill="none" stroke="cyan" />
67c457
+	  <rect x="5" y="3" width="20" height="20" fill="none" stroke="green" />
67c457
+	  <rect x="6" y="3" width="20" height="20" fill="none" stroke="brown" />
67c457
+	  <rect x="7" y="3" width="20" height="20" fill="none" stroke="pink" />
67c457
+	  <rect x="8" y="3" width="20" height="20" fill="none" stroke="grey" />
67c457
+	  <rect x="9" y="3" width="20" height="20" fill="none" stroke="cyan" />
67c457
+    </pattern>
67c457
+  </defs>
67c457
+
67c457
+  
67c457
+           cx="400" cy="200" rx="350" ry="150" />
67c457
+
67c457
+</svg>
67c457
\ No newline at end of file
67c457
diff --git a/tests/fixtures/errors/515-too-many-elements.svgz b/tests/fixtures/errors/515-too-many-elements.svgz
67c457
new file mode 100644
67c457
index 0000000000000000000000000000000000000000..a7f7cf678ca2f29af6df61078d1c6a86c73c2d1a
67c457
GIT binary patch
67c457
literal 40811
67c457
zcmeIuO)I1U007{3c1mhf$VD-7q(+J1MDM}L%|UFTsL8?1JBN{yP&im}QQ{&|QhvZj
67c457
zljI=9MY%ail5kSH<)g@tkhY%ZCp
67c457
zerz~+JUc3x2`j>*LibxH_PE;`Ph^fuo4Xpr-qW6!wVUeOr_1r`p~-)LTcZB@uIs(k
67c457
zp^3xx{A%Lt>ENbsBzwPKxl_B*S@%4>{ZPBL{_?u~dmaM@3>YwAz<>b*1`HT5V8DO@
67c457
z0|pEjFkrxd0RsjM7%*VKfB^#r3>YwAz<>b*1`HT5V8DO@0|pEjFkrxd0RsjM7%*Vq
67c457
ziwEvbqN?)X)ARdf*>V8`1`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r3>YwAz<>b*
67c457
z1`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r3>YwA;JXJdqN
67c457
zYVLd>)r0{91`HT5V8DO@0|pEjFkrxd0RsjM7%*VKfB^#r3>f$?1`2;_+E#M0g
67c457
I9YGlT0Agk%tpET3
67c457
67c457
literal 0
67c457
HcmV?d00001
67c457
67c457
-- 
67c457
2.26.2
67c457