[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;
{{#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}}
@ -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
}
@ -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
}
@ -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
}
@ -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}}
@ -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);
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;

View File

@ -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) {
@ -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
}
@ -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
}
@ -270,6 +274,24 @@ end:
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;
}