File GraphicsMagick-CVE-2016-2318.patch of Package GraphicsMagick.6323

Index: GraphicsMagick-1.3.21/coders/svg.c
===================================================================
--- GraphicsMagick-1.3.21.orig/coders/svg.c	2016-08-05 10:39:40.293684253 +0200
+++ GraphicsMagick-1.3.21/coders/svg.c	2016-08-05 10:39:40.301684382 +0200
@@ -217,7 +217,7 @@ static double GetUserSpaceCoordinateValu
 
   assert(string != (const char *) NULL);
   p=(char *) string;
-  GetToken(p,&p,token);
+  (void) MagickGetToken(p,&p,token,MaxTextExtent);
   value=MagickAtoF(token);
   if (strchr(token,'%') != (char *) NULL)
     {
@@ -233,7 +233,7 @@ static double GetUserSpaceCoordinateValu
       beta=value-svg_info->view_box.height;
       return(sqrt(alpha*alpha+beta*beta)/sqrt(2.0)/100.0);
     }
-  GetToken(p,&p,token);
+  (void) MagickGetToken(p,&p,token,MaxTextExtent);
   if (LocaleNCompare(token,"cm",2) == 0)
     return(72.0*svg_info->scale[0]/2.54*value);
   if (LocaleNCompare(token,"em",2) == 0)
@@ -647,6 +647,7 @@ static void SVGStartDocument(void *conte
   */
   (void) LogMagickEvent(CoderEvent,GetMagickModule(),"  SAX.startDocument()");
   svg_info=(SVGInfo *) context;
+  DestroyExceptionInfo(svg_info->exception);
   GetExceptionInfo(svg_info->exception);
   parser=svg_info->parser;
   svg_info->document=xmlNewDoc(parser->version);
@@ -686,19 +687,19 @@ static void SVGStartElement(void *contex
   const xmlChar **attributes)
 {
   char
-    *color,
+    *color = NULL,
     id[MaxTextExtent],
-    *p,
+    *p = NULL,
     token[MaxTextExtent],
     **tokens,
-    *units;
+    *units = NULL;
 
   const char
-    *keyword,
-    *value;
+    *keyword = NULL,
+    *value = NULL;
 
   int
-    number_tokens;
+    number_tokens = 0;
 
   SVGInfo
     *svg_info;
@@ -712,6 +713,8 @@ static void SVGStartElement(void *contex
   */
   (void) LogMagickEvent(CoderEvent,GetMagickModule(),
     "  SAX.startElement(%.1024s",name);
+  id[0]='\0';
+  token[0]='\0';
   svg_info=(SVGInfo *) context;
   svg_info->n++;
   MagickReallocMemory(double *,svg_info->scale,(svg_info->n+1)*sizeof(double));
@@ -726,7 +729,8 @@ static void SVGStartElement(void *contex
   units=AllocateString("userSpaceOnUse");
   value=(const char *) NULL;
   if (attributes != (const xmlChar **) NULL)
-    for (i=0; (attributes[i] != (const xmlChar *) NULL); i+=2)
+    for (i=0; (svg_info->exception->severity < ErrorException) &&
+           (attributes[i] != (const xmlChar *) NULL); i+=2)
     {
       keyword=(const char *) attributes[i];
       value=(const char *) attributes[i+1];
@@ -773,6 +777,12 @@ static void SVGStartElement(void *contex
             {
               svg_info->bounds.height=
                 GetUserSpaceCoordinateValue(svg_info,-1,value);
+              if (svg_info->bounds.height <= 0.0)
+                {
+                  ThrowException(svg_info->exception,CorruptImageError,
+                                 NegativeOrZeroImageSize,(char *) NULL);
+                  goto svg_start_element_error;
+                }
               break;
             }
           break;
@@ -805,6 +815,12 @@ static void SVGStartElement(void *contex
             {
               svg_info->bounds.width=
                 GetUserSpaceCoordinateValue(svg_info,1,value);
+              if (svg_info->bounds.width <= 0.0)
+                {
+                  ThrowException(svg_info->exception,CorruptImageError,
+                                 NegativeOrZeroImageSize,(char *) NULL);
+                  goto svg_start_element_error;
+                }
               break;
             }
           break;
@@ -1207,27 +1223,27 @@ static void SVGStartElement(void *contex
                     if (LocaleCompare(keyword,"matrix") == 0)
                       {
                         p=(char *) value;
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.sx=MagickAtoF(value);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.rx=MagickAtoF(token);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.ry=MagickAtoF(token);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.sy=MagickAtoF(token);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.tx=MagickAtoF(token);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.ty=MagickAtoF(token);
                         break;
                       }
@@ -1339,6 +1355,12 @@ static void SVGStartElement(void *contex
             {
               svg_info->bounds.height=
                 GetUserSpaceCoordinateValue(svg_info,-1,value);
+              if (svg_info->bounds.height <= 0.0)
+                {
+                  ThrowException(svg_info->exception,CorruptImageError,
+                                 NegativeOrZeroImageSize,(char *) NULL);
+                  goto svg_start_element_error;
+                }
               break;
             }
           if (LocaleCompare(keyword,"href") == 0)
@@ -1777,27 +1799,27 @@ static void SVGStartElement(void *contex
                     if (LocaleCompare(keyword,"matrix") == 0)
                       {
                         p=(char *) value;
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.sx=MagickAtoF(value);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.rx=MagickAtoF(token);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.ry=MagickAtoF(token);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.sy=MagickAtoF(token);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.tx=MagickAtoF(token);
-                        GetToken(p,&p,token);
+                        (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         if (*token == ',')
-                          GetToken(p,&p,token);
+                          (void) MagickGetToken(p,&p,token,MaxTextExtent);
                         affine.ty=MagickAtoF(token);
                         break;
                       }
@@ -1908,22 +1930,30 @@ static void SVGStartElement(void *contex
           if (LocaleCompare(keyword,"viewBox") == 0)
             {
               p=(char *) value;
-              GetToken(p,&p,token);
+              (void) MagickGetToken(p,&p,token,MaxTextExtent);
               svg_info->view_box.x=MagickAtoF(token);
-              GetToken(p,&p,token);
+              (void) MagickGetToken(p,&p,token,MaxTextExtent);
               if (*token == ',')
-                GetToken(p,&p,token);
+                (void) MagickGetToken(p,&p,token,MaxTextExtent);
               svg_info->view_box.y=MagickAtoF(token);
-              GetToken(p,&p,token);
+              (void) MagickGetToken(p,&p,token,MaxTextExtent);
               if (*token == ',')
-                GetToken(p,&p,token);
+                (void) MagickGetToken(p,&p,token,MaxTextExtent);
               svg_info->view_box.width=MagickAtoF(token);
-              if (svg_info->bounds.width == 0)
-                svg_info->bounds.width=svg_info->view_box.width;
-              GetToken(p,&p,token);
+              (void) MagickGetToken(p,&p,token,MaxTextExtent);
               if (*token == ',')
-                GetToken(p,&p,token);
+                (void) MagickGetToken(p,&p,token,MaxTextExtent);
               svg_info->view_box.height=MagickAtoF(token);
+              if (svg_info->view_box.width < 0.0 ||
+                  svg_info->view_box.height < 0.0)
+                {
+                  ThrowException(svg_info->exception,CorruptImageError,
+                                 NegativeOrZeroImageSize,
+                                 svg_info->image->filename);
+                  goto svg_start_element_error;
+                }
+              if (svg_info->bounds.width == 0)
+                svg_info->bounds.width=svg_info->view_box.width;
               if (svg_info->bounds.height == 0)
                 svg_info->bounds.height=svg_info->view_box.height;
               break;
@@ -1937,6 +1967,12 @@ static void SVGStartElement(void *contex
             {
               svg_info->bounds.width=
                 GetUserSpaceCoordinateValue(svg_info,1,value);
+              if (svg_info->bounds.width <= 0.0)
+                {
+                  ThrowException(svg_info->exception,CorruptImageError,
+                                 NegativeOrZeroImageSize,(char *) NULL);
+                  goto svg_start_element_error;
+                }
               break;
             }
           break;
@@ -2054,9 +2090,21 @@ static void SVGStartElement(void *contex
             sx,
             sy;
 
+          if (svg_info->bounds.width < 0.0 || svg_info->bounds.height < 0.0)
+            {
+              ThrowException(svg_info->exception,CorruptImageError,
+                             NegativeOrZeroImageSize,(char *) NULL);
+              goto svg_start_element_error;
+            }
           if ((svg_info->view_box.width == 0.0) ||
               (svg_info->view_box.height == 0.0))
             svg_info->view_box=svg_info->bounds;
+          if (svg_info->view_box.width < 0.0 || svg_info->view_box.height < 0.0)
+            {
+              ThrowException(svg_info->exception,CorruptImageError,
+                             NegativeOrZeroImageSize,(char *) NULL);
+              goto svg_start_element_error;
+            }
           SetGeometry(svg_info->image,&page);
           page.width=(unsigned long) svg_info->bounds.width;
           page.height=(unsigned long) svg_info->bounds.height;
@@ -2098,10 +2146,12 @@ static void SVGStartElement(void *contex
         }
     }
 #endif
+  /* Error dispatch point */
+ svg_start_element_error:;
+
   (void) LogMagickEvent(CoderEvent,GetMagickModule(),"  )");
   MagickFreeMemory(units);
-  if (color != (char *) NULL)
-    MagickFreeMemory(color);
+  MagickFreeMemory(color);
 }
 
 static void SVGEndElement(void *context,const xmlChar *name)
@@ -2502,7 +2552,7 @@ static void SVGWarning(void *context,con
 #else
   (void) vsnprintf(reason,MaxTextExtent,format,operands);
 #endif
-  ThrowException2(svg_info->exception,DelegateWarning,reason,(char *) NULL);
+  ThrowException2(svg_info->exception,CoderWarning,reason,(char *) NULL);
   va_end(operands);
 }
 
@@ -2774,6 +2824,7 @@ static Image *ReadSVGImage(const ImageIn
   /*
     Free resources.
   */
+  MagickFreeMemory(svg_info.size);
   if (svg_info.title != (char *) NULL)
     {
       if (image != (Image *) NULL)
@@ -2786,6 +2837,7 @@ static Image *ReadSVGImage(const ImageIn
         (void) SetImageAttribute(image,"comment",svg_info.comment);
       MagickFreeMemory(svg_info.comment);
     }
+  (void) memset(&svg_info,0,sizeof(SVGInfo));
   (void) LiberateTemporaryFile(filename);
   return(image);
 }
@@ -3116,7 +3168,8 @@ static unsigned int WriteSVGImage(const
     i;
 
   size_t
-    length;
+    length,
+    token_max_length;
 
   SVGInfo
     svg_info;
@@ -3163,6 +3216,7 @@ static unsigned int WriteSVGImage(const
     ThrowWriterException(ResourceLimitError,MemoryAllocationFailed,image);
   IdentityAffine(&affine);
   token=AllocateString(attribute->value);
+  token_max_length=strlen(token);
   active=False;
   n=0;
   status=True;
@@ -3171,7 +3225,7 @@ static unsigned int WriteSVGImage(const
     /*
       Interpret graphic primitive.
     */
-    GetToken(q,&q,keyword);
+    MagickGetToken(q,&q,keyword,MaxTextExtent);
     if (*keyword == '\0')
       break;
     if (*keyword == '#')
@@ -3207,33 +3261,33 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("affine",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             affine.sx=MagickAtoF(token);
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
             affine.rx=MagickAtoF(token);
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
             affine.ry=MagickAtoF(token);
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
             affine.sy=MagickAtoF(token);
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
             affine.tx=MagickAtoF(token);
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
             affine.ty=MagickAtoF(token);
             break;
           }
         if (LocaleCompare("angle",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             affine.rx=MagickAtoF(token);
             affine.ry=MagickAtoF(token);
             break;
@@ -3262,21 +3316,21 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("clip-path",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"clip-path:url(#%.1024s);",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("clip-rule",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"clip-rule:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("clip-units",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"clipPathUnits=%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3299,7 +3353,7 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("decorate",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"text-decoration:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3323,56 +3377,56 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("fill",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"fill:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("fill-rule",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"fill-rule:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("fill-opacity",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"fill-opacity:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("font-family",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"font-family:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("font-stretch",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"font-stretch:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("font-style",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"font-style:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("font-size",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"font-size:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("font-weight",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"font-weight:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3385,19 +3439,19 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("gradient-units",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             break;
           }
         if (LocaleCompare("text-align",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"text-align %.1024s ",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("text-anchor",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"text-anchor %.1024s ",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3410,7 +3464,7 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("image",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             primitive_type=ImagePrimitive;
             break;
           }
@@ -3444,7 +3498,7 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("opacity",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"opacity %.1024s ",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3477,7 +3531,7 @@ static unsigned int WriteSVGImage(const
           }
         if (LocaleCompare("pop",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (LocaleCompare("clip-path",token) == 0)
               {
                 (void) WriteBlobString(image,"</clipPath>\n");
@@ -3513,10 +3567,10 @@ static unsigned int WriteSVGImage(const
           }
         if (LocaleCompare("push",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (LocaleCompare("clip-path",token) == 0)
               {
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 FormatString(message,"<clipPath id=\"%s\">\n",token);
                 (void) WriteBlobString(image,message);
                 break;
@@ -3528,26 +3582,26 @@ static unsigned int WriteSVGImage(const
               }
             if (LocaleCompare("gradient",token) == 0)
               {
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 (void) strlcpy(name,token,MaxTextExtent);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 (void) strlcpy(type,token,MaxTextExtent);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 svg_info.segment.x1=MagickAtoF(token);
                 svg_info.element.cx=MagickAtoF(token);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 if (*token == ',')
-                  GetToken(q,&q,token);
+                  MagickGetToken(q,&q,token,token_max_length);
                 svg_info.segment.y1=MagickAtoF(token);
                 svg_info.element.cy=MagickAtoF(token);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 if (*token == ',')
-                  GetToken(q,&q,token);
+                  MagickGetToken(q,&q,token,token_max_length);
                 svg_info.segment.x2=MagickAtoF(token);
                 svg_info.element.major=MagickAtoF(token);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 if (*token == ',')
-                  GetToken(q,&q,token);
+                  MagickGetToken(q,&q,token,token_max_length);
                 svg_info.segment.y2=MagickAtoF(token);
                 svg_info.element.minor=MagickAtoF(token);
                 FormatString(message,"<%sGradient id=\"%s\" x1=\"%g\" "
@@ -3556,9 +3610,9 @@ static unsigned int WriteSVGImage(const
                   svg_info.segment.y2);
                 if (LocaleCompare(type,"radial") == 0)
                   {
-                    GetToken(q,&q,token);
+                    MagickGetToken(q,&q,token,token_max_length);
                     if (*token == ',')
-                      GetToken(q,&q,token);
+                      MagickGetToken(q,&q,token,token_max_length);
                     svg_info.element.angle=MagickAtoF(token);
                     FormatString(message,"<%sGradient id=\"%s\" cx=\"%g\" "
                       "cy=\"%g\" r=\"%g\" fx=\"%g\" fy=\"%g\">\n",type,name,
@@ -3582,21 +3636,21 @@ static unsigned int WriteSVGImage(const
               }
             if (LocaleCompare("pattern",token) == 0)
               {
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 (void) strlcpy(name,token,MaxTextExtent);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 svg_info.bounds.x=MagickAtoF(token);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 if (*token == ',')
-                  GetToken(q,&q,token);
+                  MagickGetToken(q,&q,token,token_max_length);
                 svg_info.bounds.y=MagickAtoF(token);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 if (*token == ',')
-                  GetToken(q,&q,token);
+                  MagickGetToken(q,&q,token,token_max_length);
                 svg_info.bounds.width=MagickAtoF(token);
-                GetToken(q,&q,token);
+                MagickGetToken(q,&q,token,token_max_length);
                 if (*token == ',')
-                  GetToken(q,&q,token);
+                  MagickGetToken(q,&q,token,token_max_length);
                 svg_info.bounds.height=MagickAtoF(token);
                 FormatString(message,"<pattern id=\"%s\" x=\"%g\" y=\"%g\" "
                   "width=\"%g\" height=\"%g\">\n",name,svg_info.bounds.x,
@@ -3625,7 +3679,7 @@ static unsigned int WriteSVGImage(const
           }
         if (LocaleCompare("rotate",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"rotate(%.1024s) ",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3638,24 +3692,24 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("scale",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             affine.sx=MagickAtoF(token);
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
             affine.sy=MagickAtoF(token);
             break;
           }
         if (LocaleCompare("skewX",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"skewX(%.1024s) ",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("skewY",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"skewY(%.1024s) ",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3665,9 +3719,9 @@ static unsigned int WriteSVGImage(const
             char
               color[MaxTextExtent];
 
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             (void) strlcpy(color,token,MaxTextExtent);
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,
               "  <stop offset=\"%s\" stop-color=\"%s\" />\n",token,color);
             (void) WriteBlobString(image,message);
@@ -3675,14 +3729,14 @@ static unsigned int WriteSVGImage(const
           }
         if (LocaleCompare("stroke",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("stroke-antialias",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke-antialias:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3695,62 +3749,62 @@ static unsigned int WriteSVGImage(const
                   k;
 
                 p=q;
-                GetToken(p,&p,token);
+                (void) MagickGetToken(p,&p,token,MaxTextExtent);
                 for (k=0; IsPoint(token); k++)
-                  GetToken(p,&p,token);
+                  (void) MagickGetToken(p,&p,token,MaxTextExtent);
                 (void) WriteBlobString(image,"stroke-dasharray:");
                 for (j=0; j < k; j++)
                 {
-                  GetToken(q,&q,token);
+                  MagickGetToken(q,&q,token,token_max_length);
                   FormatString(message,"%.1024s ",token);
                   (void) WriteBlobString(image,message);
                 }
                 (void) WriteBlobString(image,";");
                 break;
               }
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke-dasharray:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("stroke-dashoffset",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke-dashoffset:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("stroke-linecap",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke-linecap:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("stroke-linejoin",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke-linejoin:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("stroke-miterlimit",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke-miterlimit:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("stroke-opacity",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke-opacity:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
           }
         if (LocaleCompare("stroke-width",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"stroke-width:%.1024s;",token);
             (void) WriteBlobString(image,message);
             continue;
@@ -3768,7 +3822,7 @@ static unsigned int WriteSVGImage(const
           }
         if (LocaleCompare("text-antialias",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             FormatString(message,"text-antialias:%.1024s;",token);
             (void) WriteBlobString(image,message);
             break;
@@ -3780,11 +3834,11 @@ static unsigned int WriteSVGImage(const
           }
         if (LocaleCompare("translate",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             affine.tx=MagickAtoF(token);
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
             affine.ty=MagickAtoF(token);
             break;
           }
@@ -3796,16 +3850,16 @@ static unsigned int WriteSVGImage(const
       {
         if (LocaleCompare("viewbox",keyword) == 0)
           {
-            GetToken(q,&q,token);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
-            GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
-            GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
+            MagickGetToken(q,&q,token,token_max_length);
             if (*token == ',')
-              GetToken(q,&q,token);
-            GetToken(q,&q,token);
+              MagickGetToken(q,&q,token,token_max_length);
+            MagickGetToken(q,&q,token,token_max_length);
             break;
           }
         status=False;
@@ -3833,15 +3887,15 @@ static unsigned int WriteSVGImage(const
       */
       if (!IsPoint(q))
         break;
-      GetToken(q,&q,token);
+      MagickGetToken(q,&q,token,token_max_length);
       point.x=MagickAtoF(token);
-      GetToken(q,&q,token);
+      MagickGetToken(q,&q,token,token_max_length);
       if (*token == ',')
-        GetToken(q,&q,token);
+        MagickGetToken(q,&q,token,token_max_length);
       point.y=MagickAtoF(token);
-      GetToken(q,(char **) NULL,token);
+      MagickGetToken(q,(char **) NULL,token,token_max_length);
       if (*token == ',')
-        GetToken(q,&q,token);
+        MagickGetToken(q,&q,token,token_max_length);
       primitive_info[i].primitive=primitive_type;
       primitive_info[i].point=point;
       primitive_info[i].coordinates=0;
@@ -4036,7 +4090,7 @@ static unsigned int WriteSVGImage(const
         int
           number_attributes;
 
-        GetToken(q,&q,token);
+        MagickGetToken(q,&q,token,token_max_length);
         number_attributes=1;
         for (p=token; *p != '\0'; p++)
           if (isalpha((int) *p))
@@ -4066,7 +4120,7 @@ static unsigned int WriteSVGImage(const
             status=False;
             break;
           }
-        GetToken(q,&q,token);
+        MagickGetToken(q,&q,token,token_max_length);
         if (LocaleCompare("point",token) == 0)
           primitive_info[j].method=PointMethod;
         if (LocaleCompare("replace",token) == 0)
@@ -4089,7 +4143,7 @@ static unsigned int WriteSVGImage(const
             status=False;
             break;
           }
-        GetToken(q,&q,token);
+        MagickGetToken(q,&q,token,token_max_length);
         (void) FormatString(message,"  <text x=\"%g\" y=\"%g\">",
           primitive_info[j].point.x,primitive_info[j].point.y);
         (void) WriteBlobString(image,message);
@@ -4111,7 +4165,7 @@ static unsigned int WriteSVGImage(const
             status=False;
             break;
           }
-        GetToken(q,&q,token);
+        MagickGetToken(q,&q,token,token_max_length);
         (void) FormatString(message,"  <image x=\"%g\" y=\"%g\" "
           "width=\"%g\" height=\"%g\" xlink:href=\"%.1024s\"/>\n",
           primitive_info[j].point.x,primitive_info[j].point.y,
Index: GraphicsMagick-1.3.21/magick/utility.c
===================================================================
--- GraphicsMagick-1.3.21.orig/magick/utility.c	2016-08-05 10:39:40.173682324 +0200
+++ GraphicsMagick-1.3.21/magick/utility.c	2016-08-05 10:39:40.301684382 +0200
@@ -2591,98 +2591,7 @@ MagickExport void GetPathComponent(const
 */
 MagickExport void GetToken(const char *start,char **end,char *token)
 {
-  register char
-    *p;
-
-  register long
-    i;
-
-  double
-    double_val;
-
-  assert(start != (const char *) NULL);
-  assert(token != (char *) NULL);
-
-  i=0;
-  p=(char *) start;
-
-  if (*p != '\0')
-  {
-    while (isspace((int)(unsigned char) (*p)) && (*p != '\0'))
-      p++;
-    switch (*p)
-    {
-      case '"':
-      case '\'':
-      case '{':
-      {
-        register char
-          escape;
-
-        escape=(*p);
-        if (escape == '{')
-          escape='}';
-        for (p++; *p != '\0'; p++)
-        {
-          if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
-            p++;
-          else
-            if (*p == escape)
-              {
-                p++;
-                break;
-              }
-          token[i++]=(*p);
-        }
-        break;
-      }
-      default:
-      {
-        char
-          *q;
-
-        double_val=strtod(p,&q);
-        (void) double_val;
-        if (p != q)
-          {
-            for ( ; p < q; p++)
-              token[i++]=(*p);
-            if (*p == '%')
-              token[i++]=(*p++);
-            break;
-          }
-        if ((*p != '\0') && !isalpha((int) *p) && (*p != *DirectorySeparator) &&
-	    (*p != '#') && (*p != '<'))
-          {
-            token[i++]=(*p++);
-            break;
-          }
-        for ( ; *p != '\0'; p++)
-        {
-          if ((isspace((int)(unsigned char) *p) || (*p == '=')) && (*(p-1) != '\\'))
-            break;
-          token[i++]=(*p);
-          if (*p == '(')
-            for (p++; *p != '\0'; p++)
-            {
-              token[i++]=(*p);
-              if ((*p == ')') && (*(p-1) != '\\'))
-                break;
-            }
-        }
-        break;
-      }
-    }
-  }
-  token[i]='\0';
-  if (LocaleNCompare(token,"url(#",5) == 0)
-    {
-      i=(long) strlen(token);
-      (void) strlcpy(token,token+5,MaxTextExtent);
-      token[i-6]='\0';
-    }
-  if (end != (char **) NULL)
-    *end=p;
+  (void) MagickGetToken(start,end,token, MaxTextExtent);
 }
 
 /*
@@ -6297,3 +6206,124 @@ MagickExport char *TranslateTextEx(const
     MagickFreeMemory(text);
   return(translated_text);
 }
+
+MagickExport size_t MagickGetToken(const char *start,char **end,char *token,
+                                   const size_t buffer_length)
+{
+  register char
+    *p;
+
+  register size_t
+    i;
+
+  register size_t
+    length = buffer_length - 1;
+
+  double
+    double_val;
+
+  assert(start != (const char *) NULL);
+  assert(token != (char *) NULL);
+
+  i=0;
+  p=(char *) start;
+
+  if (*p != '\0')
+  {
+    while (isspace((int)(unsigned char) (*p)) && (*p != '\0'))
+      p++;
+    switch (*p)
+    {
+      case '"':
+      case '\'':
+      case '{':
+      {
+        register char
+          escape;
+
+        escape=(*p);
+        if (escape == '{')
+          escape='}';
+        for (p++; *p != '\0'; p++)
+        {
+          if ((*p == '\\') && ((*(p+1) == escape) || (*(p+1) == '\\')))
+            p++;
+          else
+            if (*p == escape)
+              {
+                p++;
+                break;
+              }
+          if (i < length)
+            token[i++]=(*p);
+        }
+        break;
+      }
+      default:
+      {
+        char
+          *q;
+
+        double_val=strtod(p,&q);
+        (void) double_val;
+        if (p != q)
+          {
+            for ( ; p < q; p++)
+              if (i < length)
+                token[i++]=(*p);
+            if (*p == '%')
+              if (i < length)
+                {
+                  token[i++]=(*p);
+                  p++;
+                }
+            break;
+          }
+        if ((*p != '\0') && !isalpha((int) *p) && (*p != *DirectorySeparator) &&
+	    (*p != '#') && (*p != '<'))
+          {
+            if (i < length)
+              {
+                token[i++]=(*p);
+                p++;
+              }
+            break;
+          }
+        for ( ; *p != '\0'; p++)
+        {
+          if ((isspace((int)(unsigned char) *p) || (*p == '=')) && (*(p-1) != '\\'))
+            break;
+          if (i < length)
+            token[i++]=(*p);
+          if (*p == '(')
+            for (p++; *p != '\0'; p++)
+            {
+              if (i < length)
+                token[i++]=(*p);
+              if ((*p == ')') && (*(p-1) != '\\'))
+                break;
+            }
+        }
+        break;
+      }
+    }
+  }
+  token[i]='\0';
+  {
+    char
+      *r;
+
+    /*
+      Parse token in form "url(#%s)"
+    */
+    if ((LocaleNCompare(token,"url(#",5) == 0) &&
+        ((r = strrchr(token,')')) != NULL))
+    {
+      *r='\0';
+      (void) memmove(token,token+5,r-token+1);
+    }
+  }
+  if (end != (char **) NULL)
+    *end=p;
+  return (p-start+1);
+}
Index: GraphicsMagick-1.3.21/magick/utility.h
===================================================================
--- GraphicsMagick-1.3.21.orig/magick/utility.h	2016-08-05 10:39:40.301684382 +0200
+++ GraphicsMagick-1.3.21/magick/utility.h	2016-08-05 10:42:31.748441700 +0200
@@ -156,6 +156,8 @@ extern MagickExport magick_int64_t
   MagickSizeStrToInt64(const char *str,const unsigned int kilo);
 
 extern MagickExport size_t
+  MagickGetToken(const char *start,char **end,char *token,
+                 const size_t buffer_length),
   MagickStripSpacesFromString(char *string),
   MagickStrlCat(char *dst, const char *src, const size_t size) MAGICK_FUNC_NONNULL,
   MagickStrlCpy(char *dst, const char *src, const size_t size) MAGICK_FUNC_NONNULL,
openSUSE Build Service is sponsored by