Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */
| 433 | |
| 434 | /* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */ |
| 435 | CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) |
| 436 | { |
| 437 | char *copy = NULL; |
| 438 | size_t v1_len; |
| 439 | size_t v2_len; |
| 440 | /* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */ |
| 441 | if ((object == NULL) || !(object->type & cJSON_String) || (object->type & cJSON_IsReference)) |
| 442 | { |
| 443 | return NULL; |
| 444 | } |
| 445 | /* return NULL if the object is corrupted or valuestring is NULL */ |
| 446 | if (object->valuestring == NULL || valuestring == NULL) |
| 447 | { |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | v1_len = strlen(valuestring); |
| 452 | v2_len = strlen(object->valuestring); |
| 453 | |
| 454 | if (v1_len <= v2_len) |
| 455 | { |
| 456 | /* strcpy does not handle overlapping string: [X1, X2] [Y1, Y2] => X2 < Y1 or Y2 < X1 */ |
| 457 | if (!( valuestring + v1_len < object->valuestring || object->valuestring + v2_len < valuestring )) |
| 458 | { |
| 459 | return NULL; |
| 460 | } |
| 461 | strcpy(object->valuestring, valuestring); |
| 462 | return object->valuestring; |
| 463 | } |
| 464 | copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks); |
| 465 | if (copy == NULL) |
| 466 | { |
| 467 | return NULL; |
| 468 | } |
| 469 | if (object->valuestring != NULL) |
| 470 | { |
| 471 | cJSON_free(object->valuestring); |
| 472 | } |
| 473 | object->valuestring = copy; |
| 474 | |
| 475 | return copy; |
| 476 | } |
| 477 | |
| 478 | typedef struct |
| 479 | { |
nothing calls this directly
no test coverage detected