Skip to content

Commit 40d4703

Browse files
authored
C overhead fixes (#20402)
* [C][Client] Remove redundant casts Don't explicitly cast void pointers as it's unnecessary in C, but leave printf arguments untouched to avoid setting off -Wformat. * [C][Client] Cosmetic: remove unnecessary parens, align some lines * [C][Client] Reduce number of unnecessary strlen() calls
1 parent 536519c commit 40d4703

File tree

23 files changed

+187
-355
lines changed

23 files changed

+187
-355
lines changed

modules/openapi-generator/src/main/resources/C-libcurl/api-body.mustache

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,7 @@ end:
108108
apiClient->response_code = 0;
109109

110110
// create the path
111-
long sizeOfPath = strlen("{{{path}}}")+1;
112-
char *localVarPath = malloc(sizeOfPath);
113-
snprintf(localVarPath, sizeOfPath, "{{{path}}}");
111+
char *localVarPath = strdup("{{{path}}}");
114112

115113
{{#pathParams}}
116114
{{#isString}}
@@ -126,7 +124,7 @@ end:
126124
{{#pathParams}}
127125

128126
// Path Params
129-
long sizeOfPathParams_{{{paramName}}} = {{#pathParams}}{{#isLong}}sizeof({{paramName}})+3{{/isLong}}{{#isString}}strlen({{^isEnum}}{{paramName}}{{/isEnum}}{{#isEnum}}{{{operationId}}}_{{enumName}}_ToString({{paramName}}){{/isEnum}})+3{{/isString}}{{^-last}} + {{/-last}}{{/pathParams}} + strlen("{ {{baseName}} }");
127+
long sizeOfPathParams_{{{paramName}}} = {{#pathParams}}{{#isLong}}sizeof({{paramName}})+3{{/isLong}}{{#isString}}strlen({{^isEnum}}{{paramName}}{{/isEnum}}{{#isEnum}}{{{operationId}}}_{{enumName}}_ToString({{paramName}}){{/isEnum}})+3{{/isString}}{{^-last}} + {{/-last}}{{/pathParams}} + sizeof("{ {{baseName}} }") - 1;
130128
{{#isNumeric}}
131129
if({{paramName}} == 0){
132130
goto end;
@@ -254,7 +252,7 @@ end:
254252
valueQuery_{{{paramName}}} = {{#isString}}{{^isEnum}}strdup({{/isEnum}}{{/isString}}({{{paramName}}}){{#isString}}{{^isEnum}}){{/isEnum}}{{/isString}};
255253
{{/isBoolean}}
256254
{{/isInteger}}
257-
keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}(void *)strdup({{{operationId}}}_{{enumName}}_ToString(
255+
keyPairQuery_{{paramName}} = keyValuePair_create(keyQuery_{{{paramName}}}, {{#isEnum}}strdup({{{operationId}}}_{{enumName}}_ToString(
258256
{{/isEnum}}{{^isString}}{{^isInteger}}{{^isBoolean}}&{{/isBoolean}}{{/isInteger}}{{/isString}}valueQuery_{{{paramName}}}{{#isEnum}})){{/isEnum}});
259257
list_addElement(localVarQueryParameters,keyPairQuery_{{paramName}});
260258
{{/isArray}}

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

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,11 @@ void sslConfig_free(sslConfig_t *sslConfig) {
173173
free(sslConfig);
174174
}
175175

176-
static void replaceSpaceWithPlus(char *stringToProcess) {
177-
for(int i = 0; i < strlen(stringToProcess); i++) {
178-
if(stringToProcess[i] == ' ') {
179-
stringToProcess[i] = '+';
176+
static void replaceSpaceWithPlus(char *str) {
177+
if (str) {
178+
for (; *str; str++) {
179+
if (*str == ' ')
180+
*str = '+';
180181
}
181182
}
182183
}
@@ -286,38 +287,26 @@ void apiClient_invoke(apiClient_t *apiClient,
286287
287288
if(headerType != NULL) {
288289
list_ForEach(listEntry, headerType) {
289-
if(strstr((char *) listEntry->data,
290-
"xml") == NULL)
290+
if(strstr(listEntry->data, "xml") == NULL)
291291
{
292-
buffHeader = malloc(strlen(
293-
"Accept: ") +
294-
strlen((char *)
295-
listEntry->
296-
data) + 1);
297-
sprintf(buffHeader, "%s%s", "Accept: ",
292+
buffHeader = malloc(sizeof("Accept: ") +
293+
strlen(listEntry->data));
294+
sprintf(buffHeader, "Accept: %s",
298295
(char *) listEntry->data);
299-
headers = curl_slist_append(headers,
300-
buffHeader);
296+
headers = curl_slist_append(headers, buffHeader);
301297
free(buffHeader);
302298
}
303299
}
304300
}
305301
if(contentType != NULL) {
306302
list_ForEach(listEntry, contentType) {
307-
if(strstr((char *) listEntry->data,
308-
"xml") == NULL)
303+
if(strstr(listEntry->data, "xml") == NULL)
309304
{
310-
buffContent =
311-
malloc(strlen(
312-
"Content-Type: ") + strlen(
313-
(char *)
314-
listEntry->data) +
315-
1);
316-
sprintf(buffContent, "%s%s",
317-
"Content-Type: ",
305+
buffContent = malloc(sizeof("Content-Type: ") +
306+
strlen(listEntry->data));
307+
sprintf(buffContent, "Content-Type: %s",
318308
(char *) listEntry->data);
319-
headers = curl_slist_append(headers,
320-
buffContent);
309+
headers = curl_slist_append(headers, buffContent);
321310
free(buffContent);
322311
buffContent = NULL;
323312
}
@@ -594,8 +583,8 @@ void apiClient_invoke(apiClient_t *apiClient,
594583

595584
size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
596585
size_t size_this_time = nmemb * size;
597-
apiClient_t *apiClient = (apiClient_t *)userp;
598-
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
586+
apiClient_t *apiClient = userp;
587+
apiClient->dataReceived = realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
599588
memcpy((char *)apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
600589
apiClient->dataReceivedLen += size_this_time;
601590
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void listEntry_free(listEntry_t *listEntry, void *additionalData) {
2020
}
2121

2222
void listEntry_printAsInt(listEntry_t *listEntry, void *additionalData) {
23-
printf("%i\n", *((int *) (listEntry->data)));
23+
printf("%i\n", *(int *)listEntry->data);
2424
}
2525

2626
list_t *list_createList() {
@@ -176,8 +176,8 @@ char* findStrInStrList(list_t *strList, const char *str)
176176

177177
listEntry_t* listEntry = NULL;
178178
list_ForEach(listEntry, strList) {
179-
if (strstr((char*)listEntry->data, str) != NULL) {
180-
return (char*)listEntry->data;
179+
if (strstr(listEntry->data, str) != NULL) {
180+
return listEntry->data;
181181
}
182182
}
183183

@@ -197,4 +197,4 @@ void clear_and_free_string_list(list_t *list)
197197
list_item = NULL;
198198
}
199199
list_freeList(list);
200-
}
200+
}

modules/openapi-generator/src/main/resources/C-libcurl/model-body.mustache

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ void {{classname}}_free({{classname}}_t *{{classname}}) {
402402
{{#isMap}}
403403
if ({{{classname}}}->{{{name}}}) {
404404
list_ForEach(listEntry, {{classname}}->{{name}}) {
405-
keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data;
405+
keyValuePair_t *localKeyValue = listEntry->data;
406406
free (localKeyValue->key);
407407
free (localKeyValue->value);
408408
keyValuePair_free(localKeyValue);
@@ -570,7 +570,7 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
570570
list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) {
571571
{{#items}}
572572
{{#isString}}
573-
if(cJSON_AddStringToObject({{{name}}}, "", (char*){{{name}}}ListEntry->data) == NULL)
573+
if(cJSON_AddStringToObject({{{name}}}, "", {{{name}}}ListEntry->data) == NULL)
574574
{
575575
goto fail;
576576
}
@@ -617,16 +617,16 @@ cJSON *{{classname}}_convertToJSON({{classname}}_t *{{classname}}) {
617617
listEntry_t *{{{name}}}ListEntry;
618618
if ({{{classname}}}->{{{name}}}) {
619619
list_ForEach({{{name}}}ListEntry, {{{classname}}}->{{{name}}}) {
620-
keyValuePair_t *localKeyValue = (keyValuePair_t*){{{name}}}ListEntry->data;
620+
keyValuePair_t *localKeyValue = {{{name}}}ListEntry->data;
621621
{{#items}}
622622
{{#isString}}
623-
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, (char*)localKeyValue->value) == NULL)
623+
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, localKeyValue->value) == NULL)
624624
{
625625
goto fail;
626626
}
627627
{{/isString}}
628628
{{#isByteArray}}
629-
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, (char*)localKeyValue->value) == NULL)
629+
if(cJSON_AddStringToObject(localMapObject, localKeyValue->key, localKeyValue->value) == NULL)
630630
{
631631
goto fail;
632632
}
@@ -848,7 +848,7 @@ fail:
848848
{
849849
goto end;
850850
}
851-
double *{{{name}}}_local_value = (double *)calloc(1, sizeof(double));
851+
double *{{{name}}}_local_value = calloc(1, sizeof(double));
852852
if(!{{{name}}}_local_value)
853853
{
854854
goto end;
@@ -1088,7 +1088,7 @@ end:
10881088
if ({{{name}}}List) {
10891089
listEntry_t *listEntry = NULL;
10901090
list_ForEach(listEntry, {{{name}}}List) {
1091-
keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data;
1091+
keyValuePair_t *localKeyValue = listEntry->data;
10921092
free(localKeyValue->key);
10931093
localKeyValue->key = NULL;
10941094
{{#items}}

samples/client/others/c/bearerAuth/api/DefaultAPI.c

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ DefaultAPI_privateGet(apiClient_t *apiClient)
2626
apiClient->response_code = 0;
2727

2828
// create the path
29-
long sizeOfPath = strlen("/private")+1;
30-
char *localVarPath = malloc(sizeOfPath);
31-
snprintf(localVarPath, sizeOfPath, "/private");
29+
char *localVarPath = strdup("/private");
3230

3331

3432

@@ -98,9 +96,7 @@ DefaultAPI_publicGet(apiClient_t *apiClient)
9896
apiClient->response_code = 0;
9997

10098
// create the path
101-
long sizeOfPath = strlen("/public")+1;
102-
char *localVarPath = malloc(sizeOfPath);
103-
snprintf(localVarPath, sizeOfPath, "/public");
99+
char *localVarPath = strdup("/public");
104100

105101

106102

@@ -170,9 +166,7 @@ DefaultAPI_usersGet(apiClient_t *apiClient)
170166
apiClient->response_code = 0;
171167

172168
// create the path
173-
long sizeOfPath = strlen("/users")+1;
174-
char *localVarPath = malloc(sizeOfPath);
175-
snprintf(localVarPath, sizeOfPath, "/users");
169+
char *localVarPath = strdup("/users");
176170

177171

178172

samples/client/others/c/bearerAuth/src/apiClient.c

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,11 @@ void sslConfig_free(sslConfig_t *sslConfig) {
8989
free(sslConfig);
9090
}
9191

92-
static void replaceSpaceWithPlus(char *stringToProcess) {
93-
for(int i = 0; i < strlen(stringToProcess); i++) {
94-
if(stringToProcess[i] == ' ') {
95-
stringToProcess[i] = '+';
92+
static void replaceSpaceWithPlus(char *str) {
93+
if (str) {
94+
for (; *str; str++) {
95+
if (*str == ' ')
96+
*str = '+';
9697
}
9798
}
9899
}
@@ -202,38 +203,26 @@ void apiClient_invoke(apiClient_t *apiClient,
202203

203204
if(headerType != NULL) {
204205
list_ForEach(listEntry, headerType) {
205-
if(strstr((char *) listEntry->data,
206-
"xml") == NULL)
206+
if(strstr(listEntry->data, "xml") == NULL)
207207
{
208-
buffHeader = malloc(strlen(
209-
"Accept: ") +
210-
strlen((char *)
211-
listEntry->
212-
data) + 1);
213-
sprintf(buffHeader, "%s%s", "Accept: ",
208+
buffHeader = malloc(sizeof("Accept: ") +
209+
strlen(listEntry->data));
210+
sprintf(buffHeader, "Accept: %s",
214211
(char *) listEntry->data);
215-
headers = curl_slist_append(headers,
216-
buffHeader);
212+
headers = curl_slist_append(headers, buffHeader);
217213
free(buffHeader);
218214
}
219215
}
220216
}
221217
if(contentType != NULL) {
222218
list_ForEach(listEntry, contentType) {
223-
if(strstr((char *) listEntry->data,
224-
"xml") == NULL)
219+
if(strstr(listEntry->data, "xml") == NULL)
225220
{
226-
buffContent =
227-
malloc(strlen(
228-
"Content-Type: ") + strlen(
229-
(char *)
230-
listEntry->data) +
231-
1);
232-
sprintf(buffContent, "%s%s",
233-
"Content-Type: ",
221+
buffContent = malloc(sizeof("Content-Type: ") +
222+
strlen(listEntry->data));
223+
sprintf(buffContent, "Content-Type: %s",
234224
(char *) listEntry->data);
235-
headers = curl_slist_append(headers,
236-
buffContent);
225+
headers = curl_slist_append(headers, buffContent);
237226
free(buffContent);
238227
buffContent = NULL;
239228
}
@@ -438,8 +427,8 @@ void apiClient_invoke(apiClient_t *apiClient,
438427

439428
size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
440429
size_t size_this_time = nmemb * size;
441-
apiClient_t *apiClient = (apiClient_t *)userp;
442-
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
430+
apiClient_t *apiClient = userp;
431+
apiClient->dataReceived = realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
443432
memcpy((char *)apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
444433
apiClient->dataReceivedLen += size_this_time;
445434
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1

samples/client/others/c/bearerAuth/src/list.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void listEntry_free(listEntry_t *listEntry, void *additionalData) {
2020
}
2121

2222
void listEntry_printAsInt(listEntry_t *listEntry, void *additionalData) {
23-
printf("%i\n", *((int *) (listEntry->data)));
23+
printf("%i\n", *(int *)listEntry->data);
2424
}
2525

2626
list_t *list_createList() {
@@ -176,8 +176,8 @@ char* findStrInStrList(list_t *strList, const char *str)
176176

177177
listEntry_t* listEntry = NULL;
178178
list_ForEach(listEntry, strList) {
179-
if (strstr((char*)listEntry->data, str) != NULL) {
180-
return (char*)listEntry->data;
179+
if (strstr(listEntry->data, str) != NULL) {
180+
return listEntry->data;
181181
}
182182
}
183183

@@ -197,4 +197,4 @@ void clear_and_free_string_list(list_t *list)
197197
list_item = NULL;
198198
}
199199
list_freeList(list);
200-
}
200+
}

0 commit comments

Comments
 (0)