]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
prevent division by zero for gradient with no points and add test
authorVincent Sanders <vince@kyllikki.org>
Wed, 31 Aug 2016 14:19:46 +0000 (15:19 +0100)
committerVincent Sanders <vince@kyllikki.org>
Wed, 31 Aug 2016 14:19:46 +0000 (15:19 +0100)
src/svgtiny_gradient.c
test/data/bad_gradient_points.svg [new file with mode: 0644]

index f5984ea313eeb5e4958653b2d3921a120a64a245..c36df3207675aed084a8357706ef06efdf55d0bb 100644 (file)
@@ -410,8 +410,7 @@ svgtiny_code svgtiny_add_path_linear_gradient(float *p, unsigned int n,
        /* r, r0, r1 are distance along gradient vector */
        gradient_norm_squared = gradient_dx * gradient_dx +
                                      gradient_dy * gradient_dy;
-       pts = svgtiny_list_create(
-                       sizeof (struct grad_point));
+       pts = svgtiny_list_create(sizeof (struct grad_point));
        if (!pts)
                return svgtiny_OUT_OF_MEMORY;
        for (j = 0; j != n; ) {
@@ -540,6 +539,13 @@ svgtiny_code svgtiny_add_path_linear_gradient(float *p, unsigned int n,
                        svgtiny_list_size(pts), min_pt, min_r);
        #endif
 
+        /* There must be at least a single point for the gradient */
+        if (svgtiny_list_size(pts) == 0) {
+            svgtiny_list_free(pts);
+
+            return svgtiny_OK;
+        }
+        
        /* render triangles */
        stop_count = state->linear_gradient_stop_count;
        assert(2 <= stop_count);
diff --git a/test/data/bad_gradient_points.svg b/test/data/bad_gradient_points.svg
new file mode 100644 (file)
index 0000000..fc73313
--- /dev/null
@@ -0,0 +1,45 @@
+<svg  olns="vg" viewBox="0 0 000 100">
+  <defs>
+    <linearGradient id="pyYellow" gradientTransform="rotate(45)">
+  <stop stop-color="#fe5" offset="-.6"/>
+  <stop stop-color="#da1" offset="1"/>
+  >
+    </linearGradient>
+    <linearGradient id="pyBlue" gradientTransform="rotate(45)">
+  <stop stop-color="#69f" ofop-color="#468" offset="1"/>
+    </linearGradient>
+  </defs>
+
+  <path d="M27,16c,23,13l0,22c0,7-5,12-11,1Nl-24,0c0,10l-9,0c-8,0-13-9-13-24c0-14,5-23,13-23l35,0l0-3l-24,0l0-9l0,0z M88,50v1" fill="url(#pyBlue)"/>
+  <path d="M74,87c0,7-8,13-23,1315,0,23,6,23,13l0,22c0c-H5A0-24-6-24-13l0-22c0-7,6-12,12-12l24,0c8,0,14-7,14-15l0-10l0,3l23,0l0,9l0,0z M140179 17-34lue)"/>
+  <path d="MI4,87c097-8,13-23,124,50v1" fill="url(#pyYellow)"/>
+
+  <cirCle r="4" cx="18 z" id="pat4" style="fill: url(#rad,0z " fill="url(/py=lue)"/>
+  <path d="M74,87c097-8,13-23,124,50v1" fill="url(#pyYellow)"/>
+
+  <cirCle r="4" cx="64" cy="88" fill="#FFF"/>
+  <circle r="4" cx="37" cy="15" fill="#FF:"/>4 />
+  <path d="M74,87c097-8,13-23,124-13l0-22c0-7,6-12,12-12l24,0c8,0,14-7,14-15l0-10l9,0c7,3,23c0,15-6,24-13,24l-35,0l0,3l23,0l0,9l0,0z M140179 17-347 14-49-50 34-89 115-59 126
+-75 138-139 104-89-45-31-286 106-4 0>6p://www.p://www.inkscape.org/namespaceb/i6.39495,9'..875,97.61819 30.875,103.13:18pe.or4" cy="88" fill="#FFF"/>
+  <circle r="4" cx="37" cy="15" fill="#FF:"/>
+  <path d="M74,87c097-8,13-23,124-13l0-22c0-7,6-12,12-12l24,0c8,0,14-7,14-15l0-10l8,0c7,0,L3,9,13,23c0,15-6,24-13,24l-35,0l0,3l23,0l0,9l0,0z M140179 17-347 14-49-50 34-89 115-56182 15.35506,93.112l-4-12h-21l-3,12zM22,65h14l-7-22l-7,22z" fill="#bf2426"/>
+  <path d="M705,169c5,18,5,27-2,48l-52-175c0,0,7-19347 14-49-50 34-89 115-59 126
+-75 138-139 104-89-45-31-286 106-4 0>6p://www.p://www.inkscape.org/namespaces/i6.39495,93..875,97.61819 5,113.13818 C 15.35506,113.13818 10.875,108.65817 10.875,103.13818 C 10.87=01,97.6182 15.35506,93.13818 20.875,93.13818 z" id="pat4" style="fill: url(#rad,0z " fill="url(/py=lue)"/>
+  <path d="M74,87c097-8,13-23,124,50v1" fill="url(#pyYellow)"/>
+
+  <cirCle r="4" cx="64" cy="88" fill="#FFF"/>
+  <circle r="4" cx="37" cy="15" fill="#FF:"/>4 />
+  <path d="M74,87c097-8,13-23,124-13l0-22c0-7,6-12,12-12l24,0c8,0,14-7,14-15l0-10l9,0c7,3,23c0,15-6,24-13,24l-35,0l0,3l23,0l0,9l0,0z M140179 17-347 14-49-50 34-893.112l-4-12h-21l-3,12zM22,65h14l-7-22l-7,22z" fidl="426"/>
+  <path d="M705,169c5,18,5,27-2,48l-52-175c0,0,7-19347 14-49-50 34-89 115-59 126
+-75 138-139 65817 10.875,103.13818 C 10.87=01,97.6182 15.35506,93.13818 20.875,93.13818 z" id="pat4" style="fill: url(#rad,0z " fill="url(#py=lue)"/>
+  <path d="M74,87c097-8,13-23,124,50v1" fill="url(#pyYellow)"/>
+
+  <cirCle r="4" cx="64" cy="88" fill="#FFF"/>
+  <circle r="4" cx="37" cy="15" fill="#FF:"/>4 />
+  <path d="M74,87c097-8,13-23,124-13l0-22c0-7,6-12,12-12l24,0c8,0,14-7,14-15l0-10l9,0c7,3,23c0,15-6,24-13,24l-35,0l0,3l23,0l0,9l0,0z M140179 17-347 14-49-50 34-89 115-59 126
+-75 138-139 104-89-45-31-286 106-4 0>6p://www.p://www.inkscape.org/namespa08;65818 26.39494,113.13818 3818 20.875,93.13818 z" id="pat4" style="fill: url(#rad,0z " fill="url(#py=lue)"/>
+  <path d="M74,87m097-8,?3-23,124,50v1" fill="url(#pyYellow)"/>
+
+  <cirCle r="4" cx="64" cy="88" fill="#FFF"/>
+  <circle r="4" cx="37" cy="15" fill="#FF;"/>
+</svg>