cJSON

mirror of Dave's cJSON
git clone git://git.thc420.xyz/cJSON
Log | Files | Refs | README | LICENSE

commit 9f6fa94c91a87b71e4c6868dbf2ce431a48517b0
parent 123bb1af7bfae41d805337fef4b41045ef6c7d25
Author: Max Bruckner <max@maxbruckner.de>
Date:   Thu, 16 Feb 2017 00:11:34 +0100

ensure: replace pow2gt with multiplication by two

This replaces the bit fiddling involved with calculating a new buffer
size by just multiplying the required length by two, paving the way to a
complete switch to size_t at a later point in time.

Diffstat:
McJSON.c | 51+++++++++++++++------------------------------------
1 file changed, 15 insertions(+), 36 deletions(-)

diff --git a/cJSON.c b/cJSON.c @@ -32,20 +32,6 @@ #include <ctype.h> #include "cJSON.h" -/* Determine the number of bits that an integer has using the preprocessor */ -#if INT_MAX == 32767 - /* 16 bits */ - #define INTEGER_SIZE 0x0010 -#elif INT_MAX == 2147483647 - /* 32 bits */ - #define INTEGER_SIZE 0x0100 -#elif INT_MAX == 9223372036854775807 - /* 64 bits */ - #define INTEGER_SIZE 0x1000 -#else - #error "Failed to determine the size of an integer" -#endif - /* define our own boolean type */ typedef int cjbool; #define true ((cjbool)1) @@ -223,27 +209,6 @@ double cJSON_SetNumberHelper(cJSON *object, double number) return object->valuedouble = number; } -/* calculate the next largest power of 2 */ -static int pow2gt (int x) -{ - --x; - - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; -#if INTEGER_SIZE & 0x1110 /* at least 16 bit */ - x |= x >> 8; -#endif -#if INTEGER_SIZE & 0x1100 /* at least 32 bit */ - x |= x >> 16; -#endif -#if INTEGER_SIZE & 0x1000 /* 64 bit */ - x |= x >> 32; -#endif - - return x + 1; -} - typedef struct { unsigned char *buffer; @@ -278,7 +243,21 @@ static unsigned char* ensure(printbuffer *p, size_t needed) return NULL; } - newsize = (size_t) pow2gt((int)needed); + /* calculate new buffer size */ + newsize = needed * 2; + if (newsize > INT_MAX) + { + /* overflow of int, use INT_MAX if possible */ + if (needed <= INT_MAX) + { + newsize = INT_MAX; + } + else + { + return NULL; + } + } + newbuffer = (unsigned char*)cJSON_malloc(newsize); if (!newbuffer) {