Skip to content

Commit 2468533

Browse files
committed
[c] fix memory leaks
1 parent ca2d452 commit 2468533

17 files changed

Lines changed: 204 additions & 266 deletions

File tree

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

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -202,45 +202,10 @@ static {{classname}}_t *{{classname}}_create_internal(
202202
return NULL;
203203
}
204204
memset({{classname}}_local_var, 0, sizeof({{classname}}_t));
205+
{{classname}}_local_var->_library_owned = 1;
205206
{{#vars}}
206-
{{^isContainer}}
207-
{{#isPrimitiveType}}
208-
{{#isNumeric}}
209-
if ({{{name}}}) {
210-
{{classname}}_local_var->{{{name}}} = malloc(sizeof({{datatype}}));
211-
if (!{{classname}}_local_var->{{{name}}}) {
212-
{{classname}}_free({{classname}}_local_var);
213-
return NULL;
214-
}
215-
*{{classname}}_local_var->{{{name}}} = *{{{name}}};
216-
}
217-
{{/isNumeric}}
218-
{{#isBoolean}}
219-
if ({{{name}}}) {
220-
{{classname}}_local_var->{{{name}}} = malloc(sizeof({{datatype}}));
221-
if (!{{classname}}_local_var->{{{name}}}) {
222-
{{classname}}_free({{classname}}_local_var);
223-
return NULL;
224-
}
225-
*{{classname}}_local_var->{{{name}}} = *{{{name}}};
226-
}
227-
{{/isBoolean}}
228-
{{^isNumeric}}
229-
{{^isBoolean}}
230-
{{classname}}_local_var->{{{name}}} = {{{name}}};
231-
{{/isBoolean}}
232-
{{/isNumeric}}
233-
{{/isPrimitiveType}}
234-
{{^isPrimitiveType}}
235207
{{classname}}_local_var->{{{name}}} = {{{name}}};
236-
{{/isPrimitiveType}}
237-
{{/isContainer}}
238-
{{#isContainer}}
239-
{{classname}}_local_var->{{{name}}} = {{{name}}};
240-
{{/isContainer}}
241208
{{/vars}}
242-
243-
{{classname}}_local_var->_library_owned = 1;
244209
return {{classname}}_local_var;
245210
}
246211

@@ -322,9 +287,42 @@ __attribute__((deprecated)) {{classname}}_t *{{classname}}_create(
322287
{{/isContainer}}
323288
{{/vars}}
324289
) {
290+
{{#vars}}
291+
{{#isPrimitiveType}}
292+
{{#isNumeric}}
293+
{{datatype}} *{{name}}_copy = NULL;
294+
if ({{{name}}}) {
295+
{{name}}_copy = malloc(sizeof({{datatype}}));
296+
if ({{name}}_copy) *{{name}}_copy = *{{{name}}};
297+
}
298+
{{/isNumeric}}
299+
{{#isBoolean}}
300+
{{datatype}} *{{name}}_copy = NULL;
301+
if ({{{name}}}) {
302+
{{name}}_copy = malloc(sizeof({{datatype}}));
303+
if ({{name}}_copy) *{{name}}_copy = *{{{name}}};
304+
}
305+
{{/isBoolean}}
306+
{{/isPrimitiveType}}
307+
{{/vars}}
325308
return {{classname}}_create_internal (
326309
{{#vars}}
310+
{{#isPrimitiveType}}
311+
{{#isNumeric}}
312+
{{name}}_copy{{^-last}},{{/-last}}
313+
{{/isNumeric}}
314+
{{#isBoolean}}
315+
{{name}}_copy{{^-last}},{{/-last}}
316+
{{/isBoolean}}
317+
{{^isNumeric}}
318+
{{^isBoolean}}
327319
{{name}}{{^-last}},{{/-last}}
320+
{{/isBoolean}}
321+
{{/isNumeric}}
322+
{{/isPrimitiveType}}
323+
{{^isPrimitiveType}}
324+
{{name}}{{^-last}},{{/-last}}
325+
{{/isPrimitiveType}}
328326
{{/vars}}
329327
);
330328
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,10 @@ static api_response_t *api_response_create_internal(
1515
return NULL;
1616
}
1717
memset(api_response_local_var, 0, sizeof(api_response_t));
18-
if (code) {
19-
api_response_local_var->code = malloc(sizeof(int));
20-
if (!api_response_local_var->code) {
21-
api_response_free(api_response_local_var);
22-
return NULL;
23-
}
24-
*api_response_local_var->code = *code;
25-
}
18+
api_response_local_var->_library_owned = 1;
19+
api_response_local_var->code = code;
2620
api_response_local_var->type = type;
2721
api_response_local_var->message = message;
28-
29-
api_response_local_var->_library_owned = 1;
3022
return api_response_local_var;
3123
}
3224

@@ -35,8 +27,13 @@ __attribute__((deprecated)) api_response_t *api_response_create(
3527
char *type,
3628
char *message
3729
) {
30+
int *code_copy = NULL;
31+
if (code) {
32+
code_copy = malloc(sizeof(int));
33+
if (code_copy) *code_copy = *code;
34+
}
3835
return api_response_create_internal (
39-
code,
36+
code_copy,
4037
type,
4138
message
4239
);

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,23 @@ static category_t *category_create_internal(
1414
return NULL;
1515
}
1616
memset(category_local_var, 0, sizeof(category_t));
17-
if (id) {
18-
category_local_var->id = malloc(sizeof(long));
19-
if (!category_local_var->id) {
20-
category_free(category_local_var);
21-
return NULL;
22-
}
23-
*category_local_var->id = *id;
24-
}
25-
category_local_var->name = name;
26-
2717
category_local_var->_library_owned = 1;
18+
category_local_var->id = id;
19+
category_local_var->name = name;
2820
return category_local_var;
2921
}
3022

3123
__attribute__((deprecated)) category_t *category_create(
3224
long *id,
3325
char *name
3426
) {
27+
long *id_copy = NULL;
28+
if (id) {
29+
id_copy = malloc(sizeof(long));
30+
if (id_copy) *id_copy = *id;
31+
}
3532
return category_create_internal (
36-
id,
33+
id_copy,
3734
name
3835
);
3936
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,23 @@ static MappedModel_t *MappedModel_create_internal(
1414
return NULL;
1515
}
1616
memset(MappedModel_local_var, 0, sizeof(MappedModel_t));
17-
if (another_property) {
18-
MappedModel_local_var->another_property = malloc(sizeof(int));
19-
if (!MappedModel_local_var->another_property) {
20-
MappedModel_free(MappedModel_local_var);
21-
return NULL;
22-
}
23-
*MappedModel_local_var->another_property = *another_property;
24-
}
25-
MappedModel_local_var->uuid_property = uuid_property;
26-
2717
MappedModel_local_var->_library_owned = 1;
18+
MappedModel_local_var->another_property = another_property;
19+
MappedModel_local_var->uuid_property = uuid_property;
2820
return MappedModel_local_var;
2921
}
3022

3123
__attribute__((deprecated)) MappedModel_t *MappedModel_create(
3224
int *another_property,
3325
char *uuid_property
3426
) {
27+
int *another_property_copy = NULL;
28+
if (another_property) {
29+
another_property_copy = malloc(sizeof(int));
30+
if (another_property_copy) *another_property_copy = *another_property;
31+
}
3532
return MappedModel_create_internal (
36-
another_property,
33+
another_property_copy,
3734
uuid_property
3835
);
3936
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ static model_with_set_propertes_t *model_with_set_propertes_create_internal(
1414
return NULL;
1515
}
1616
memset(model_with_set_propertes_local_var, 0, sizeof(model_with_set_propertes_t));
17+
model_with_set_propertes_local_var->_library_owned = 1;
1718
model_with_set_propertes_local_var->tag_set = tag_set;
1819
model_with_set_propertes_local_var->string_set = string_set;
19-
20-
model_with_set_propertes_local_var->_library_owned = 1;
2120
return model_with_set_propertes_local_var;
2221
}
2322

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

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,13 @@ static order_t *order_create_internal(
3535
return NULL;
3636
}
3737
memset(order_local_var, 0, sizeof(order_t));
38-
if (id) {
39-
order_local_var->id = malloc(sizeof(long));
40-
if (!order_local_var->id) {
41-
order_free(order_local_var);
42-
return NULL;
43-
}
44-
*order_local_var->id = *id;
45-
}
46-
if (pet_id) {
47-
order_local_var->pet_id = malloc(sizeof(long));
48-
if (!order_local_var->pet_id) {
49-
order_free(order_local_var);
50-
return NULL;
51-
}
52-
*order_local_var->pet_id = *pet_id;
53-
}
54-
if (quantity) {
55-
order_local_var->quantity = malloc(sizeof(int));
56-
if (!order_local_var->quantity) {
57-
order_free(order_local_var);
58-
return NULL;
59-
}
60-
*order_local_var->quantity = *quantity;
61-
}
38+
order_local_var->_library_owned = 1;
39+
order_local_var->id = id;
40+
order_local_var->pet_id = pet_id;
41+
order_local_var->quantity = quantity;
6242
order_local_var->ship_date = ship_date;
6343
order_local_var->status = status;
64-
if (complete) {
65-
order_local_var->complete = malloc(sizeof(int));
66-
if (!order_local_var->complete) {
67-
order_free(order_local_var);
68-
return NULL;
69-
}
70-
*order_local_var->complete = *complete;
71-
}
72-
73-
order_local_var->_library_owned = 1;
44+
order_local_var->complete = complete;
7445
return order_local_var;
7546
}
7647

@@ -82,13 +53,33 @@ __attribute__((deprecated)) order_t *order_create(
8253
openapi_petstore_order_STATUS_e status,
8354
int *complete
8455
) {
56+
long *id_copy = NULL;
57+
if (id) {
58+
id_copy = malloc(sizeof(long));
59+
if (id_copy) *id_copy = *id;
60+
}
61+
long *pet_id_copy = NULL;
62+
if (pet_id) {
63+
pet_id_copy = malloc(sizeof(long));
64+
if (pet_id_copy) *pet_id_copy = *pet_id;
65+
}
66+
int *quantity_copy = NULL;
67+
if (quantity) {
68+
quantity_copy = malloc(sizeof(int));
69+
if (quantity_copy) *quantity_copy = *quantity;
70+
}
71+
int *complete_copy = NULL;
72+
if (complete) {
73+
complete_copy = malloc(sizeof(int));
74+
if (complete_copy) *complete_copy = *complete;
75+
}
8576
return order_create_internal (
86-
id,
87-
pet_id,
88-
quantity,
77+
id_copy,
78+
pet_id_copy,
79+
quantity_copy,
8980
ship_date,
9081
status,
91-
complete
82+
complete_copy
9283
);
9384
}
9485

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,13 @@ static pet_t *pet_create_internal(
3535
return NULL;
3636
}
3737
memset(pet_local_var, 0, sizeof(pet_t));
38-
if (id) {
39-
pet_local_var->id = malloc(sizeof(long));
40-
if (!pet_local_var->id) {
41-
pet_free(pet_local_var);
42-
return NULL;
43-
}
44-
*pet_local_var->id = *id;
45-
}
38+
pet_local_var->_library_owned = 1;
39+
pet_local_var->id = id;
4640
pet_local_var->category = category;
4741
pet_local_var->name = name;
4842
pet_local_var->photo_urls = photo_urls;
4943
pet_local_var->tags = tags;
5044
pet_local_var->status = status;
51-
52-
pet_local_var->_library_owned = 1;
5345
return pet_local_var;
5446
}
5547

@@ -61,8 +53,13 @@ __attribute__((deprecated)) pet_t *pet_create(
6153
list_t *tags,
6254
openapi_petstore_pet_STATUS_e status
6355
) {
56+
long *id_copy = NULL;
57+
if (id) {
58+
id_copy = malloc(sizeof(long));
59+
if (id_copy) *id_copy = *id;
60+
}
6461
return pet_create_internal (
65-
id,
62+
id_copy,
6663
category,
6764
name,
6865
photo_urls,

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,23 @@ static tag_t *tag_create_internal(
1414
return NULL;
1515
}
1616
memset(tag_local_var, 0, sizeof(tag_t));
17-
if (id) {
18-
tag_local_var->id = malloc(sizeof(long));
19-
if (!tag_local_var->id) {
20-
tag_free(tag_local_var);
21-
return NULL;
22-
}
23-
*tag_local_var->id = *id;
24-
}
25-
tag_local_var->name = name;
26-
2717
tag_local_var->_library_owned = 1;
18+
tag_local_var->id = id;
19+
tag_local_var->name = name;
2820
return tag_local_var;
2921
}
3022

3123
__attribute__((deprecated)) tag_t *tag_create(
3224
long *id,
3325
char *name
3426
) {
27+
long *id_copy = NULL;
28+
if (id) {
29+
id_copy = malloc(sizeof(long));
30+
if (id_copy) *id_copy = *id;
31+
}
3532
return tag_create_internal (
36-
id,
33+
id_copy,
3734
name
3835
);
3936
}

0 commit comments

Comments
 (0)