Replace SIZE_MAX with type specific _MAX

This commit is contained in:
wiredfool 2016-05-21 07:31:19 -07:00
parent 54a979749c
commit 5369d8edd7
9 changed files with 31 additions and 18 deletions

View File

@ -189,7 +189,7 @@ _setimage(ImagingDecoderObject* decoder, PyObject* args)
/* Allocate memory buffer (if bits field is set) */ /* Allocate memory buffer (if bits field is set) */
if (state->bits > 0) { if (state->bits > 0) {
if (!state->bytes) { if (!state->bytes) {
if (state->xsize > ((SIZE_MAX / state->bits)-7)){ if (state->xsize > ((INT_MAX / state->bits)-7)){
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
state->bytes = (state->bits * state->xsize+7)/8; state->bytes = (state->bits * state->xsize+7)/8;

View File

@ -234,7 +234,7 @@ _setimage(ImagingEncoderObject* encoder, PyObject* args)
/* Allocate memory buffer (if bits field is set) */ /* Allocate memory buffer (if bits field is set) */
if (state->bits > 0) { if (state->bits > 0) {
if (state->xsize > ((SIZE_MAX / state->bits)-7)) { if (state->xsize > ((INT_MAX / state->bits)-7)) {
return PyErr_NoMemory(); return PyErr_NoMemory();
} }
state->bytes = (state->bits * state->xsize+7)/8; state->bytes = (state->bits * state->xsize+7)/8;

View File

@ -937,7 +937,7 @@ allocate(ImagingOutline outline, int extra)
/* malloc check ok, uses calloc for overflow */ /* malloc check ok, uses calloc for overflow */
e = calloc(outline->size, sizeof(Edge)); e = calloc(outline->size, sizeof(Edge));
} else { } else {
if (outline->size > SIZE_MAX / sizeof(Edge)) { if (outline->size > INT_MAX / sizeof(Edge)) {
return NULL; return NULL;
} }
/* malloc check ok, overflow checked above */ /* malloc check ok, overflow checked above */

View File

@ -1092,6 +1092,10 @@ k_means(Pixel *pixelData,
uint32_t **avgDistSortKey; uint32_t **avgDistSortKey;
int changes; int changes;
int built=0; int built=0;
if (nPaletteEntries > UINT32_MAX / (sizeof(uint32_t))) {
return 0;
}
/* malloc check ok, using calloc */ /* malloc check ok, using calloc */
if (!(count=calloc(nPaletteEntries, sizeof(uint32_t)))) { if (!(count=calloc(nPaletteEntries, sizeof(uint32_t)))) {
return 0; return 0;
@ -1107,7 +1111,7 @@ k_means(Pixel *pixelData,
} }
/* this is enough of a check, since the multiplication n*size is done above */ /* this is enough of a check, since the multiplication n*size is done above */
if (nPaletteEntries > SIZE_MAX / (nPaletteEntries * sizeof(uint32_t))) { if (nPaletteEntries > UINT32_MAX / (nPaletteEntries * sizeof(uint32_t))) {
goto error_1; goto error_1;
} }
/* malloc check ok, using calloc, checking n*n above */ /* malloc check ok, using calloc, checking n*n above */
@ -1266,8 +1270,8 @@ quantize(Pixel *pixelData,
qp=calloc(nPixels, sizeof(uint32_t)); qp=calloc(nPixels, sizeof(uint32_t));
if (!qp) { goto error_4; } if (!qp) { goto error_4; }
if ((nPaletteEntries > SIZE_MAX / nPaletteEntries ) || if ((nPaletteEntries > UINT32_MAX / nPaletteEntries ) ||
(nPaletteEntries > SIZE_MAX / (nPaletteEntries * sizeof(uint32_t)))) { (nPaletteEntries > UINT32_MAX / (nPaletteEntries * sizeof(uint32_t)))) {
goto error_5; goto error_5;
} }
/* malloc check ok, using calloc for overflow, check of n*n above */ /* malloc check ok, using calloc for overflow, check of n*n above */
@ -1445,8 +1449,8 @@ quantize2(Pixel *pixelData,
qp=calloc(nPixels, sizeof(uint32_t)); qp=calloc(nPixels, sizeof(uint32_t));
if (!qp) { goto error_1; } if (!qp) { goto error_1; }
if ((nQuantPixels > SIZE_MAX / nQuantPixels ) || if ((nQuantPixels > UINT32_MAX / nQuantPixels ) ||
(nQuantPixels > SIZE_MAX / (nQuantPixels * sizeof(uint32_t)))) { (nQuantPixels > UINT32_MAX / (nQuantPixels * sizeof(uint32_t)))) {
goto error_2; goto error_2;
} }
@ -1515,8 +1519,8 @@ ImagingQuantize(Imaging im, int colors, int mode, int kmeans)
if (!strcmp(im->mode, "RGBA") && mode != 2 && mode != 3) if (!strcmp(im->mode, "RGBA") && mode != 2 && mode != 3)
return ImagingError_ModeError(); return ImagingError_ModeError();
if ((im->xsize > SIZE_MAX / im->ysize) || if ((im->xsize > INT_MAX / im->ysize) ||
(im->xsize > SIZE_MAX / (im->ysize * sizeof(Pixel)))) { (im->xsize > INT_MAX / (im->ysize * sizeof(Pixel)))) {
return ImagingError_MemoryError(); return ImagingError_MemoryError();
} }
/* malloc check ok, using calloc for final overflow, x*y above */ /* malloc check ok, using calloc for final overflow, x*y above */

View File

@ -20,6 +20,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <limits.h>
#include "QuantHeap.h" #include "QuantHeap.h"
@ -47,7 +48,7 @@ static int _heap_grow(Heap *h,int newsize) {
void *newheap; void *newheap;
if (!newsize) newsize=h->heapsize<<1; if (!newsize) newsize=h->heapsize<<1;
if (newsize<h->heapsize) return 0; if (newsize<h->heapsize) return 0;
if (newsize > ((int)SIZE_MAX) / sizeof(void *)){ if (newsize > INT_MAX / sizeof(void *)){
return 0; return 0;
} }
/* malloc check ok, using calloc for overflow, also checking /* malloc check ok, using calloc for overflow, also checking

View File

@ -26,6 +26,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h>
#include "QuantOctree.h" #include "QuantOctree.h"
@ -62,6 +63,12 @@ new_color_cube(int r, int g, int b, int a) {
cube->bBits = MAX(b, 0); cube->bBits = MAX(b, 0);
cube->aBits = MAX(a, 0); cube->aBits = MAX(a, 0);
/* overflow check for size multiplication below */
if (cube->rBits + cube->gBits + cube->bBits + cube->aBits > 31) {
free(cube);
return NULL;
}
/* the width of the cube for each dimension */ /* the width of the cube for each dimension */
cube->rWidth = 1<<cube->rBits; cube->rWidth = 1<<cube->rBits;
cube->gWidth = 1<<cube->gBits; cube->gWidth = 1<<cube->gBits;
@ -77,6 +84,7 @@ new_color_cube(int r, int g, int b, int a) {
/* the number of color buckets */ /* the number of color buckets */
cube->size = cube->rWidth * cube->gWidth * cube->bWidth * cube->aWidth; cube->size = cube->rWidth * cube->gWidth * cube->bWidth * cube->aWidth;
/* malloc check ok, overflow checked above */
cube->buckets = calloc(cube->size, sizeof(struct _ColorBucket)); cube->buckets = calloc(cube->size, sizeof(struct _ColorBucket));
if (!cube->buckets) { if (!cube->buckets) {
@ -155,7 +163,7 @@ compare_bucket_count(const ColorBucket a, const ColorBucket b) {
static ColorBucket static ColorBucket
create_sorted_color_palette(const ColorCube cube) { create_sorted_color_palette(const ColorCube cube) {
ColorBucket buckets; ColorBucket buckets;
if (cube->size > SIZE_MAX / sizeof(struct _ColorBucket)) { if (cube->size > LONG_MAX / sizeof(struct _ColorBucket)) {
return NULL; return NULL;
} }
/* malloc check ok, calloc + overflow check above for memcpy */ /* malloc check ok, calloc + overflow check above for memcpy */
@ -285,8 +293,8 @@ void add_lookup_buckets(ColorCube cube, ColorBucket palette, long nColors, long
ColorBucket ColorBucket
combined_palette(ColorBucket bucketsA, long nBucketsA, ColorBucket bucketsB, long nBucketsB) { combined_palette(ColorBucket bucketsA, long nBucketsA, ColorBucket bucketsB, long nBucketsB) {
ColorBucket result; ColorBucket result;
if (nBucketsA > SIZE_MAX - nBucketsB || if (nBucketsA > LONG_MAX - nBucketsB ||
(nBucketsA+nBucketsB) > SIZE_MAX / sizeof(struct _ColorBucket)) { (nBucketsA+nBucketsB) > LONG_MAX / sizeof(struct _ColorBucket)) {
return NULL; return NULL;
} }
/* malloc check ok, overflow check above */ /* malloc check ok, overflow check above */

View File

@ -61,8 +61,8 @@ ImagingRankFilter(Imaging im, int size, int rank)
return (Imaging) ImagingError_ValueError("bad filter size"); return (Imaging) ImagingError_ValueError("bad filter size");
/* malloc check ok, for overflow in the define below */ /* malloc check ok, for overflow in the define below */
if (size > SIZE_MAX / size || if (size > INT_MAX / size ||
size > SIZE_MAX / (size * sizeof(FLOAT32))) { size > INT_MAX / (size * sizeof(FLOAT32))) {
return (Imaging) ImagingError_ValueError("filter size too large"); return (Imaging) ImagingError_ValueError("filter size too large");
} }

View File

@ -57,7 +57,7 @@ ImagingZipDecode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
context->prefix = 1; /* PNG */ context->prefix = 1; /* PNG */
/* overflow check for malloc */ /* overflow check for malloc */
if (state->bytes > SIZE_MAX - 1) { if (state->bytes > INT_MAX - 1) {
state->errcode = IMAGING_CODEC_MEMORY; state->errcode = IMAGING_CODEC_MEMORY;
return -1; return -1;
} }

View File

@ -38,7 +38,7 @@ ImagingZipEncode(Imaging im, ImagingCodecState state, UINT8* buf, int bytes)
/* Valid modes are ZIP_PNG, ZIP_PNG_PALETTE, and ZIP_TIFF */ /* Valid modes are ZIP_PNG, ZIP_PNG_PALETTE, and ZIP_TIFF */
/* overflow check for malloc */ /* overflow check for malloc */
if (state->bytes > SIZE_MAX - 1) { if (state->bytes > INT_MAX - 1) {
state->errcode = IMAGING_CODEC_MEMORY; state->errcode = IMAGING_CODEC_MEMORY;
return -1; return -1;
} }