Review ImageCrop() security checks

This commit is contained in:
Ray 2019-09-20 15:55:55 +02:00
parent b331edd7a6
commit 1f730b3b35

View file

@ -1336,20 +1336,13 @@ void ImageCrop(Image *image, Rectangle crop)
{ {
// Security check to avoid program crash // Security check to avoid program crash
if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return; if ((image->data == NULL) || (image->width == 0) || (image->height == 0)) return;
// Security checks to make sure cropping rectangle is inside margins // Security checks to validate crop rectangle
if ((crop.x + crop.width) > image->width) if (crop.x < 0) { crop.width += crop.x; crop.x = 0; }
{ if (crop.y < 0) { crop.height += crop.y; crop.y = 0; }
crop.width = image->width - crop.x; if ((crop.x + crop.width) > image->width) crop.width = image->width - crop.x;
TraceLog(LOG_WARNING, "Crop rectangle width out of bounds, rescaled crop width: %i", crop.width); if ((crop.y + crop.height) > image->height) crop.height = image->height - crop.y;
}
if ((crop.y + crop.height) > image->height)
{
crop.height = image->height - crop.y;
TraceLog(LOG_WARNING, "Crop rectangle height out of bounds, rescaled crop height: %i", crop.height);
}
if ((crop.x < image->width) && (crop.y < image->height)) if ((crop.x < image->width) && (crop.y < image->height))
{ {
// Start the cropping process // Start the cropping process
@ -1377,10 +1370,7 @@ void ImageCrop(Image *image, Rectangle crop)
// Reformat 32bit RGBA image to original format // Reformat 32bit RGBA image to original format
ImageFormat(image, format); ImageFormat(image, format);
} }
else else TraceLog(LOG_WARNING, "Image can not be cropped, crop rectangle out of bounds");
{
TraceLog(LOG_WARNING, "Image can not be cropped, crop rectangle out of bounds");
}
} }
// Crop image depending on alpha value // Crop image depending on alpha value
@ -2985,7 +2975,7 @@ static Image LoadAnimatedGIF(const char *fileName, int *frames, int **delays)
int size = ftell(gifFile); int size = ftell(gifFile);
fseek(gifFile, 0L, SEEK_SET); fseek(gifFile, 0L, SEEK_SET);
char *buffer = (char *)RL_CALLOC(size, sizeof(char)); unsigned char *buffer = (unsigned char *)RL_CALLOC(size, sizeof(char));
fread(buffer, sizeof(char), size, gifFile); fread(buffer, sizeof(char), size, gifFile);
fclose(gifFile); // Close file pointer fclose(gifFile); // Close file pointer