-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
no NULL pointer check in cJSON_DetachItemViaPointer #882
Comments
Given item being the first element of Besides this, cjson is designed to believe the input arguments are correct, which means the else if (item->next == NULL)
{
/* last element */
parent->child->prev = item->prev;
} As We can valid the input arguments, expensively. We can iterate the Let's get back to your case. We and add a null check for |
This sentence makes me think of a possible solution. Can we modify the struct This will automatically tell if two nodes belong to the same tree. This field should be updated if you detach. |
Btw, I disagree with a statement of yours. In the snippet below, IMO, I would avoid any NULL pointer dereference as much as possible. This may lead to a crash of the process. else if (item->next == NULL)
{
/* last element */
parent->child->prev = item->prev;
} |
Even with a root reference, a caller can easily forge a corrupted item like this: int main(int argc, char** argv) {
cJSON *a, *b;
a = cJSON_ParseWithOpts("\"foo\"", nullptr, 0);
b = cJSON_ParseWithOpts("\"bar\"", nullptr, 0);
// bypass the root valid
b->root = a->root;
cJSON_DetachItemViaPointer(b, a);
return 0;
} |
Personally I do agree with you. But |
I just noticed that the function
cJSON_DetachItemViaPointer
does not perform a proper null-check foritem->prev
for the second argument. Library commit3249730
.Let's take this simple example:
item
argument is like:but there is no check for
item->prev
:I can write a PR but I do not know how it is the intended behavior of the library. Where is the best place to put the NULL check?
The text was updated successfully, but these errors were encountered: