Commit d286490f0 for imagemagick.org
commit d286490f0db881344a36df847dcd71f1f56c5aef
Author: Cristy <urban-warrior@imagemagick.org>
Date: Sun Nov 29 14:47:45 2026 -0500
check rejects headers that claim far more data than the file contains
diff --git a/coders/xbm.c b/coders/xbm.c
index 995e74dab..9a7dff01e 100644
--- a/coders/xbm.c
+++ b/coders/xbm.c
@@ -150,18 +150,19 @@ static int XBMInteger(Image *image,short int *hex_digits)
return(-1);
} while ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\r'));
/*
- Evaluate number.
+ A value must start with a digit; anything else (a stray brace, comma, or
+ garbage) means the bitmap data is malformed.
+ */
+ if (hex_digits[c & 0xff] < 0)
+ return(-1);
+ /*
+ Evaluate number, ignoring digits that would overflow an int.
*/
value=0;
do
{
if (value <= (unsigned int) (INT_MAX/16))
- {
- value*=16;
- c&=0xff;
- if (value <= (unsigned int) ((INT_MAX-1)-hex_digits[c]))
- value+=(unsigned int) hex_digits[c];
- }
+ value=(value*16)+(unsigned int) hex_digits[c & 0xff];
c=ReadBlobByte(image);
if (c == EOF)
return(-1);
@@ -186,11 +187,15 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
width;
MagickBooleanType
+ found,
status;
MagickOffsetType
offset;
+ MagickSizeType
+ blob_size;
+
Quantum
*q;
@@ -260,28 +265,37 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
Scan until hex digits.
*/
version=11;
- offset=TellBlob(image);
- while (ReadBlobString(image,buffer) != (char *) NULL)
+ found=MagickFalse;
+ for ( ; ; )
{
+ offset=TellBlob(image);
+ if (ReadBlobString(image,buffer) == (char *) NULL)
+ break;
if (MagickSscanf(buffer,"static short %1024s = {",name) == 1)
version=10;
else if (MagickSscanf(buffer,"static unsigned char %1024s = {",name) == 1)
version=11;
else if (MagickSscanf(buffer,"static char %1024s = {",name) == 1)
version=11;
+ else if (MagickSscanf(buffer,"static const unsigned char %1024s = {",name) == 1)
+ version=11;
+ else if (MagickSscanf(buffer,"static const char %1024s = {",name) == 1)
+ version=11;
else
- {
- offset=TellBlob(image);
- continue;
- }
+ continue;
p=(unsigned char *) strrchr(name,'_');
if (p == (unsigned char *) NULL)
p=(unsigned char *) name;
else
p++;
if (LocaleCompare("bits[]",(char *) p) == 0)
- break;
+ {
+ found=MagickTrue;
+ break;
+ }
}
+ if (found == MagickFalse)
+ ThrowReaderException(CorruptImageError,"ImproperImageHeader");
/*
Initialize image structure.
*/
@@ -335,6 +349,7 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
hex_digits[(int) 'e']=14;
hex_digits[(int) 'f']=15;
hex_digits[(int) 'x']=0;
+ hex_digits[(int) 'X']=0;
hex_digits[(int) ' ']=(-1);
hex_digits[(int) ',']=(-1);
hex_digits[(int) '}']=(-1);
@@ -350,9 +365,13 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
bytes_per_line=(image->columns+7)/8+padding;
if (HeapOverflowSanityCheckGetSize(bytes_per_line,image->rows,&length) != MagickFalse)
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
+ blob_size=GetBlobSize(image);
+ if ((blob_size != 0) && ((MagickSizeType) length > blob_size))
+ ThrowReaderException(CorruptImageError,"InsufficientImageDataInFile");
data=(unsigned char *) AcquireQuantumMemory(length,sizeof(*data));
if (data == (unsigned char *) NULL)
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
+ (void) memset(data,0,length*sizeof(*data));
p=data;
if (version == 10)
for (i=0; i < (ssize_t) length; i+=2)
@@ -364,7 +383,7 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
}
*p++=(unsigned char) c;
- if ((padding == 0) || (((i+2) % bytes_per_line) != 0))
+ if ((padding == 0) || ((((size_t) i+2) % bytes_per_line) != 0))
*p++=(unsigned char) (c >> 8);
}
else
@@ -391,7 +410,10 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
{
q=QueueAuthenticPixels(image,0,y,image->columns,1,exception);
if (q == (Quantum *) NULL)
- break;
+ {
+ status=MagickFalse;
+ break;
+ }
bit=0;
byte=0;
for (x=0; x < (ssize_t) image->columns; x++)
@@ -406,7 +428,10 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
q+=(ptrdiff_t) GetPixelChannels(image);
}
if (SyncAuthenticPixels(image,exception) == MagickFalse)
- break;
+ {
+ status=MagickFalse;
+ break;
+ }
status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) y,
image->rows);
if (status == MagickFalse)
@@ -511,6 +536,7 @@ ModuleExport void UnregisterXBMImage(void)
% o exception: return any errors or warnings in this structure.
%
*/
+
static inline MagickBooleanType WriteXBMBuffer(Image *image,const char *buffer)
{
size_t
@@ -521,6 +547,28 @@ static inline MagickBooleanType WriteXBMBuffer(Image *image,const char *buffer)
(ssize_t) length ? MagickTrue : MagickFalse);
}
+static MagickBooleanType WriteXBMByte(Image *image,const unsigned int byte,
+ size_t *count)
+{
+ char
+ buffer[16];
+
+ /*
+ Write a bitmap byte to the image file, twelve bytes to a line.
+ */
+ (void) FormatLocaleString(buffer,sizeof(buffer),"0x%02X, ",byte & 0xff);
+ if (WriteXBMBuffer(image,buffer) == MagickFalse)
+ return(MagickFalse);
+ (*count)++;
+ if (*count == 12)
+ {
+ if (WriteXBMBuffer(image,"\n ") == MagickFalse)
+ return(MagickFalse);
+ *count=0;
+ }
+ return(MagickTrue);
+}
+
static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
ExceptionInfo *exception)
{
@@ -535,17 +583,17 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
const Quantum
*p;
- ssize_t
- x;
-
size_t
- bit,
- byte;
+ count;
ssize_t
- count,
+ x,
y;
+ unsigned int
+ bit,
+ byte;
+
/*
Open output image file.
*/
@@ -567,18 +615,27 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
*/
GetPathComponent(image->filename,BasePath,basename);
sanitized_filename=SanitizeStringForIdentifier(basename);
+ if (sanitized_filename == (char *) NULL)
+ ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
(void) FormatLocaleString(buffer,MagickPathExtent,"#define %s_width %.17g\n",
sanitized_filename,(double) image->columns);
- (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
+ status=WriteXBMBuffer(image,buffer);
(void) FormatLocaleString(buffer,MagickPathExtent,"#define %s_height %.17g\n",
sanitized_filename,(double) image->rows);
- (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
+ if (WriteXBMBuffer(image,buffer) == MagickFalse)
+ status=MagickFalse;
(void) FormatLocaleString(buffer,MagickPathExtent,
"static char %s_bits[] = {\n",sanitized_filename);
sanitized_filename=DestroyString(sanitized_filename);
- (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
- (void) CopyMagickString(buffer," ",MagickPathExtent);
- (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
+ if (WriteXBMBuffer(image,buffer) == MagickFalse)
+ status=MagickFalse;
+ if (WriteXBMBuffer(image," ") == MagickFalse)
+ status=MagickFalse;
+ if (status == MagickFalse)
+ {
+ (void) CloseBlob(image);
+ return(MagickFalse);
+ }
/*
Convert MIFF to X bitmap pixels.
*/
@@ -586,15 +643,14 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
bit=0;
byte=0;
count=0;
- x=0;
- y=0;
- (void) CopyMagickString(buffer," ",MagickPathExtent);
- (void) WriteBlob(image,strlen(buffer),(unsigned char *) buffer);
for (y=0; y < (ssize_t) image->rows; y++)
{
p=GetVirtualPixels(image,0,y,image->columns,1,exception);
if (p == (const Quantum *) NULL)
- break;
+ {
+ status=MagickFalse;
+ break;
+ }
for (x=0; x < (ssize_t) image->columns; x++)
{
byte>>=1;
@@ -603,27 +659,11 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
bit++;
if (bit == 8)
{
- /*
- Write a bitmap byte to the image file.
- */
- (void) FormatLocaleString(buffer,MagickPathExtent,"0x%02X, ",
- (unsigned int) (byte & 0xff));
- if (WriteXBMBuffer(image,buffer) == MagickFalse)
+ if (WriteXBMByte(image,byte,&count) == MagickFalse)
{
status=MagickFalse;
break;
}
- count++;
- if (count == 12)
- {
- (void) CopyMagickString(buffer,"\n ",MagickPathExtent);
- if (WriteXBMBuffer(image,buffer) == MagickFalse)
- {
- status=MagickFalse;
- break;
- }
- count=0;
- };
bit=0;
byte=0;
}
@@ -634,37 +674,23 @@ static MagickBooleanType WriteXBMImage(const ImageInfo *image_info,Image *image,
if (bit != 0)
{
/*
- Write a bitmap byte to the image file.
+ Flush the partial byte at the end of the scanline.
*/
byte>>=(8-bit);
- (void) FormatLocaleString(buffer,MagickPathExtent,"0x%02X, ",
- (unsigned int) (byte & 0xff));
- if (WriteXBMBuffer(image,buffer) == MagickFalse)
+ if (WriteXBMByte(image,byte,&count) == MagickFalse)
{
status=MagickFalse;
break;
}
- count++;
- if (count == 12)
- {
- (void) CopyMagickString(buffer,"\n ",MagickPathExtent);
- if (WriteXBMBuffer(image,buffer) == MagickFalse)
- {
- status=MagickFalse;
- break;
- }
- count=0;
- };
bit=0;
byte=0;
- };
+ }
status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
image->rows);
if (status == MagickFalse)
break;
}
- (void) CopyMagickString(buffer,"};\n",MagickPathExtent);
- if ((status != MagickFalse) && (WriteXBMBuffer(image,buffer) == MagickFalse))
+ if ((status != MagickFalse) && (WriteXBMBuffer(image,"};\n") == MagickFalse))
status=MagickFalse;
if (CloseBlob(image) == MagickFalse)
status=MagickFalse;