Skip to content

Fix setvaluestring ptr compare - #1132

Open
kanishk25249-sudo wants to merge 2 commits into
DaveGamble:masterfrom
kanishk25249-sudo:fix-setvaluestring-ptr-compare
Open

kanishk25249-sudo wants to merge 2 commits into
DaveGamble:masterfrom
kanishk25249-sudo:fix-setvaluestring-ptr-compare

Conversation

@kanishk25249-sudo

Copy link
Copy Markdown

Fixes #1106.
The previous implementation compared unrelated pointers using relational operators (<), which results in undefined behavior in C.
This PR replaces the pointer comparison with a strings_overlap() helper that uses pointer equality checks to detect overlapping memory regions.
The existing behavior is preserved: cJSON_SetValuestring() still returns NULL when the source and destination strings overlap.
The helper introduces O(length) additional time complexity, which is the same order as the existing strlen() calls.

@kanishk25249-sudo

Copy link
Copy Markdown
Author

I've opened a PR for this: #1106.
It replaces the relational pointer comparison with a strings_overlap() helper that uses pointer equality to detect overlapping strings, avoiding undefined behavior while preserving the existing behavior of returning NULL on overlap.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pointer comparsion undefined behavior in cJSON_SetValuestring

1 participant