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

Fix Undefined Behavior by replacing illegal overlap test with memmove() #924

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

imaami
Copy link

@imaami imaami commented Jan 19, 2025

I pointed this out in #919, sorry for taking so long to make a PR. But here it is. No need to check for string overlap–it's simply not legal in C, and memmove() exists to guarantee that overlapping copies don't cause unforeseen problems.

According to the C standard it is Undefined Behavior to arithmetically
compare addresses that don't point to within the same object. There is
simply no way to implement this sort of check legally. The correct way
to avoid problems from overlapping strings is to replace strcpy() with
memmove().
Minor optimizations to cJSON_SetValueString() program flow:

- NULL check the input first before going into more detail.
- Check the type flags with one bitwise AND instead of two.
- Remove a redundant second object->valuestring NULL check.
@imaami
Copy link
Author

imaami commented Jan 21, 2025

@DaveGamble are you busy with life these days? It's understandable. But it would be nice to see cJSON improve, too.

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.

1 participant