Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ However it is thread safe under the following conditions:

* `cJSON_GetErrorPtr` is never used (the `return_parse_end` parameter of `cJSON_ParseWithOpts` can be used instead)
* `cJSON_InitHooks` is only ever called before using cJSON in any threads.
* `cJSON_Version` is not called concurrently from multiple threads. Note that `cJSON_Version` returns a pointer to an internal buffer that is overwritten on every call, so the returned pointer is only valid until the next call to `cJSON_Version` and must not be shared between threads (copy the string if you need to keep it).
* `setlocale` is never called before all calls to cJSON functions have returned.

#### Case Sensitivity
Expand Down
6 changes: 5 additions & 1 deletion cJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,11 @@ typedef int cJSON_bool;
#define CJSON_CIRCULAR_LIMIT 10000
#endif

/* returns the version of cJSON as a string */
/* returns the version of cJSON as a string.
* NOTE: the returned pointer references an internal static buffer that is
* overwritten on every call. The pointer is only valid until the next call to
* cJSON_Version, and the function is not thread safe, so do not share the
* returned pointer between threads. Copy the string if you need to keep it. */
CJSON_PUBLIC(const char*) cJSON_Version(void);

/* Supply malloc, realloc and free functions to cJSON */
Expand Down
16 changes: 16 additions & 0 deletions tests/misc_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,21 @@ static void cjson_parse_big_numbers_should_not_report_error(void)
cJSON_Delete(valid_big_number_json_object2);
}

static void cjson_version_should_return_reused_static_buffer(void)
{
/* cJSON_Version returns a pointer to an internal static buffer that is
* overwritten on every call. The pointer is only valid until the next call
* to cJSON_Version and must not be shared between threads. This captures
* the documented non-reentrant contract (see issue E in the static review). */
const char *first = cJSON_Version();
const char *second = cJSON_Version();

TEST_ASSERT_NOT_NULL(first);
TEST_ASSERT_NOT_NULL(second);
/* both calls return the same static buffer */
TEST_ASSERT_EQUAL_PTR(first, second);
}

int CJSON_CDECL main(void)
{
UNITY_BEGIN();
Expand Down Expand Up @@ -833,6 +848,7 @@ int CJSON_CDECL main(void)
RUN_TEST(cjson_set_valuestring_to_object_should_not_leak_memory);
RUN_TEST(cjson_set_bool_value_must_not_break_objects);
RUN_TEST(cjson_parse_big_numbers_should_not_report_error);
RUN_TEST(cjson_version_should_return_reused_static_buffer);

return UNITY_END();
}