Skip to content

Commit

Permalink
fix #881, check overlap before calling strcpy in cJSON_SetValuestring
Browse files Browse the repository at this point in the history
  • Loading branch information
vwvw committed Aug 23, 2024
1 parent 424ce4c commit d3b2452
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{
char *copy = NULL;
size_t v1_len;
size_t v2_len;
/* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference))
{
Expand All @@ -413,8 +415,17 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{
return NULL;
}
if (strlen(valuestring) <= strlen(object->valuestring))

v1_len = strlen(valuestring);
v2_len = strlen(object->valuestring);

if (v1_len <= v2_len)
{
/* strcpy does not handle overlapping string: [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */
if (!( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring ))
{
return NULL;
}
strcpy(object->valuestring, valuestring);
return object->valuestring;
}
Expand Down

0 comments on commit d3b2452

Please sign in to comment.