move checks before mallocs to prevent memory leaks

This commit is contained in:
homm 2016-05-01 17:31:54 +03:00
parent 73c81404e6
commit a5b99dd302

View File

@ -139,11 +139,11 @@ ImagingResampleHorizontal(Imaging imIn, int xsize, int filter)
kmax = (int) ceil(support) * 2 + 1;
// check for overflow
if (kmax > 0 && xsize > SIZE_MAX / kmax)
if (xsize > SIZE_MAX / (kmax * sizeof(float)))
return (Imaging) ImagingError_MemoryError();
// sizeof(float) should be greater than 0
if (xsize * kmax > SIZE_MAX / sizeof(float))
// sizeof(int) should be greater than 0 as well
if (xsize > SIZE_MAX / (2 * sizeof(int)))
return (Imaging) ImagingError_MemoryError();
/* coefficient buffer */
@ -151,10 +151,6 @@ ImagingResampleHorizontal(Imaging imIn, int xsize, int filter)
if ( ! kk)
return (Imaging) ImagingError_MemoryError();
// sizeof(int) should be greater than 0 as well
if (xsize > SIZE_MAX / (2 * sizeof(int)))
return (Imaging) ImagingError_MemoryError();
xbounds = malloc(xsize * 2 * sizeof(int));
if ( ! xbounds) {
free(kk);