handle SIZE_T_MAX

This commit is contained in:
Josh Coalson
2007-09-11 04:46:34 +00:00
parent 582e675d67
commit 0221d87c89

View File

@@ -29,6 +29,13 @@
#endif
#include <stdlib.h> /* for size_t, malloc(), etc */
#ifndef SIZE_MAX
#ifndef SIZE_T_MAX
#error
#endif
#define SIZE_MAX SIZE_T_MAX
#endif
/* avoid malloc()ing 0 bytes, see:
* https://www.securecoding.cert.org/confluence/display/seccode/MEM04-A.+Do+not+make+assumptions+about+the+result+of+allocating+0+bytes?focusedCommentId=5407003
*/
@@ -126,6 +133,17 @@ static void *safe_malloc_mul2add_(size_t size1, size_t size2, size_t size3)
return safe_malloc_add_2op_(size1*size2, size3);
}
/* size1 * (size2 + size3) */
static void *safe_malloc_muladd2_(size_t size1, size_t size2, size_t size3)
{
if(!size1 || (!size2 && !size3))
return malloc(1); /* malloc(0) is undefined; FLAC src convention is to always allocate */
size2 += size3;
if(size2 < size3)
return 0;
return safe_malloc_mul_2op_(size1, size2);
}
static void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
{
size2 += size1;
@@ -168,4 +186,15 @@ static void *safe_realloc_mul_2op_(void *ptr, size_t size1, size_t size2)
return realloc(ptr, size1*size2);
}
/* size1 * (size2 + size3) */
static void *safe_realloc_muladd2_(void *ptr, size_t size1, size_t size2, size_t size3)
{
if(!size1 || (!size2 && !size3))
return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
size2 += size3;
if(size2 < size3)
return 0;
return safe_realloc_mul_2op_(ptr, size1, size2);
}
#endif