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

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

* [C][Client] Free list or map memory when json parsing fails (part 2)

* Note for unsupported data type
This commit is contained in:
Hui Yu 2022-03-27 14:58:45 +08:00 committed by GitHub
parent 36453bcf88
commit 0a9429f1a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 116 additions and 10 deletions

View File

@ -553,6 +553,18 @@ fail:
{{classname}}_t *{{classname}}_local_var = NULL; {{classname}}_t *{{classname}}_local_var = NULL;
{{#vars}} {{#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}} {{^isContainer}}
{{^isPrimitiveType}} {{^isPrimitiveType}}
{{#isModel}} {{#isModel}}
@ -693,9 +705,8 @@ fail:
{{#isContainer}} {{#isContainer}}
{{#isArray}} {{#isArray}}
{{#isPrimitiveType}} {{#isPrimitiveType}}
list_t *{{{name}}}List;
{{^required}}if ({{{name}}}) { {{/required}} {{^required}}if ({{{name}}}) { {{/required}}
cJSON *{{{name}}}_local; cJSON *{{{name}}}_local = NULL;
if(!cJSON_IsArray({{{name}}})) { if(!cJSON_IsArray({{{name}}})) {
goto end;//primitive container goto end;//primitive container
} }
@ -735,9 +746,8 @@ fail:
} }
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{^isPrimitiveType}} {{^isPrimitiveType}}
list_t *{{{name}}}List;
{{^required}}if ({{{name}}}) { {{/required}} {{^required}}if ({{{name}}}) { {{/required}}
cJSON *{{{name}}}_local_nonprimitive; cJSON *{{{name}}}_local_nonprimitive = NULL;
if(!cJSON_IsArray({{{name}}})){ if(!cJSON_IsArray({{{name}}})){
goto end; //nonprimitive container goto end; //nonprimitive container
} }
@ -756,9 +766,9 @@ fail:
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{/isArray}} {{/isArray}}
{{#isMap}} {{#isMap}}
list_t *{{{name}}}List;
{{^required}}if ({{{name}}}) { {{/required}} {{^required}}if ({{{name}}}) { {{/required}}
cJSON *{{{name}}}_local_map; {{#isPrimitiveType}}
cJSON *{{{name}}}_local_map = NULL;
if(!cJSON_IsObject({{{name}}})) { if(!cJSON_IsObject({{{name}}})) {
goto end;//primitive map container goto end;//primitive map container
} }
@ -799,6 +809,12 @@ fail:
{{/items}} {{/items}}
list_addElement({{{name}}}List , localMapKeyPair); list_addElement({{{name}}}List , localMapKeyPair);
} }
{{/isPrimitiveType}}
{{^isPrimitiveType}}
// The data type of the elements in {{{classname}}}->{{{name}}} is currently not supported.
{{/isPrimitiveType}}
{{/isMap}} {{/isMap}}
{{/isContainer}} {{/isContainer}}
{{^required}} {{^required}}
@ -904,6 +920,74 @@ end:
{{/isModel}} {{/isModel}}
{{/isPrimitiveType}} {{/isPrimitiveType}}
{{/isContainer}} {{/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);
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}} {{/vars}}
return NULL; return NULL;

View File

@ -171,6 +171,12 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
// define the local variable for pet->category // define the local variable for pet->category
category_t *category_local_nonprim = NULL; 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 // pet->id
cJSON *id = cJSON_GetObjectItemCaseSensitive(petJSON, "id"); cJSON *id = cJSON_GetObjectItemCaseSensitive(petJSON, "id");
if (id) { if (id) {
@ -204,9 +210,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
goto end; goto end;
} }
list_t *photo_urlsList;
cJSON *photo_urls_local; cJSON *photo_urls_local = NULL;
if(!cJSON_IsArray(photo_urls)) { if(!cJSON_IsArray(photo_urls)) {
goto end;//primitive container goto end;//primitive container
} }
@ -223,9 +228,8 @@ pet_t *pet_parseFromJSON(cJSON *petJSON){
// pet->tags // pet->tags
cJSON *tags = cJSON_GetObjectItemCaseSensitive(petJSON, "tags"); cJSON *tags = cJSON_GetObjectItemCaseSensitive(petJSON, "tags");
list_t *tagsList;
if (tags) { if (tags) {
cJSON *tags_local_nonprimitive; cJSON *tags_local_nonprimitive = NULL;
if(!cJSON_IsArray(tags)){ if(!cJSON_IsArray(tags)){
goto end; //nonprimitive container goto end; //nonprimitive container
} }
@ -270,6 +274,24 @@ end:
category_free(category_local_nonprim); category_free(category_local_nonprim);
category_local_nonprim = NULL; 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; return NULL;
} }