-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Remove multiple calls to free
when successively calling jq_reset
.
#3134
Conversation
Hey, thanks. Possible to add a minimal reuse/reset regression test case to https://github.com/jqlang/jq/blob/master/src/jq_test.c that would be detected by valgrind? |
63b2d0f
to
6ab6c29
Compare
Added a separate function for testing the Please let me know if you had something else in mind. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! looks good to me. Let's merge once one more maintainer is happy
Please drop the change to gitignore file. Files generated not by the project should not be in per-project gitignore file. |
`jq_reset` calls `jv_free` on the `exit_code` and the `error_message` stored on the jq state. However, it doesn't replace the actual instance of those members. This means that subsequent calls to `jq_reset` will call `jv_free` again on those members, which in turn may call `free` on the same pointer multiple times. Freeing the same pointer multiple times is undefined behavior and can cause heap corruption, which is how I spotted this issue. In practice, this issue only occurs when using a program that may `halt_error`, because that is when the `exit_code` and `error_message` are set to values other than `jv_invalid`. Subsequent attempts to call `jq_start` (which calls `jq_reset` internally) after hitting a `halt_error` can cause you to run into this issue. The changes simply reset the `exit_code` and the `error_message` to `jv_invalid` (the initial value set in `jq_init`) after they are freed.
6ab6c29
to
ae99179
Compare
Removed the commit with the changes to gitignore |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
jq_reset
callsjv_free
on theexit_code
and theerror_message
stored on the jq state. However, it doesn't replace the actual instance of those members. This means that subsequent calls tojq_reset
will calljv_free
again on those members, which in turn may callfree
on the same pointer multiple times. Freeing the same pointer multiple times is undefined behavior and can cause heap corruption, which is how I spotted this issue.In practice, this issue only occurs when using a program that may
halt_error
, because that is when theexit_code
anderror_message
are set to values other thanjv_invalid
. Subsequent attempts to calljq_start
(which callsjq_reset
internally) after hitting ahalt_error
can cause you to run into this issue.The changes simply reset the
exit_code
and theerror_message
tojv_invalid
(the initial value set injq_init
) after they are freed.