Skip to content

Commit

Permalink
ensure: Fix potential overflow of size_t
Browse files Browse the repository at this point in the history
This could only happen if the maximum SIZE_T is not at least 2 times
bigger than INT_MAX. Not sure if this can happen on real systems, but
better be safe then sorry.
  • Loading branch information
FSMaxB committed Mar 23, 2017
1 parent 4bfb880 commit e58f7ec
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
{
Expand Down

0 comments on commit e58f7ec

Please sign in to comment.