@@ -1674,6 +1674,8 @@ MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1674
1674
/*
1675
1675
Start with a copy of the format string.
1676
1676
*/
1677
+ assert (format != (const char * ) NULL );
1678
+ assert (filename != (char * ) NULL );
1677
1679
(void ) CopyMagickString (filename ,format ,MagickPathExtent );
1678
1680
if (IsStringTrue (GetImageOption (image_info ,"filename:literal" )) != MagickFalse )
1679
1681
return (strlen (filename ));
@@ -1697,7 +1699,7 @@ MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1697
1699
/*
1698
1700
Skip padding digits like %03d.
1699
1701
*/
1700
- if (* cursor == '0' )
1702
+ if (isdigit (( int ) (( unsigned char ) * cursor )) != 0 )
1701
1703
(void ) strtol (cursor ,(char * * ) & cursor ,10 );
1702
1704
switch (* cursor )
1703
1705
{
@@ -1709,9 +1711,8 @@ MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1709
1711
count ;
1710
1712
1711
1713
count = FormatLocaleString (pattern ,sizeof (pattern ),q ,value );
1712
- if ((count <= 0 ) || (count >= MagickPathExtent ))
1713
- return (0 );
1714
- if ((offset + count ) >= MagickPathExtent )
1714
+ if ((count <= 0 ) || (count >= MagickPathExtent ) ||
1715
+ ((offset + count ) >= MagickPathExtent ))
1715
1716
return (0 );
1716
1717
(void ) CopyMagickString (p + offset ,pattern ,(size_t ) (MagickPathExtent -
1717
1718
offset ));
@@ -1725,7 +1726,9 @@ MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1725
1726
* option = (const char * ) NULL ;
1726
1727
1727
1728
size_t
1728
- extent = (size_t ) (end - cursor );
1729
+ extent = (size_t ) (end - cursor - 1 ),
1730
+ option_length ,
1731
+ tail_length ;
1729
1732
1730
1733
/*
1731
1734
Handle %[key:value];
@@ -1734,21 +1737,27 @@ MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1734
1737
break ;
1735
1738
if (extent >= sizeof (pattern ))
1736
1739
break ;
1737
- (void ) CopyMagickString (pattern ,cursor ,extent );
1740
+ (void ) CopyMagickString (pattern ,cursor + 1 ,extent + 1 );
1738
1741
pattern [extent ]= '\0' ;
1739
1742
if (image != (Image * ) NULL )
1740
- option = GetImageProperty (image ,pattern );
1741
- if ((option == (const char * ) NULL ) && (image != (Image * )NULL ))
1742
- option = GetImageArtifact (image ,pattern );
1743
+ {
1744
+ option = GetImageProperty (image ,pattern );
1745
+ if (option == (const char * ) NULL )
1746
+ option = GetImageArtifact (image ,pattern );
1747
+ }
1743
1748
if ((option == (const char * ) NULL ) &&
1744
1749
(image_info != (ImageInfo * ) NULL ))
1745
1750
option = GetImageOption (image_info ,pattern );
1746
1751
if (option == (const char * ) NULL )
1747
1752
break ;
1753
+ option_length = strlen (option );
1754
+ tail_length = strlen (end + 1 );
1755
+ if ((offset + option_length + tail_length + 1 ) > MagickPathExtent )
1756
+ return (0 );
1748
1757
(void ) CopyMagickString (p + offset ,option ,(size_t ) (MagickPathExtent -
1749
1758
offset ));
1750
- (void ) ConcatenateMagickString (p + offset + strlen ( option ) ,end + 1 ,(size_t )
1751
- ( MagickPathExtent - offset - strlen ( option ) - strlen ( end ) - 1 ));
1759
+ (void ) ConcatenateMagickString (p + offset + option_length ,end + 1 ,(size_t ) (
1760
+ MagickPathExtent - offset - option_length - tail_length - 1 ));
1752
1761
cursor = end + 1 ;
1753
1762
break ;
1754
1763
}
@@ -1762,7 +1771,7 @@ MagickExport size_t InterpretImageFilename(const ImageInfo *image_info,
1762
1771
Replace "%%" with "%".
1763
1772
*/
1764
1773
if ((* p == '%' ) && (* (p + 1 ) == '%' ))
1765
- (void ) memmove (p ,p + 1 ,strlen (p ) ); /* shift left */
1774
+ (void ) memmove (p ,p + 1 ,strlen (p + 1 ) + 1 ); /* shift left */
1766
1775
else
1767
1776
p ++ ;
1768
1777
}
0 commit comments