Skip to content

Commit

Permalink
[C][Client][Clang Static Analyzer] Fix memory leak in apiClient_invoke (
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuhui authored Aug 25, 2020
1 parent d868fd6 commit 1852f61
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
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;
}

0 comments on commit 1852f61

Please sign in to comment.