FIXED: ImageFromImage() heap-buffer-overflow on compressed formats

This commit is contained in:
gummyniki 2026-07-17 21:39:26 +00:00
commit 233a0661a0

View file

@ -1230,10 +1230,17 @@ Image ImageFromImage(Image image, Rectangle rec)
{ {
Image result = { 0 }; Image result = { 0 };
// Security check to avoid program crash
if ((image.data == NULL) || (image.width == 0) || (image.height == 0)) return result;
// Basic rectangle validation: size smaller than image size // Basic rectangle validation: size smaller than image size
if ((rec.x >= 0) && (rec.y >= 0) && (rec.width > 0) && (rec.height > 0) && if ((rec.x >= 0) && (rec.y >= 0) && (rec.width > 0) && (rec.height > 0) &&
(((int)rec.x + (int)rec.width) <= image.width) && (((int)rec.x + (int)rec.width) <= image.width) &&
(((int)rec.y + (int)rec.height) <= image.height)) (((int)rec.y + (int)rec.height) <= image.height))
{
if (image.format >= PIXELFORMAT_COMPRESSED_DXT1_RGB) TRACELOG(LOG_WARNING, "IMAGE: Image manipulation not supported for compressed formats");
else
{ {
int bytesPerPixel = GetPixelDataSize(1, 1, image.format); int bytesPerPixel = GetPixelDataSize(1, 1, image.format);
@ -1248,6 +1255,7 @@ Image ImageFromImage(Image image, Rectangle rec)
memcpy(((unsigned char *)result.data) + y*(int)rec.width*bytesPerPixel, ((unsigned char *)image.data) + ((y + (int)rec.y)*image.width + (int)rec.x)*bytesPerPixel, (int)rec.width*bytesPerPixel); memcpy(((unsigned char *)result.data) + y*(int)rec.width*bytesPerPixel, ((unsigned char *)image.data) + ((y + (int)rec.y)*image.width + (int)rec.x)*bytesPerPixel, (int)rec.width*bytesPerPixel);
} }
} }
}
else TRACELOG(LOG_WARNING, "IMAGE: Rectangle provided for ImageToImage not valid"); else TRACELOG(LOG_WARNING, "IMAGE: Rectangle provided for ImageToImage not valid");
return result; return result;