Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d6d5449

Browse files
vwvwAlanscut
authored andcommitted
fix DaveGamble#881, check overlap before calling strcpy in cJSON_SetValuestring
1 parent a78d975 commit d6d5449

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

‎cJSON.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,8 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
403403
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
404404
{
405405
char *copy = NULL;
406+
size_t v1_len;
407+
size_t v2_len;
406408
/* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
407409
if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference))
408410
{
@@ -413,8 +415,17 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
413415
{
414416
return NULL;
415417
}
416-
if (strlen(valuestring) <= strlen(object->valuestring))
418+
419+
v1_len = strlen(valuestring);
420+
v2_len = strlen(object->valuestring);
421+
422+
if (v1_len <= v2_len)
417423
{
424+
/* strcpy does not handle overlapping string: [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */
425+
if (!( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring ))
426+
{
427+
return NULL;
428+
}
418429
strcpy(object->valuestring, valuestring);
419430
return object->valuestring;
420431
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /