Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C][Client] Free list or map memory when json parsing fails #11866

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,18 @@ fail:
{{classname}}_t *{{classname}}_local_var = NULL;

{{#vars}}
{{#isContainer}}
{{#isArray}}
// define the local list for {{{classname}}}->{{{name}}}
list_t *{{{name}}}List = NULL;

{{/isArray}}
{{#isMap}}
// define the local map for {{{classname}}}->{{{name}}}
list_t *{{{name}}}List = NULL;

{{/isMap}}
{{/isContainer}}
{{^isContainer}}
{{^isPrimitiveType}}
{{#isModel}}
Expand Down Expand Up @@ -693,9 +705,8 @@ fail:
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
list_t *{{{name}}}List;
{{^required}}if ({{{name}}}) { {{/required}}
cJSON *{{{name}}}_local;
cJSON *{{{name}}}_local = NULL;
if(!cJSON_IsArray({{{name}}})) {
goto end;//primitive container
}
Expand Down Expand Up @@ -735,9 +746,8 @@ fail:
}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
list_t *{{{name}}}List;
{{^required}}if ({{{name}}}) { {{/required}}
cJSON *{{{name}}}_local_nonprimitive;
cJSON *{{{name}}}_local_nonprimitive = NULL;
if(!cJSON_IsArray({{{name}}})){
goto end; //nonprimitive container
}
Expand All @@ -756,9 +766,9 @@ fail:
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
list_t *{{{name}}}List;
{{^required}}if ({{{name}}}) { {{/required}}
cJSON *{{{name}}}_local_map;
{{#isPrimitiveType}}
cJSON *{{{name}}}_local_map = NULL;
if(!cJSON_IsObject({{{name}}})) {
goto end;//primitive map container
}
Expand Down Expand Up @@ -799,6 +809,12 @@ fail:
{{/items}}
list_addElement({{{name}}}List , localMapKeyPair);
}
{{/isPrimitiveType}}
{{^isPrimitiveType}}

// The data type of the elements in {{{classname}}}->{{{name}}} is currently not supported.

{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{^required}}
Expand Down Expand Up @@ -904,6 +920,74 @@ end:
{{/isModel}}
{{/isPrimitiveType}}
{{/isContainer}}
{{#isContainer}}
{{#isArray}}
{{#isPrimitiveType}}
if ({{{name}}}List) {
{{#items}}
{{#isString}}
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, {{{name}}}List) {
free(listEntry->data);
listEntry->data = NULL;
}
{{/isString}}
{{#isNumeric}}
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, {{{name}}}List) {
free(listEntry->data);
listEntry->data = NULL;
}
{{/isNumeric}}
{{/items}}
list_freeList({{{name}}}List);
{{{name}}}List = NULL;
}
{{/isPrimitiveType}}
{{^isPrimitiveType}}
if ({{{name}}}List) {
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, {{{name}}}List) {
{{complexType}}_free(listEntry->data);
listEntry->data = NULL;
}
list_freeList({{{name}}}List);
{{{name}}}List = NULL;
}
{{/isPrimitiveType}}
{{/isArray}}
{{#isMap}}
{{#isPrimitiveType}}
if ({{{name}}}List) {
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, {{{name}}}List) {
keyValuePair_t *localKeyValue = (keyValuePair_t*) listEntry->data;
free(localKeyValue->key);
localKeyValue->key = NULL;
{{#items}}
{{#isString}}
free(localKeyValue->value);
localKeyValue->value = NULL;
{{/isString}}
{{#isByteArray}}
free(localKeyValue->value);

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I'll deal by data type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Can you take another look ?

localKeyValue->value = NULL;
{{/isByteArray}}
{{/items}}
keyValuePair_free(localKeyValue);
localKeyValue = NULL;
}
list_freeList({{{name}}}List);
{{{name}}}List = NULL;
}
{{/isPrimitiveType}}
{{^isPrimitiveType}}

// The data type of the elements in {{{classname}}}->{{{name}}} is currently not supported.

{{/isPrimitiveType}}
{{/isMap}}
{{/isContainer}}
{{/vars}}
return NULL;

Expand Down
30 changes: 26 additions & 4 deletions samples/client/petstore/c/model/pet.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
// define the local variable for pet->category
category_t *category_local_nonprim = NULL;

// define the local list for pet->photo_urls
list_t *photo_urlsList = NULL;

// define the local list for pet->tags
list_t *tagsList = NULL;

// pet->id
cJSON *id = cJSON_GetObjectItemCaseSensitive(petJSON, "id");
if (id) {
Expand Down Expand Up @@ -204,9 +210,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
goto end;
}

list_t *photo_urlsList;

cJSON *photo_urls_local;
cJSON *photo_urls_local = NULL;
if(!cJSON_IsArray(photo_urls)) {
goto end;//primitive container
}
Expand All @@ -223,9 +228,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){

// pet->tags
cJSON *tags = cJSON_GetObjectItemCaseSensitive(petJSON, "tags");
list_t *tagsList;
if (tags) {
cJSON *tags_local_nonprimitive;
cJSON *tags_local_nonprimitive = NULL;
if(!cJSON_IsArray(tags)){
goto end; //nonprimitive container
}
Expand Down Expand Up @@ -270,6 +274,24 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
category_free(category_local_nonprim);
category_local_nonprim = NULL;
}
if (photo_urlsList) {
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, photo_urlsList) {
free(listEntry->data);
listEntry->data = NULL;
}
list_freeList(photo_urlsList);
photo_urlsList = NULL;
}
if (tagsList) {
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, tagsList) {
tag_free(listEntry->data);
listEntry->data = NULL;
}
list_freeList(tagsList);
tagsList = NULL;
}
return NULL;

}