Skip to content

Commit 9b1227d

Browse files
committed
[c] fix memory leaks: second round
1 parent 2468533 commit 9b1227d

17 files changed

Lines changed: 168 additions & 17 deletions

File tree

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

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create(
305305
{{/isBoolean}}
306306
{{/isPrimitiveType}}
307307
{{/vars}}
308-
return {{classname}}_create_internal (
308+
{{classname}}_t *result = {{classname}}_create_internal (
309309
{{#vars}}
310310
{{#isPrimitiveType}}
311311
{{#isNumeric}}
@@ -325,6 +325,19 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create(
325325
{{/isPrimitiveType}}
326326
{{/vars}}
327327
);
328+
if (!result) {
329+
{{#vars}}
330+
{{#isPrimitiveType}}
331+
{{#isNumeric}}
332+
free({{name}}_copy);
333+
{{/isNumeric}}
334+
{{#isBoolean}}
335+
free({{name}}_copy);
336+
{{/isBoolean}}
337+
{{/isPrimitiveType}}
338+
{{/vars}}
339+
}
340+
return result;
328341
}
329342

330343
void {{classname}}_free({{classname}}_t *{{classname}}) {
@@ -1091,6 +1104,10 @@ fail:
10911104
{{/vars}}
10921105
);
10931106

1107+
if (!{{classname}}_local_var) {
1108+
goto end;
1109+
}
1110+
10941111
return {{classname}}_local_var;
10951112
end:
10961113
{{#vars}}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ __attribute__((deprecated)) api_response_t *api_response_create(
3232
code_copy = malloc(sizeof(int));
3333
if (code_copy) *code_copy = *code;
3434
}
35-
return api_response_create_internal (
35+
api_response_t *result = api_response_create_internal (
3636
code_copy,
3737
type,
3838
message
3939
);
40+
if (!result) {
41+
free(code_copy);
42+
}
43+
return result;
4044
}
4145

4246
void api_response_free(api_response_t *api_response) {
@@ -153,6 +157,10 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
153157
message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL
154158
);
155159

160+
if (!api_response_local_var) {
161+
goto end;
162+
}
163+
156164
return api_response_local_var;
157165
end:
158166
if (code_local_var) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ __attribute__((deprecated)) category_t *category_create(
2929
id_copy = malloc(sizeof(long));
3030
if (id_copy) *id_copy = *id;
3131
}
32-
return category_create_internal (
32+
category_t *result = category_create_internal (
3333
id_copy,
3434
name
3535
);
36+
if (!result) {
37+
free(id_copy);
38+
}
39+
return result;
3640
}
3741

3842
void category_free(category_t *category) {
@@ -124,6 +128,10 @@ category_t *category_parseFromJSON(cJSON *categoryJSON){
124128
name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL
125129
);
126130

131+
if (!category_local_var) {
132+
goto end;
133+
}
134+
127135
return category_local_var;
128136
end:
129137
if (id_local_var) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ __attribute__((deprecated)) MappedModel_t *MappedModel_create(
2929
another_property_copy = malloc(sizeof(int));
3030
if (another_property_copy) *another_property_copy = *another_property;
3131
}
32-
return MappedModel_create_internal (
32+
MappedModel_t *result = MappedModel_create_internal (
3333
another_property_copy,
3434
uuid_property
3535
);
36+
if (!result) {
37+
free(another_property_copy);
38+
}
39+
return result;
3640
}
3741

3842
void MappedModel_free(MappedModel_t *MappedModel) {
@@ -124,6 +128,10 @@ MappedModel_t *MappedModel_parseFromJSON(cJSON *MappedModelJSON){
124128
uuid_property && !cJSON_IsNull(uuid_property) ? strdup(uuid_property->valuestring) : NULL
125129
);
126130

131+
if (!MappedModel_local_var) {
132+
goto end;
133+
}
134+
127135
return MappedModel_local_var;
128136
end:
129137
if (another_property_local_var) {

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@ __attribute__((deprecated)) model_with_set_propertes_t *model_with_set_propertes
2424
list_t *tag_set,
2525
list_t *string_set
2626
) {
27-
return model_with_set_propertes_create_internal (
27+
model_with_set_propertes_t *result = model_with_set_propertes_create_internal (
2828
tag_set,
2929
string_set
3030
);
31+
if (!result) {
32+
}
33+
return result;
3134
}
3235

3336
void model_with_set_propertes_free(model_with_set_propertes_t *model_with_set_propertes) {
@@ -165,6 +168,10 @@ model_with_set_propertes_t *model_with_set_propertes_parseFromJSON(cJSON *model_
165168
string_set ? string_setList : NULL
166169
);
167170

171+
if (!model_with_set_propertes_local_var) {
172+
goto end;
173+
}
174+
168175
return model_with_set_propertes_local_var;
169176
end:
170177
if (tag_setList) {

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,21 @@ __attribute__((deprecated)) order_t *order_create(
7373
complete_copy = malloc(sizeof(int));
7474
if (complete_copy) *complete_copy = *complete;
7575
}
76-
return order_create_internal (
76+
order_t *result = order_create_internal (
7777
id_copy,
7878
pet_id_copy,
7979
quantity_copy,
8080
ship_date,
8181
status,
8282
complete_copy
8383
);
84+
if (!result) {
85+
free(id_copy);
86+
free(pet_id_copy);
87+
free(quantity_copy);
88+
free(complete_copy);
89+
}
90+
return result;
8491
}
8592

8693
void order_free(order_t *order) {
@@ -298,6 +305,10 @@ order_t *order_parseFromJSON(cJSON *orderJSON){
298305
complete_local_var
299306
);
300307

308+
if (!order_local_var) {
309+
goto end;
310+
}
311+
301312
return order_local_var;
302313
end:
303314
if (id_local_var) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ __attribute__((deprecated)) pet_t *pet_create(
5858
id_copy = malloc(sizeof(long));
5959
if (id_copy) *id_copy = *id;
6060
}
61-
return pet_create_internal (
61+
pet_t *result = pet_create_internal (
6262
id_copy,
6363
category,
6464
name,
6565
photo_urls,
6666
tags,
6767
status
6868
);
69+
if (!result) {
70+
free(id_copy);
71+
}
72+
return result;
6973
}
7074

7175
void pet_free(pet_t *pet) {
@@ -324,6 +328,10 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
324328
status ? statusVariable : openapi_petstore_pet_STATUS_NULL
325329
);
326330

331+
if (!pet_local_var) {
332+
goto end;
333+
}
334+
327335
return pet_local_var;
328336
end:
329337
if (id_local_var) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ __attribute__((deprecated)) tag_t *tag_create(
2929
id_copy = malloc(sizeof(long));
3030
if (id_copy) *id_copy = *id;
3131
}
32-
return tag_create_internal (
32+
tag_t *result = tag_create_internal (
3333
id_copy,
3434
name
3535
);
36+
if (!result) {
37+
free(id_copy);
38+
}
39+
return result;
3640
}
3741

3842
void tag_free(tag_t *tag) {
@@ -124,6 +128,10 @@ tag_t *tag_parseFromJSON(cJSON *tagJSON){
124128
name && !cJSON_IsNull(name) ? strdup(name->valuestring) : NULL
125129
);
126130

131+
if (!tag_local_var) {
132+
goto end;
133+
}
134+
127135
return tag_local_var;
128136
end:
129137
if (id_local_var) {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ __attribute__((deprecated)) user_t *user_create(
5858
user_status_copy = malloc(sizeof(int));
5959
if (user_status_copy) *user_status_copy = *user_status;
6060
}
61-
return user_create_internal (
61+
user_t *result = user_create_internal (
6262
id_copy,
6363
username,
6464
first_name,
@@ -70,6 +70,11 @@ __attribute__((deprecated)) user_t *user_create(
7070
extra,
7171
preference
7272
);
73+
if (!result) {
74+
free(id_copy);
75+
free(user_status_copy);
76+
}
77+
return result;
7378
}
7479

7580
void user_free(user_t *user) {
@@ -399,6 +404,10 @@ user_t *user_parseFromJSON(cJSON *userJSON){
399404
preference ? preference_local_nonprim : 0
400405
);
401406

407+
if (!user_local_var) {
408+
goto end;
409+
}
410+
402411
return user_local_var;
403412
end:
404413
if (id_local_var) {

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ __attribute__((deprecated)) api_response_t *api_response_create(
3232
code_copy = malloc(sizeof(int));
3333
if (code_copy) *code_copy = *code;
3434
}
35-
return api_response_create_internal (
35+
api_response_t *result = api_response_create_internal (
3636
code_copy,
3737
type,
3838
message
3939
);
40+
if (!result) {
41+
free(code_copy);
42+
}
43+
return result;
4044
}
4145

4246
void api_response_free(api_response_t *api_response) {
@@ -153,6 +157,10 @@ api_response_t *api_response_parseFromJSON(cJSON *api_responseJSON){
153157
message && !cJSON_IsNull(message) ? strdup(message->valuestring) : NULL
154158
);
155159

160+
if (!api_response_local_var) {
161+
goto end;
162+
}
163+
156164
return api_response_local_var;
157165
end:
158166
if (code_local_var) {

0 commit comments

Comments
 (0)