update comments and add tests for cJSON_SetValuestring

This commit is contained in:
Alanscut 2024-04-28 18:09:25 +08:00 committed by Alan Wang
parent 5671646e97
commit 19396a49a6
2 changed files with 3 additions and 1 deletions

View File

@ -397,6 +397,7 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
return object->valuedouble = number; return object->valuedouble = number;
} }
/* Note: when passing a NULL valuestring, cJSON_SetValuestring treats this as an error and return NULL */
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring) CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{ {
char *copy = NULL; char *copy = NULL;
@ -405,7 +406,7 @@ CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{ {
return NULL; return NULL;
} }
/* return NULL if the object is corrupted */ /* return NULL if the object is corrupted or valuestring is NULL */
if (object->valuestring == NULL || valuestring == NULL) if (object->valuestring == NULL || valuestring == NULL)
{ {
return NULL; return NULL;

View File

@ -444,6 +444,7 @@ static void cjson_functions_should_not_crash_with_null_pointers(void)
TEST_ASSERT_FALSE(cJSON_Compare(NULL, item, false)); TEST_ASSERT_FALSE(cJSON_Compare(NULL, item, false));
TEST_ASSERT_NULL(cJSON_SetValuestring(NULL, "test")); TEST_ASSERT_NULL(cJSON_SetValuestring(NULL, "test"));
TEST_ASSERT_NULL(cJSON_SetValuestring(corruptedString, "test")); TEST_ASSERT_NULL(cJSON_SetValuestring(corruptedString, "test"));
TEST_ASSERT_NULL(cJSON_SetValuestring(item, NULL));
cJSON_Minify(NULL); cJSON_Minify(NULL);
/* skipped because it is only used via a macro that checks for NULL */ /* skipped because it is only used via a macro that checks for NULL */
/* cJSON_SetNumberHelper(NULL, 0); */ /* cJSON_SetNumberHelper(NULL, 0); */