Skip to content

Commit 61bb901

Browse files
committed
[c] fix memory leaks: third round
1 parent 9b1227d commit 61bb901

18 files changed

Lines changed: 346 additions & 55 deletions

File tree

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

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -757,6 +757,14 @@ fail:
757757

758758
{{/isFreeFormObject}}
759759
{{/isModel}}
760+
{{#isUuid}}
761+
char *{{name}}_local_str = NULL;
762+
763+
{{/isUuid}}
764+
{{#isEmail}}
765+
char *{{name}}_local_str = NULL;
766+
767+
{{/isEmail}}
760768
{{/isPrimitiveType}}
761769
{{#isPrimitiveType}}
762770
{{#isNumeric}}
@@ -769,6 +777,24 @@ fail:
769777
{{datatype}} *{{name}}_local_var = NULL;
770778

771779
{{/isBoolean}}
780+
{{^isEnum}}
781+
{{#isString}}
782+
char *{{name}}_local_str = NULL;
783+
784+
{{/isString}}
785+
{{/isEnum}}
786+
{{#isByteArray}}
787+
char *{{name}}_local_str = NULL;
788+
789+
{{/isByteArray}}
790+
{{#isDate}}
791+
char *{{name}}_local_str = NULL;
792+
793+
{{/isDate}}
794+
{{#isDateTime}}
795+
char *{{name}}_local_str = NULL;
796+
797+
{{/isDateTime}}
772798
{{/isPrimitiveType}}
773799
{{/isContainer}}
774800
{{/vars}}
@@ -1030,6 +1056,35 @@ fail:
10301056

10311057
{{/vars}}
10321058

1059+
{{#vars}}
1060+
{{^isContainer}}
1061+
{{^isPrimitiveType}}
1062+
{{#isUuid}}
1063+
if ({{{name}}}) {{name}}_local_str = strdup({{{name}}}->valuestring);
1064+
{{/isUuid}}
1065+
{{#isEmail}}
1066+
if ({{{name}}}) {{name}}_local_str = strdup({{{name}}}->valuestring);
1067+
{{/isEmail}}
1068+
{{/isPrimitiveType}}
1069+
{{#isPrimitiveType}}
1070+
{{^isEnum}}
1071+
{{#isString}}
1072+
if ({{{name}}} && !cJSON_IsNull({{{name}}})) {{name}}_local_str = strdup({{{name}}}->valuestring);
1073+
{{/isString}}
1074+
{{/isEnum}}
1075+
{{#isByteArray}}
1076+
if ({{{name}}}) {{name}}_local_str = strdup({{{name}}}->valuestring);
1077+
{{/isByteArray}}
1078+
{{#isDate}}
1079+
if ({{{name}}}) {{name}}_local_str = strdup({{{name}}}->valuestring);
1080+
{{/isDate}}
1081+
{{#isDateTime}}
1082+
if ({{{name}}} && !cJSON_IsNull({{{name}}})) {{name}}_local_str = strdup({{{name}}}->valuestring);
1083+
{{/isDateTime}}
1084+
{{/isPrimitiveType}}
1085+
{{/isContainer}}
1086+
{{/vars}}
1087+
10331088
{{classname}}_local_var = {{classname}}_create_internal (
10341089
{{#vars}}
10351090
{{^isContainer}}
@@ -1048,10 +1103,10 @@ fail:
10481103
{{/isFreeFormObject}}
10491104
{{/isModel}}
10501105
{{#isUuid}}
1051-
{{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
1106+
{{name}}_local_str{{^-last}},{{/-last}}
10521107
{{/isUuid}}
10531108
{{#isEmail}}
1054-
{{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
1109+
{{name}}_local_str{{^-last}},{{/-last}}
10551110
{{/isEmail}}
10561111
{{#isFreeFormObject}}
10571112
{{^required}}{{{name}}} ? {{/required}}{{{name}}}_local_object{{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
@@ -1071,20 +1126,20 @@ fail:
10711126
{{/isEnum}}
10721127
{{^isEnum}}
10731128
{{#isString}}
1074-
{{^required}}{{{name}}} && !cJSON_IsNull({{{name}}}) ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
1129+
{{name}}_local_str{{^-last}},{{/-last}}
10751130
{{/isString}}
10761131
{{/isEnum}}
10771132
{{#isByteArray}}
1078-
{{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
1133+
{{name}}_local_str{{^-last}},{{/-last}}
10791134
{{/isByteArray}}
10801135
{{#isBinary}}
10811136
{{^required}}{{{name}}} ? {{/required}}decoded_str_{{{name}}}{{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
10821137
{{/isBinary}}
10831138
{{#isDate}}
1084-
{{^required}}{{{name}}} ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
1139+
{{name}}_local_str{{^-last}},{{/-last}}
10851140
{{/isDate}}
10861141
{{#isDateTime}}
1087-
{{^required}}{{{name}}} && !cJSON_IsNull({{{name}}}) ? {{/required}}strdup({{{name}}}->valuestring){{^required}} : NULL{{/required}}{{^-last}},{{/-last}}
1142+
{{name}}_local_str{{^-last}},{{/-last}}
10881143
{{/isDateTime}}
10891144
{{/isPrimitiveType}}
10901145
{{/isContainer}}
@@ -1131,6 +1186,18 @@ end:
11311186
}
11321187
{{/isFreeFormObject}}
11331188
{{/isModel}}
1189+
{{#isUuid}}
1190+
if ({{{name}}}_local_str) {
1191+
free({{{name}}}_local_str);
1192+
{{{name}}}_local_str = NULL;
1193+
}
1194+
{{/isUuid}}
1195+
{{#isEmail}}
1196+
if ({{{name}}}_local_str) {
1197+
free({{{name}}}_local_str);
1198+
{{{name}}}_local_str = NULL;
1199+
}
1200+
{{/isEmail}}
11341201
{{/isPrimitiveType}}
11351202
{{#isPrimitiveType}}
11361203
{{#isNumeric}}
@@ -1145,6 +1212,32 @@ end:
11451212
{{{name}}}_local_var = NULL;
11461213
}
11471214
{{/isBoolean}}
1215+
{{^isEnum}}
1216+
{{#isString}}
1217+
if ({{{name}}}_local_str) {
1218+
free({{{name}}}_local_str);
1219+
{{{name}}}_local_str = NULL;
1220+
}
1221+
{{/isString}}
1222+
{{/isEnum}}
1223+
{{#isByteArray}}
1224+
if ({{{name}}}_local_str) {
1225+
free({{{name}}}_local_str);
1226+
{{{name}}}_local_str = NULL;
1227+
}
1228+
{{/isByteArray}}
1229+
{{#isDate}}
1230+
if ({{{name}}}_local_str) {
1231+
free({{{name}}}_local_str);
1232+
{{{name}}}_local_str = NULL;
1233+
}
1234+
{{/isDate}}
1235+
{{#isDateTime}}
1236+
if ({{{name}}}_local_str) {
1237+
free({{{name}}}_local_str);
1238+
{{{name}}}_local_str = NULL;
1239+
}
1240+
{{/isDateTime}}
11481241
{{/isPrimitiveType}}
11491242
{{/isContainer}}
11501243
{{#isContainer}}

samples/client/petstore/c-useJsonUnformatted/model/api_response.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
108108
// define the local variable for api_response->code
109109
int *code_local_var = NULL;
110110

111+
char *type_local_str = NULL;
112+
113+
char *message_local_str = NULL;
114+
111115
// api_response->code
112116
cJSON *code = cJSON_GetObjectItemCaseSensitive(api_responseJSON, "code");
113117
if (cJSON_IsNull(code)) {
@@ -151,10 +155,13 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
151155
}
152156

153157

158+
if (type && !cJSON_IsNull(type)) type_local_str = strdup(type->valuestring);
159+
if (message && !cJSON_IsNull(message)) message_local_str = strdup(message->valuestring);
160+
154161
api_response_local_var = api_response_create_internal (
155162
code_local_var,
156-
type && !cJSON_IsNull(type) ? strdup(type->valuestring) : NULL,
157-
message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL
163+
type_local_str,
164+
message_local_str
158165
);
159166

160167
if (!api_response_local_var) {
@@ -167,6 +174,14 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
167174
free(code_local_var);
168175
code_local_var = NULL;
169176
}
177+
if (type_local_str) {
178+
free(type_local_str);
179+
type_local_str = NULL;
180+
}
181+
if (message_local_str) {
182+
free(message_local_str);
183+
message_local_str = NULL;
184+
}
170185
return NULL;
171186

172187
}

samples/client/petstore/c-useJsonUnformatted/model/category.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){
9292
// define the local variable for category->id
9393
long *id_local_var = NULL;
9494

95+
char *name_local_str = NULL;
96+
9597
// category->id
9698
cJSON *id = cJSON_GetObjectItemCaseSensitive(categoryJSON, "id");
9799
if (cJSON_IsNull(id)) {
@@ -123,9 +125,11 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){
123125
}
124126

125127

128+
if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring);
129+
126130
category_local_var = category_create_internal (
127131
id_local_var,
128-
name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL
132+
name_local_str
129133
);
130134

131135
if (!category_local_var) {
@@ -138,6 +142,10 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){
138142
free(id_local_var);
139143
id_local_var = NULL;
140144
}
145+
if (name_local_str) {
146+
free(name_local_str);
147+
name_local_str = NULL;
148+
}
141149
return NULL;
142150

143151
}

samples/client/petstore/c-useJsonUnformatted/model/mapped_model.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){
9292
// define the local variable for MappedModel->another_property
9393
int *another_property_local_var = NULL;
9494

95+
char *uuid_property_local_str = NULL;
96+
9597
// MappedModel->another_property
9698
cJSON *another_property = cJSON_GetObjectItemCaseSensitive(MappedModelJSON, "another_property");
9799
if (cJSON_IsNull(another_property)) {
@@ -123,9 +125,11 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){
123125
}
124126

125127

128+
if (uuid_property && !cJSON_IsNull(uuid_property)) uuid_property_local_str = strdup(uuid_property->valuestring);
129+
126130
MappedModel_local_var = MappedModel_create_internal (
127131
another_property_local_var,
128-
uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL
132+
uuid_property_local_str
129133
);
130134

131135
if (!MappedModel_local_var) {
@@ -138,6 +142,10 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){
138142
free(another_property_local_var);
139143
another_property_local_var = NULL;
140144
}
145+
if (uuid_property_local_str) {
146+
free(uuid_property_local_str);
147+
uuid_property_local_str = NULL;
148+
}
141149
return NULL;
142150

143151
}

samples/client/petstore/c-useJsonUnformatted/model/model_with_set_propertes.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_
163163
}
164164

165165

166+
166167
model_with_set_propertes_local_var = model_with_set_propertes_create_internal (
167168
tag_set ? tag_setList : NULL,
168169
string_set ? string_setList : NULL

samples/client/petstore/c-useJsonUnformatted/model/order.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
194194
// define the local variable for order->quantity
195195
int *quantity_local_var = NULL;
196196

197+
char *ship_date_local_str = NULL;
198+
197199
// define the local variable for order->complete
198200
int *complete_local_var = NULL;
199201

@@ -296,11 +298,13 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
296298
}
297299

298300

301+
if (ship_date && !cJSON_IsNull(ship_date)) ship_date_local_str = strdup(ship_date->valuestring);
302+
299303
order_local_var = order_create_internal (
300304
id_local_var,
301305
pet_id_local_var,
302306
quantity_local_var,
303-
ship_date && !cJSON_IsNull(ship_date) ? strdup(ship_date->valuestring) : NULL,
307+
ship_date_local_str,
304308
status ? statusVariable : openapi_petstore_order_STATUS_NULL,
305309
complete_local_var
306310
);
@@ -323,6 +327,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
323327
free(quantity_local_var);
324328
quantity_local_var = NULL;
325329
}
330+
if (ship_date_local_str) {
331+
free(ship_date_local_str);
332+
ship_date_local_str = NULL;
333+
}
326334
if (complete_local_var) {
327335
free(complete_local_var);
328336
complete_local_var = NULL;

samples/client/petstore/c-useJsonUnformatted/model/pet.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
207207
// define the local variable for pet->category
208208
category_t *category_local_nonprim = NULL;
209209

210+
char *name_local_str = NULL;
211+
210212
// define the local list for pet->photo_urls
211213
list_t *photo_urlsList = NULL;
212214

@@ -319,10 +321,12 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
319321
}
320322

321323

324+
if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring);
325+
322326
pet_local_var = pet_create_internal (
323327
id_local_var,
324328
category ? category_local_nonprim : NULL,
325-
strdup(name->valuestring),
329+
name_local_str,
326330
photo_urlsList,
327331
tags ? tagsList : NULL,
328332
status ? statusVariable : openapi_petstore_pet_STATUS_NULL
@@ -342,6 +346,10 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
342346
category_free(category_local_nonprim);
343347
category_local_nonprim = NULL;
344348
}
349+
if (name_local_str) {
350+
free(name_local_str);
351+
name_local_str = NULL;
352+
}
345353
if (photo_urlsList) {
346354
listEntry_t *listEntry = NULL;
347355
list_ForEach(listEntry, photo_urlsList) {

samples/client/petstore/c-useJsonUnformatted/model/tag.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){
9292
// define the local variable for tag->id
9393
long *id_local_var = NULL;
9494

95+
char *name_local_str = NULL;
96+
9597
// tag->id
9698
cJSON *id = cJSON_GetObjectItemCaseSensitive(tagJSON, "id");
9799
if (cJSON_IsNull(id)) {
@@ -123,9 +125,11 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){
123125
}
124126

125127

128+
if (name && !cJSON_IsNull(name)) name_local_str = strdup(name->valuestring);
129+
126130
tag_local_var = tag_create_internal (
127131
id_local_var,
128-
name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL
132+
name_local_str
129133
);
130134

131135
if (!tag_local_var) {
@@ -138,6 +142,10 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){
138142
free(id_local_var);
139143
id_local_var = NULL;
140144
}
145+
if (name_local_str) {
146+
free(name_local_str);
147+
name_local_str = NULL;
148+
}
141149
return NULL;
142150

143151
}

0 commit comments

Comments
 (0)