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

[C][Client]Fix data lost when libcurl write-data callback function is called multiple times #5828

Merged
merged 3 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,8 @@ end:
{{/returnTypeIsPrimitive}}
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
{{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}}
{{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}}
Expand Down Expand Up @@ -463,6 +465,8 @@ end:
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
{{#hasQueryParams}}list_free(localVarQueryParameters);{{/hasQueryParams}}
{{#hasHeaderParams}}list_free(localVarHeaderParameters);{{/hasHeaderParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ apiClient_t *apiClient_create() {
apiClient->basePath = strdup("{{{basePath}}}");
apiClient->sslConfig = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down Expand Up @@ -56,6 +57,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
}

apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down Expand Up @@ -466,7 +468,7 @@ void apiClient_invoke(apiClient_t *apiClient,
writeDataCallback);
curl_easy_setopt(handle,
CURLOPT_WRITEDATA,
&apiClient->dataReceived);
apiClient);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable

Expand Down Expand Up @@ -556,9 +558,12 @@ void apiClient_invoke(apiClient_t *apiClient,
}

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
*(char **) userp = strdup(buffer);

return size * nmemb;
size_t size_this_time = nmemb * size;
apiClient_t *apiClient = (apiClient_t *)userp;
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
return size_this_time;
}

char *strReplace(char *orig, char *rep, char *with) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef struct apiClient_t {
char *basePath;
sslConfig_t *sslConfig;
void *dataReceived;
long dataReceivedLen;
long response_code;
{{#hasAuthMethods}}
{{#authMethods}}
Expand Down
16 changes: 16 additions & 0 deletions samples/client/petstore/c/api/PetAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ PetAPI_addPet(apiClient_t *apiClient, pet_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -176,6 +178,8 @@ PetAPI_deletePet(apiClient_t *apiClient, long petId , char * api_key )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}

list_free(localVarHeaderParameters);
Expand Down Expand Up @@ -256,6 +260,8 @@ PetAPI_findPetsByStatus(apiClient_t *apiClient, list_t * status )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_free(localVarQueryParameters);

Expand Down Expand Up @@ -335,6 +341,8 @@ PetAPI_findPetsByTags(apiClient_t *apiClient, list_t * tags )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_free(localVarQueryParameters);

Expand Down Expand Up @@ -415,6 +423,8 @@ PetAPI_getPetById(apiClient_t *apiClient, long petId )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -482,6 +492,8 @@ PetAPI_updatePet(apiClient_t *apiClient, pet_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -569,6 +581,8 @@ PetAPI_updatePetWithForm(apiClient_t *apiClient, long petId , char * name , char
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -669,6 +683,8 @@ PetAPI_uploadFile(apiClient_t *apiClient, long petId , char * additionalMetadata
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down
8 changes: 8 additions & 0 deletions samples/client/petstore/c/api/StoreAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ StoreAPI_deleteOrder(apiClient_t *apiClient, char * orderId )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -121,6 +123,8 @@ StoreAPI_getInventory(apiClient_t *apiClient)

if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -201,6 +205,8 @@ StoreAPI_getOrderById(apiClient_t *apiClient, long orderId )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -272,6 +278,8 @@ StoreAPI_placeOrder(apiClient_t *apiClient, order_t * body )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down
16 changes: 16 additions & 0 deletions samples/client/petstore/c/api/UserAPI.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ UserAPI_createUser(apiClient_t *apiClient, user_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -136,6 +138,8 @@ UserAPI_createUsersWithArrayInput(apiClient_t *apiClient, list_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -215,6 +219,8 @@ UserAPI_createUsersWithListInput(apiClient_t *apiClient, list_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -280,6 +286,8 @@ UserAPI_deleteUser(apiClient_t *apiClient, char * username )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -352,6 +360,8 @@ UserAPI_getUserByName(apiClient_t *apiClient, char * username )
//return type
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -432,6 +442,8 @@ UserAPI_loginUser(apiClient_t *apiClient, char * username , char * password )

if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}
list_free(localVarQueryParameters);

Expand Down Expand Up @@ -505,6 +517,8 @@ UserAPI_logoutUser(apiClient_t *apiClient)
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down Expand Up @@ -575,6 +589,8 @@ UserAPI_updateUser(apiClient_t *apiClient, char * username , user_t * body )
end:
if (apiClient->dataReceived) {
free(apiClient->dataReceived);
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
}


Expand Down
1 change: 1 addition & 0 deletions samples/client/petstore/c/include/apiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ typedef struct apiClient_t {
char *basePath;
sslConfig_t *sslConfig;
void *dataReceived;
long dataReceivedLen;
long response_code;
list_t *apiKeys_api_key;
char *accessToken;
Expand Down
13 changes: 9 additions & 4 deletions samples/client/petstore/c/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ apiClient_t *apiClient_create() {
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
apiClient->sslConfig = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
apiClient->apiKeys_api_key = NULL;
apiClient->accessToken = NULL;
Expand All @@ -38,6 +39,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
}

apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
if(apiKeys_api_key!= NULL) {
apiClient->apiKeys_api_key = list_create();
Expand Down Expand Up @@ -414,7 +416,7 @@ void apiClient_invoke(apiClient_t *apiClient,
writeDataCallback);
curl_easy_setopt(handle,
CURLOPT_WRITEDATA,
&apiClient->dataReceived);
apiClient);
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable

Expand Down Expand Up @@ -462,9 +464,12 @@ void apiClient_invoke(apiClient_t *apiClient,
}

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
*(char **) userp = strdup(buffer);

return size * nmemb;
size_t size_this_time = nmemb * size;
apiClient_t *apiClient = (apiClient_t *)userp;
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
return size_this_time;
}

char *strReplace(char *orig, char *rep, char *with) {
Expand Down