Commit db2d3c013 for imagemagick.org
commit db2d3c013ff688ed91fef87b18b6cc41a9a15c8b
Author: Cristy <urban-warrior@imagemagick.org>
Date: Sun Nov 29 14:42:39 2026 -0500
a crafted run op could push the pixel pointer past the end of the buffer
diff --git a/coders/qoi.c b/coders/qoi.c
index cca58de7c..07e7c29a5 100644
--- a/coders/qoi.c
+++ b/coders/qoi.c
@@ -74,7 +74,10 @@
#define QOI_MASK_2 0xc0 /* 11000000 */
-#define QOI_COLOR_HASH(C) (C.rgba.r*3 + C.rgba.g*5 + C.rgba.b*7 + C.rgba.a*11)
+#define QOI_COLOR_HASH(C) \
+ ((C).rgba.r*3 + (C).rgba.g*5 + (C).rgba.b*7 + (C).rgba.a*11)
+
+#define QOI_PIXELS_MAX ((size_t) 400000000) /* limit recommended by the spec */
/*
Typedef declarations.
@@ -102,7 +105,7 @@ static MagickBooleanType
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
-% IsQOI()() returns MagickTrue if the image format type, identified by the
+% IsQOI() returns MagickTrue if the image format type, identified by the
% magick string, is QOI.
%
% The format of the IsQOI method is:
@@ -116,11 +119,11 @@ static MagickBooleanType
% o length: Specifies the length of the magick string.
%
*/
-static unsigned int IsQOI(const unsigned char *magick,const size_t length)
+static MagickBooleanType IsQOI(const unsigned char *magick,const size_t length)
{
if (length < 4)
return(MagickFalse);
- if (LocaleNCompare((const char *) magick, "qoif", 4) == 0)
+ if (memcmp(magick,"qoif",4) == 0)
return(MagickTrue);
return(MagickFalse);
}
@@ -166,6 +169,9 @@ static Image *ReadQOIImage(const ImageInfo *image_info,ExceptionInfo *exception)
colorspace,
channels;
+ size_t
+ total;
+
ssize_t
count,
i;
@@ -205,12 +211,14 @@ static Image *ReadQOIImage(const ImageInfo *image_info,ExceptionInfo *exception)
image->endian=MSBEndian;
image->depth=8;
count=ReadBlob(image,4,magick);
- if ((count != 4) || (LocaleNCompare((char *) magick, "qoif", 4) != 0))
+ if ((count != 4) || (memcmp(magick,"qoif",4) != 0))
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
image->columns=(size_t) ReadBlobMSBLong(image);
image->rows=(size_t) ReadBlobMSBLong(image);
if (image->columns == 0 || image->rows == 0)
ThrowReaderException(CorruptImageError,"NegativeOrZeroImageSize");
+ if (image->columns > (QOI_PIXELS_MAX/image->rows))
+ ThrowReaderException(ResourceLimitError,"WidthOrHeightExceedsLimit");
channels=(size_t) ReadBlobByte(image);
if (channels == 3)
SetQuantumImageType(image,RGBQuantum);
@@ -222,6 +230,8 @@ static Image *ReadQOIImage(const ImageInfo *image_info,ExceptionInfo *exception)
else
ThrowReaderException(CorruptImageError,"ImproperImageHeader");
colorspace=(size_t) ReadBlobByte(image);
+ if (EOFBlob(image) != MagickFalse)
+ ThrowReaderException(CorruptImageError,"UnexpectedEndOfFile");
if (colorspace == QOI_SRGB)
(void) SetImageColorspace(image,sRGBColorspace,exception);
else if (colorspace == QOI_LINEAR)
@@ -253,7 +263,8 @@ static Image *ReadQOIImage(const ImageInfo *image_info,ExceptionInfo *exception)
/*
Actual decoding.
*/
- for (i=0; i < (ssize_t) (image->rows * image->columns);)
+ total=image->columns*image->rows;
+ for (i=0; i < (ssize_t) total;)
{
run = 0;
@@ -292,9 +303,9 @@ static Image *ReadQOIImage(const ImageInfo *image_info,ExceptionInfo *exception)
}
else if ((b & QOI_MASK_2) == QOI_OP_DIFF)
{
- px.rgba.r+=((b >> 4) & 0x03) - 2;
- px.rgba.g+=((b >> 2) & 0x03) - 2;
- px.rgba.b+=( b & 0x03) - 2;
+ px.rgba.r+=(unsigned char) (((b >> 4) & 0x03) - 2);
+ px.rgba.g+=(unsigned char) (((b >> 2) & 0x03) - 2);
+ px.rgba.b+=(unsigned char) (( b & 0x03) - 2);
}
else if ((b & QOI_MASK_2) == QOI_OP_LUMA)
{
@@ -310,26 +321,28 @@ static Image *ReadQOIImage(const ImageInfo *image_info,ExceptionInfo *exception)
run=b & ~QOI_MASK_2;
}
lut[QOI_COLOR_HASH(px) % 64]=px;
+ /*
+ Never run past the end of the pixel buffer, even if the stream says so.
+ */
+ if ((size_t) run >= (total-(size_t) i))
+ run=(int) (total-(size_t) i)-1;
do
{
- if (i < (ssize_t) (image->columns*image->rows))
- {
- SetPixelRed(image,ScaleCharToQuantum((unsigned char)px.rgba.r),q);
- SetPixelGreen(image,ScaleCharToQuantum((unsigned char)px.rgba.g),q);
- SetPixelBlue(image,ScaleCharToQuantum((unsigned char)px.rgba.b),q);
- if (channels == 4)
- SetPixelAlpha(image,ScaleCharToQuantum((unsigned char) px.rgba.a),q);
- }
+ SetPixelRed(image,ScaleCharToQuantum(px.rgba.r),q);
+ SetPixelGreen(image,ScaleCharToQuantum(px.rgba.g),q);
+ SetPixelBlue(image,ScaleCharToQuantum(px.rgba.b),q);
+ if (channels == 4)
+ SetPixelAlpha(image,ScaleCharToQuantum(px.rgba.a),q);
q+=(ptrdiff_t) GetPixelChannels(image);
i++;
} while (run-- > 0);
- status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) i,
- image->rows * image->columns);
+ status=SetImageProgress(image,LoadImageTag,(MagickOffsetType) i,total);
if (status == MagickFalse)
break;
}
- status=SyncAuthenticPixels(image,exception);
- if (i < (ssize_t) (image->columns*image->rows))
+ if (SyncAuthenticPixels(image,exception) == MagickFalse)
+ status=MagickFalse;
+ if (i < (ssize_t) total)
ThrowReaderException(CorruptImageError,"NotEnoughPixelData");
if (EOFBlob(image) != MagickFalse)
ThrowFileException(exception,CorruptImageError,
@@ -373,6 +386,7 @@ ModuleExport size_t RegisterQOIImage(void)
entry->decoder=(DecodeImageHandler *) ReadQOIImage;
entry->encoder=(EncodeImageHandler *) WriteQOIImage;
entry->magick=(IsImageFormatHandler *) IsQOI;
+ entry->mime_type=ConstantString("image/qoi");
entry->flags^=CoderAdjoinFlag;
(void) RegisterMagickInfo(entry);
return(MagickImageCoderSignature);
@@ -478,7 +492,11 @@ static MagickBooleanType WriteQOIImage(const ImageInfo *image_info,Image *image,
image->endian=MSBEndian;
image->depth=8;
if (IssRGBCompatibleColorspace(image->colorspace) == MagickFalse)
- (void) TransformImageColorspace(image,sRGBColorspace,exception);
+ if (TransformImageColorspace(image,sRGBColorspace,exception) == MagickFalse)
+ {
+ (void) CloseBlob(image);
+ return(MagickFalse);
+ }
if (IsRGBColorspace(image->colorspace) != MagickFalse)
colorspace=QOI_LINEAR;
else
@@ -493,6 +511,9 @@ static MagickBooleanType WriteQOIImage(const ImageInfo *image_info,Image *image,
channels=4;
else
ThrowWriterException(ImageError,"ImageTypeNotSupported");
+ if ((image->columns == 0) || (image->rows == 0) ||
+ (image->columns > (QOI_PIXELS_MAX/image->rows)))
+ ThrowWriterException(ImageError,"WidthOrHeightExceedsLimit");
/*
Write QOI header.
*/
@@ -510,9 +531,6 @@ static MagickBooleanType WriteQOIImage(const ImageInfo *image_info,Image *image,
px.rgba.a=255;
memset(lut,0,sizeof(lut));
run=0;
- p=GetVirtualPixels(image,0,0,image->columns,image->rows,exception);
- if (p == (const Quantum *) NULL)
- ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
/* Allow space for a pending run from the preceding row. */
if (image->columns > ((size_t) MAGICK_SSIZE_MAX-1)/5)
ThrowWriterException(ResourceLimitError,"MemoryAllocationFailed");
@@ -525,6 +543,12 @@ static MagickBooleanType WriteQOIImage(const ImageInfo *image_info,Image *image,
*/
for (y=0; y < (ssize_t) image->rows; y++)
{
+ p=GetVirtualPixels(image,0,y,image->columns,1,exception);
+ if (p == (const Quantum *) NULL)
+ {
+ status=MagickFalse;
+ break;
+ }
q=pixels;
for (x=0; x < (ssize_t) image->columns; x++)
{
@@ -616,13 +640,24 @@ static MagickBooleanType WriteQOIImage(const ImageInfo *image_info,Image *image,
status=MagickFalse;
break;
}
+ status=SetImageProgress(image,SaveImageTag,(MagickOffsetType) y,
+ image->rows);
+ if (status == MagickFalse)
+ break;
}
pixels=(unsigned char *) RelinquishMagickMemory(pixels);
/*
Write the QOI end marker: seven 0x00 bytes followed by 0x01.
*/
- (void) WriteBlobPadding(image,7,'\0');
- (void) WriteBlobByte(image,0x01);
+ if (status != MagickFalse)
+ {
+ static const unsigned char
+ end_marker[8] = { 0, 0, 0, 0, 0, 0, 0, 1 };
+
+ if (WriteBlob(image,sizeof(end_marker),end_marker) !=
+ (ssize_t) sizeof(end_marker))
+ status=MagickFalse;
+ }
if (CloseBlob(image) == MagickFalse)
status=MagickFalse;
return(status);