Skip to content

Commit 92395c0

Browse files
committed
limit numeric token length during parsing
Reject overly long numeric tokens before allocating temporary number strings to avoid unbounded allocations for malformed or malicious JSON input.
1 parent fb16e5c commit 92395c0

2 files changed

Lines changed: 9 additions & 1 deletion

File tree

cJSON.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu
323323
* of the current locale (for strtod)
324324
* This also takes care of '\0' not necessarily being available for marking the end of the input */
325325
for (i = 0; can_access_at_index(input_buffer, i); i++)
326-
{
326+
{
327327
switch (buffer_at_offset(input_buffer)[i])
328328
{
329329
case '0':
@@ -351,6 +351,10 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu
351351
default:
352352
goto loop_end;
353353
}
354+
if (number_string_length > CJSON_NUMBER_LENGTH_LIMIT)
355+
{
356+
return false;
357+
}
354358
}
355359
loop_end:
356360
/* malloc for temporary buffer, add 1 for '\0' */

cJSON.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ
9999
#define cJSON_IsReference 256
100100
#define cJSON_StringIsConst 512
101101

102+
#ifndef CJSON_NUMBER_LENGTH_LIMIT
103+
#define CJSON_NUMBER_LENGTH_LIMIT 512
104+
#endif
105+
102106
/* The cJSON structure: */
103107
typedef struct cJSON
104108
{

0 commit comments

Comments
 (0)