Skip to content

Commit 536512a

Browse files
committed
Added overflow checks to prevent an out of bounds write
1 parent 6947a76 commit 536512a

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

coders/xbm.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
200200
short int
201201
hex_digits[256];
202202

203+
size_t
204+
bytes_per_line,
205+
length;
206+
203207
ssize_t
204208
i,
205209
x,
@@ -212,8 +216,6 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
212216
unsigned int
213217
bit,
214218
byte,
215-
bytes_per_line,
216-
length,
217219
padding,
218220
version;
219221

@@ -351,15 +353,15 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
351353
if (((image->columns % 16) != 0) && ((image->columns % 16) < 9) &&
352354
(version == 10))
353355
padding=1;
354-
bytes_per_line=(unsigned int) (image->columns+7)/8+padding;
355-
length=(unsigned int) image->rows;
356-
data=(unsigned char *) AcquireQuantumMemory(length,bytes_per_line*
357-
sizeof(*data));
356+
bytes_per_line=(image->columns+7)/8+padding;
357+
if (HeapOverflowSanityCheckGetSize(bytes_per_line,image->rows,&length) != MagickFalse)
358+
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
359+
data=(unsigned char *) AcquireQuantumMemory(length,sizeof(*data));
358360
if (data == (unsigned char *) NULL)
359361
ThrowReaderException(ResourceLimitError,"MemoryAllocationFailed");
360362
p=data;
361363
if (version == 10)
362-
for (i=0; i < (ssize_t) (bytes_per_line*image->rows); (i+=2))
364+
for (i=0; i < (ssize_t) length; (i+=2))
363365
{
364366
c=XBMInteger(image,hex_digits);
365367
if (c < 0)
@@ -372,7 +374,7 @@ static Image *ReadXBMImage(const ImageInfo *image_info,ExceptionInfo *exception)
372374
*p++=(unsigned char) (c >> 8);
373375
}
374376
else
375-
for (i=0; i < (ssize_t) (bytes_per_line*image->rows); i++)
377+
for (i=0; i < (ssize_t) length; i++)
376378
{
377379
c=XBMInteger(image,hex_digits);
378380
if (c < 0)

0 commit comments

Comments
 (0)