Skip to content

Commit

Permalink
Fix memory leak when performing a request without accessing the respo…
Browse files Browse the repository at this point in the history
…nse data
  • Loading branch information
ErikMinekus committed Mar 24, 2020
1 parent 384f132 commit 47f3056
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
5 changes: 1 addition & 4 deletions extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ static void CheckCompletedRequests()

if (res == CURLE_OK)
{
context->response.data = json_loads(context->response.body, 0, NULL);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &context->response.status);
}

Expand Down Expand Up @@ -283,9 +282,7 @@ void HTTPClientObjectHandler::OnHandleDestroy(HandleType_t type, void *object)

void HTTPResponseObjectHandler::OnHandleDestroy(HandleType_t type, void *object)
{
struct HTTPResponse *response = (struct HTTPResponse *)object;

free(response->body);
/* Response objects are automatically cleaned up */
}

void JSONObjectHandler::OnHandleDestroy(HandleType_t type, void *object)
Expand Down
15 changes: 10 additions & 5 deletions http_natives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,22 @@ static cell_t GetResponseData(IPluginContext *pContext, const cell_t *params)
return BAD_HANDLE;
}

if (response->data == NULL)
{
return BAD_HANDLE;
}

/* Return the same handle every time we get the HTTP response data */
if (response->hndlData == BAD_HANDLE)
{
json_error_t error;
response->data = json_loads(response->body, 0, &error);
if (response->data == NULL)
{
pContext->ThrowNativeError("Invalid JSON in line %d, column %d: %s", error.line, error.column, error.text);
return BAD_HANDLE;
}

response->hndlData = handlesys->CreateHandleEx(htJSONObject, response->data, &sec, NULL, NULL);
if (response->hndlData == BAD_HANDLE)
{
json_decref(response->data);

pContext->ThrowNativeError("Could not create data handle.");
return BAD_HANDLE;
}
Expand Down
3 changes: 1 addition & 2 deletions httpcontext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ HTTPContext::~HTTPContext()
curl_easy_cleanup(curl);
curl_slist_free_all(headers);
free(request.body);
free(response.body);
}

void HTTPContext::OnCompleted()
{
/* Return early if the plugin was unloaded while the thread was running */
if (forward->GetFunctionCount() == 0)
{
free(response.body);
json_decref(response.data);

forwards->ReleaseForward(forward);
Expand All @@ -148,7 +148,6 @@ void HTTPContext::OnCompleted()
Handle_t hndlResponse = handlesys->CreateHandleEx(htHTTPResponseObject, &response, &sec, NULL, NULL);
if (hndlResponse == BAD_HANDLE)
{
free(response.body);
json_decref(response.data);

forwards->ReleaseForward(forward);
Expand Down

0 comments on commit 47f3056

Please sign in to comment.