From b2c794ac272a4dadd7750c30a0d128a69cd614f2 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Mon, 24 Aug 2020 20:45:34 +0800 Subject: [PATCH] [C][Client][Clang Static Analyzer] Fix memory leak in apiClient_invoke --- .../resources/C-libcurl/apiClient.c.mustache | 9 ++++++--- .../main/resources/C-libcurl/list.c.mustache | 17 +++++++++++++++++ .../main/resources/C-libcurl/list.h.mustache | 2 ++ samples/client/petstore/c/include/list.h | 2 ++ samples/client/petstore/c/src/apiClient.c | 9 ++++++--- samples/client/petstore/c/src/list.c | 17 +++++++++++++++++ 6 files changed, 50 insertions(+), 6 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache index 015a915afdff..900a986cb081 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache @@ -300,6 +300,8 @@ void apiClient_invoke(apiClient_t *apiClient, (char *) listEntry->data); headers = curl_slist_append(headers, buffContent); + free(buffContent); + buffContent = NULL; } } } else { @@ -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; @@ -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 = diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache index 13b8b23dc271..b6d16c7de086 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache @@ -1,6 +1,7 @@ #include #include #include +#include #include "../include/list.h" static listEntry_t *listEntry_create(void *data) { @@ -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; +} diff --git a/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache b/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache index 7d98d7f306b1..b2aa25b3963d 100644 --- a/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache +++ b/modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache @@ -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 diff --git a/samples/client/petstore/c/include/list.h b/samples/client/petstore/c/include/list.h index 7d98d7f306b1..b2aa25b3963d 100644 --- a/samples/client/petstore/c/include/list.h +++ b/samples/client/petstore/c/include/list.h @@ -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 diff --git a/samples/client/petstore/c/src/apiClient.c b/samples/client/petstore/c/src/apiClient.c index 355e4eab8617..e156bc53b959 100644 --- a/samples/client/petstore/c/src/apiClient.c +++ b/samples/client/petstore/c/src/apiClient.c @@ -254,6 +254,8 @@ void apiClient_invoke(apiClient_t *apiClient, (char *) listEntry->data); headers = curl_slist_append(headers, buffContent); + free(buffContent); + buffContent = NULL; } } } else { @@ -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; @@ -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 = diff --git a/samples/client/petstore/c/src/list.c b/samples/client/petstore/c/src/list.c index 13b8b23dc271..b6d16c7de086 100644 --- a/samples/client/petstore/c/src/list.c +++ b/samples/client/petstore/c/src/list.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "../include/list.h" static listEntry_t *listEntry_create(void *data) { @@ -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; +}