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:
parent
f31df7521a
commit
7b96925292
|
|
@ -668,18 +668,25 @@ bool ExportImageAsCode(Image image, const char *fileName)
|
||||||
// Generate image: plain color
|
// Generate image: plain color
|
||||||
Image GenImageColor(int width, int height, Color 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 = {
|
Image image = {
|
||||||
.data = pixels,
|
.data = NULL,
|
||||||
.width = width,
|
.width = width,
|
||||||
.height = height,
|
.height = height,
|
||||||
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
|
.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
|
||||||
.mipmaps = 1
|
.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;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user