diff --git a/cJSON.c b/cJSON.c index 727e1827c..822c10900 100644 --- a/cJSON.c +++ b/cJSON.c @@ -42,6 +42,7 @@ #include #include #include +#include #ifdef ENABLE_LOCALES #include @@ -394,6 +395,11 @@ static unsigned char* ensure(printbuffer * const p, size_t needed) return NULL; } + /* Ensure adding (p->offset + 1) won't overflow SIZE_MAX */ + if (p->offset > SIZE_MAX - 1 - needed) { + return NULL; + } + needed += p->offset + 1; if (needed <= p->length) {