Fix integer overflow when calculating memory allocation size

Allocating memory with a size controlled by an external user can result
in integer overflow.
Also, we need to check return values of malloc/calloc for null.
This commit is contained in:
Mingjie Shen 2023-05-20 22:44:10 -04:00
parent f31df7521a
commit 7b96925292

View File

@ -668,18 +668,25 @@ bool ExportImageAsCode(Image image, const char *fileName)
// Generate image: plain color
Image GenImageColor(int width, int height, Color color)
{
Color *pixels = (Color *)RL_CALLOC(width*height, sizeof(Color));
for (int i = 0; i < width*height; i++) pixels[i] = color;
Image image = {
.data = pixels,
.data = NULL,
.width = width,
.height = height,
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
.mipmaps = 1
};
if (height > INT_MAX / sizeof(Color)) {
return image;
}
Color *pixels = (Color *)RL_CALLOC(width, height*sizeof(Color));
if (pixels == NULL) {
return image;
}
image.data = pixels;
for (int i = 0; i < width*height; i++) pixels[i] = color;
return image;
}