Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute cJSON_Version() at compile time #858

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Compute cJSON_Version at compile time
As CJSON_VERSION_... macros are known to be constant integers, it is
possible to use C macros to transform them to literal strings and to
directly use the result in function cJSON_Version.

As a side-effect, this makes cJSON_Version thread-safe, as a there is no
longer a shared buffer (static char version[15]) shared between threads.
  • Loading branch information
niooss-ledger committed May 14, 2024
commit d34dda6d6e334737f5eabfca3231add23769be77
9 changes: 5 additions & 4 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 121,13 @@ CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item)
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
#endif

#define STRINGIFY_(s) #s
#define STRINGIFY(s) STRINGIFY_(s)

CJSON_PUBLIC(const char*) cJSON_Version(void)
{
static char version[15];
sprintf(version, "%i.%i.%i", CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);

return version;
return STRINGIFY(CJSON_VERSION_MAJOR) "." STRINGIFY(CJSON_VERSION_MINOR) "." \
STRINGIFY(CJSON_VERSION_PATCH);
}

/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
Expand Down
Loading