Fix/remove incorrect rc decrement causing double free and leak reported#113
Open
faldor20 wants to merge 1 commit into
Open
Fix/remove incorrect rc decrement causing double free and leak reported#113faldor20 wants to merge 1 commit into
faldor20 wants to merge 1 commit into
Conversation
Decrementing the rc here creates a double free because it creates a new handle and both handles will be garbage collected freeing the underlying connection twice.
Collaborator
|
Hello, thanks for the detective work. Just trying to understand, though:
The function |
Author
|
Ahhh, I see, I missed that, good point. I'll take another look when I'm back at my computer. Seems like we shoudlbeb able to check if the handle was actually removed or some such. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
So I've been building a client for Picos using the multi socket api and I've been fighting double free and leaked handle errors for a week now. I finally narrowed it down to definitely being somewhere in the bindings/libcurl.
I'm sure this function is the culprit:
So rough overview:
value caml_curlm_remove_finished(value v_multi) { CAMLparam1(v_multi); CAMLlocal2(v_easy, v_tuple); CURL* handle; CURLM* multi_handle; CURLcode result; Connection* conn = NULL; multi_handle = CURLM_val(v_multi); caml_release_runtime_system(); handle = curlm_remove_finished(multi_handle,&result); caml_acquire_runtime_system(); if (NULL == handle) { CAMLreturn(Val_none); } else { conn = getConnection(handle); if (conn->curl_ERRORBUFFER != NULL) { Store_field(Field(conn->ocamlValues, Ocaml_ERRORBUFFER), 0, caml_copy_string(conn->curl_ERRORBUFFER)); } conn->refcount--; /* NB: same handle, but different block */ v_easy = caml_curl_alloc(conn); v_tuple = caml_alloc_tuple(2); Store_field(v_tuple,0,v_easy); Store_field(v_tuple,1,Val_int(result)); /* CURLcode */ CAMLreturn(caml_alloc_some(v_tuple)); } }the reason is that it makes the incorrect assumption that the refcount should be decremented, without considering that this new handle will be garbage collected and finalisation will be run twice
I also added logging to see this effect in practice:
This was a hard won find...