Skip to content

Commit 1852f61

Browse files
authored
[C][Client][Clang Static Analyzer] Fix memory leak in apiClient_invoke (#7285)
1 parent d868fd6 commit 1852f61

File tree

6 files changed

+50
-6
lines changed

6 files changed

+50
-6
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ void apiClient_invoke(apiClient_t *apiClient,
300300
(char *) listEntry->data);
301301
headers = curl_slist_append(headers,
302302
buffContent);
303+
free(buffContent);
304+
buffContent = NULL;
303305
}
304306
}
305307
} else {
@@ -313,8 +315,8 @@ void apiClient_invoke(apiClient_t *apiClient,
313315
}
314316

315317
if(formParameters != NULL) {
316-
if(strstr(buffContent,
317-
"application/x-www-form-urlencoded") != NULL)
318+
if(contentType &&
319+
findStrInStrList(contentType, "application/x-www-form-urlencoded") != NULL)
318320
{
319321
long parameterLength = 0;
320322
long keyPairLength = 0;
@@ -356,7 +358,8 @@ void apiClient_invoke(apiClient_t *apiClient,
356358
curl_easy_setopt(handle, CURLOPT_POSTFIELDS,
357359
formString);
358360
}
359-
if(strstr(buffContent, "multipart/form-data") != NULL) {
361+
if(contentType &&
362+
findStrInStrList(contentType, "multipart/form-data") != NULL) {
360363
mime = curl_mime_init(handle);
361364
list_ForEach(listEntry, formParameters) {
362365
keyValuePair_t *keyValuePair =

modules/openapi-generator/src/main/resources/C-libcurl/list.c.mustache

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stddef.h>
22
#include <stdlib.h>
33
#include <stdio.h>
4+
#include <string.h>
45

56
#include "../include/list.h"
67
static listEntry_t *listEntry_create(void *data) {
@@ -166,3 +167,19 @@ listEntry_t *list_getElementAt(list_t *list, long indexOfElement) {
166167
return currentListEntry;
167168
}
168169
}
170+
171+
char* findStrInStrList(list_t *strList, const char *str)
172+
{
173+
if (!strList || !str) {
174+
return NULL;
175+
}
176+
177+
listEntry_t* listEntry = NULL;
178+
list_ForEach(listEntry, strList) {
179+
if (strstr((char*)listEntry->data, str) != NULL) {
180+
return (char*)listEntry->data;
181+
}
182+
}
183+
184+
return NULL;
185+
}

modules/openapi-generator/src/main/resources/C-libcurl/list.h.mustache

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ void list_iterateThroughListBackward(list_t* list, void (*operationToPerform)(li
3636

3737
void listEntry_printAsInt(listEntry_t* listEntry, void *additionalData);
3838
void listEntry_free(listEntry_t *listEntry, void *additionalData);
39+
40+
char* findStrInStrList(list_t* strList, const char* str);
3941
#endif // INCLUDE_LIST_H

samples/client/petstore/c/include/list.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ void list_iterateThroughListBackward(list_t* list, void (*operationToPerform)(li
3636

3737
void listEntry_printAsInt(listEntry_t* listEntry, void *additionalData);
3838
void listEntry_free(listEntry_t *listEntry, void *additionalData);
39+
40+
char* findStrInStrList(list_t* strList, const char* str);
3941
#endif // INCLUDE_LIST_H

samples/client/petstore/c/src/apiClient.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ void apiClient_invoke(apiClient_t *apiClient,
254254
(char *) listEntry->data);
255255
headers = curl_slist_append(headers,
256256
buffContent);
257+
free(buffContent);
258+
buffContent = NULL;
257259
}
258260
}
259261
} else {
@@ -267,8 +269,8 @@ void apiClient_invoke(apiClient_t *apiClient,
267269
}
268270

269271
if(formParameters != NULL) {
270-
if(strstr(buffContent,
271-
"application/x-www-form-urlencoded") != NULL)
272+
if(contentType &&
273+
findStrInStrList(contentType, "application/x-www-form-urlencoded") != NULL)
272274
{
273275
long parameterLength = 0;
274276
long keyPairLength = 0;
@@ -310,7 +312,8 @@ void apiClient_invoke(apiClient_t *apiClient,
310312
curl_easy_setopt(handle, CURLOPT_POSTFIELDS,
311313
formString);
312314
}
313-
if(strstr(buffContent, "multipart/form-data") != NULL) {
315+
if(contentType &&
316+
findStrInStrList(contentType, "multipart/form-data") != NULL) {
314317
mime = curl_mime_init(handle);
315318
list_ForEach(listEntry, formParameters) {
316319
keyValuePair_t *keyValuePair =

samples/client/petstore/c/src/list.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include <stddef.h>
22
#include <stdlib.h>
33
#include <stdio.h>
4+
#include <string.h>
45

56
#include "../include/list.h"
67
static listEntry_t *listEntry_create(void *data) {
@@ -166,3 +167,19 @@ listEntry_t *list_getElementAt(list_t *list, long indexOfElement) {
166167
return currentListEntry;
167168
}
168169
}
170+
171+
char* findStrInStrList(list_t *strList, const char *str)
172+
{
173+
if (!strList || !str) {
174+
return NULL;
175+
}
176+
177+
listEntry_t* listEntry = NULL;
178+
list_ForEach(listEntry, strList) {
179+
if (strstr((char*)listEntry->data, str) != NULL) {
180+
return (char*)listEntry->data;
181+
}
182+
}
183+
184+
return NULL;
185+
}

0 commit comments

Comments
 (0)