diff --git a/cJSON.c b/cJSON.c index c090df336ce88506a7ce39fb2fb50c0ed83dd291..283faa0557e3572e65f2aaf26c92c16e77a86c2a 100644 --- a/cJSON.c +++ b/cJSON.c @@ -270,8 +270,7 @@ static unsigned char* ensure(printbuffer * const p, size_t needed, const interna } /* calculate new buffer size */ - newsize = needed * 2; - if (newsize > INT_MAX) + if (newsize > (INT_MAX / 2)) { /* overflow of int, use INT_MAX if possible */ if (needed <= INT_MAX) @@ -283,6 +282,10 @@ static unsigned char* ensure(printbuffer * const p, size_t needed, const interna return NULL; } } + else + { + newsize = needed * 2; + } if (hooks->reallocate != NULL) {