Skip to content
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
13 changes: 8 additions & 5 deletions cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ CJSON_PUBLIC(const char*) cJSON_Version(void)
/* Case insensitive string comparison, doesn't consider two NULL pointers equal though */
static int case_insensitive_strcmp(const unsigned char *string1, const unsigned char *string2)
{
int l = 0;
int r = 0;
if ((string1 == NULL) || (string2 == NULL))
{
return 1;
Expand All @@ -142,15 +144,16 @@ static int case_insensitive_strcmp(const unsigned char *string1, const unsigned
return 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider calling strcasecmp(3) directly when available after checking NULL pointer? This library function has been optimized, and you almost can't write one faster than it.

Copy link
Author

@gardonkoo gardonkoo Sep 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function case_insensitive_strcmp is a native implementation of the CJSON library.
According to the code, it is mainly used to find items in a JSON object by name.
By default, two NULL values are not considered equal.
It does not care about the length/size of the two strings, neither the magnitude relationship in ASCII order.
It only cares about whether they are equal.
Its functionality is not entirely consistent with that of strcasecmp(3) (at least in the current usage scenario within the source code).
Additionally, strcasecmp(3) is a POSIX-compliant function, while CJSON is a cross-platform library.
Perhaps we need to use macros to distinguish between different platforms and call the optimized functions for each respective platform; or we need to implement a cross-platform general interface ourselves. CJSON chose the latter.

}

for(; tolower(*string1) == tolower(*string2); (void)string1++, string2++)
while (true)
{
if (*string1 == '\0')
l = tolower(*string1++);
r = tolower(*string2++);
if (l != r || l == 0)
{
return 0;
return l - r;
}
}

return tolower(*string1) - tolower(*string2);
return 0;
}

typedef struct internal_hooks
Expand Down