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][Clang Static Analyzer] Fix memory leak in apiClient_invoke #7285

Merged
merged 1 commit into from
Aug 25, 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 @@ -300,6 +300,8 @@ void apiClient_invoke(apiClient_t *apiClient,
(char *) listEntry->data);
headers = curl_slist_append(headers,
buffContent);
free(buffContent);
buffContent = NULL;
}
}
} else {
Expand All @@ -313,8 +315,8 @@ void apiClient_invoke(apiClient_t *apiClient,
}

if(formParameters != NULL) {
if(strstr(buffContent,
"application/x-www-form-urlencoded") != NULL)
if(contentType &&
findStrInStrList(contentType, "application/x-www-form-urlencoded") != NULL)
{
long parameterLength = 0;
long keyPairLength = 0;
Expand Down Expand Up @@ -356,7 +358,8 @@ void apiClient_invoke(apiClient_t *apiClient,
curl_easy_setopt(handle, CURLOPT_POSTFIELDS,
formString);
}
if(strstr(buffContent, "multipart/form-data") != NULL) {
if(contentType &&
findStrInStrList(contentType, "multipart/form-data") != NULL) {
mime = curl_mime_init(handle);
list_ForEach(listEntry, formParameters) {
keyValuePair_t *keyValuePair =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../include/list.h"
static listEntry_t *listEntry_create(void *data) {
Expand Down Expand Up @@ -166,3 +167,19 @@ listEntry_t *list_getElementAt(list_t *list, long indexOfElement) {
return currentListEntry;
}
}

char* findStrInStrList(list_t *strList, const char *str)
{
if (!strList || !str) {
return NULL;
}

listEntry_t* listEntry = NULL;
list_ForEach(listEntry, strList) {
if (strstr((char*)listEntry->data, str) != NULL) {
return (char*)listEntry->data;
}
}

return NULL;
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ void list_iterateThroughListBackward(list_t* list, void (*operationToPerform)(li

void listEntry_printAsInt(listEntry_t* listEntry, void *additionalData);
void listEntry_free(listEntry_t *listEntry, void *additionalData);

char* findStrInStrList(list_t* strList, const char* str);
#endif // INCLUDE_LIST_H
2 changes: 2 additions & 0 deletions samples/client/petstore/c/include/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,6 @@ void list_iterateThroughListBackward(list_t* list, void (*operationToPerform)(li

void listEntry_printAsInt(listEntry_t* listEntry, void *additionalData);
void listEntry_free(listEntry_t *listEntry, void *additionalData);

char* findStrInStrList(list_t* strList, const char* str);
#endif // INCLUDE_LIST_H
9 changes: 6 additions & 3 deletions samples/client/petstore/c/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ void apiClient_invoke(apiClient_t *apiClient,
(char *) listEntry->data);
headers = curl_slist_append(headers,
buffContent);
free(buffContent);
buffContent = NULL;
}
}
} else {
Expand All @@ -267,8 +269,8 @@ void apiClient_invoke(apiClient_t *apiClient,
}

if(formParameters != NULL) {
if(strstr(buffContent,
"application/x-www-form-urlencoded") != NULL)
if(contentType &&
findStrInStrList(contentType, "application/x-www-form-urlencoded") != NULL)
{
long parameterLength = 0;
long keyPairLength = 0;
Expand Down Expand Up @@ -310,7 +312,8 @@ void apiClient_invoke(apiClient_t *apiClient,
curl_easy_setopt(handle, CURLOPT_POSTFIELDS,
formString);
}
if(strstr(buffContent, "multipart/form-data") != NULL) {
if(contentType &&
findStrInStrList(contentType, "multipart/form-data") != NULL) {
mime = curl_mime_init(handle);
list_ForEach(listEntry, formParameters) {
keyValuePair_t *keyValuePair =
Expand Down
17 changes: 17 additions & 0 deletions samples/client/petstore/c/src/list.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../include/list.h"
static listEntry_t *listEntry_create(void *data) {
Expand Down Expand Up @@ -166,3 +167,19 @@ listEntry_t *list_getElementAt(list_t *list, long indexOfElement) {
return currentListEntry;
}
}

char* findStrInStrList(list_t *strList, const char *str)
{
if (!strList || !str) {
return NULL;
}

listEntry_t* listEntry = NULL;
list_ForEach(listEntry, strList) {
if (strstr((char*)listEntry->data, str) != NULL) {
return (char*)listEntry->data;
}
}

return NULL;
}