Bug Summary

File:model/pet.c
Warning:line 256, column 19
Potential memory leak

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name pet.c -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib/llvm-9/lib/clang/9.0.0 -D openapi_petstore_EXPORTS -I /usr/local/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-9/lib/clang/9.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /home/wing328/Code/openapi-generator/samples/client/petstore/c -ferror-limit 19 -fmessage-length 0 -fvisibility default -fobjc-runtime=gcc -fdiagnostics-show-option -analyzer-output=html -faddrsig -o /tmp/scan-build-2020-12-16-230944-1865-1 -x c /home/wing328/Code/openapi-generator/samples/client/petstore/c/model/pet.c
1#include <stdlib.h>
2#include <string.h>
3#include <stdio.h>
4#include "pet.h"
5
6
7char* statuspet_ToString(openapi_petstore_pet_STATUS_e status) {
8 char* statusArray[] = { "NULL", "available", "pending", "sold" };
9 return statusArray[status];
10}
11
12openapi_petstore_pet_STATUS_e statuspet_FromString(char* status){
13 int stringToReturn = 0;
14 char *statusArray[] = { "NULL", "available", "pending", "sold" };
15 size_t sizeofArray = sizeof(statusArray) / sizeof(statusArray[0]);
16 while(stringToReturn < sizeofArray) {
17 if(strcmp(status, statusArray[stringToReturn]) == 0) {
18 return stringToReturn;
19 }
20 stringToReturn++;
21 }
22 return 0;
23}
24
25pet_t *pet_create(
26 long id,
27 category_t *category,
28 char *name,
29 list_t *photo_urls,
30 list_t *tags,
31 openapi_petstore_pet_STATUS_e status
32 ) {
33 pet_t *pet_local_var = malloc(sizeof(pet_t));
34 if (!pet_local_var) {
35 return NULL((void*)0);
36 }
37 pet_local_var->id = id;
38 pet_local_var->category = category;
39 pet_local_var->name = name;
40 pet_local_var->photo_urls = photo_urls;
41 pet_local_var->tags = tags;
42 pet_local_var->status = status;
43
44 return pet_local_var;
45}
46
47
48void pet_free(pet_t *pet) {
49 if(NULL((void*)0) == pet){
50 return ;
51 }
52 listEntry_t *listEntry;
53 if (pet->category) {
54 category_free(pet->category);
55 pet->category = NULL((void*)0);
56 }
57 if (pet->name) {
58 free(pet->name);
59 pet->name = NULL((void*)0);
60 }
61 if (pet->photo_urls) {
62 list_ForEach(listEntry, pet->photo_urls)for(listEntry = (pet->photo_urls != ((void*)0)) ? (pet->
photo_urls)->firstEntry : ((void*)0); listEntry != ((void*
)0); listEntry = listEntry->nextListEntry)
{
63 free(listEntry->data);
64 }
65 list_free(pet->photo_urls);
66 pet->photo_urls = NULL((void*)0);
67 }
68 if (pet->tags) {
69 list_ForEach(listEntry, pet->tags)for(listEntry = (pet->tags != ((void*)0)) ? (pet->tags)
->firstEntry : ((void*)0); listEntry != ((void*)0); listEntry
= listEntry->nextListEntry)
{
70 tag_free(listEntry->data);
71 }
72 list_free(pet->tags);
73 pet->tags = NULL((void*)0);
74 }
75 free(pet);
76}
77
78cJSON *pet_convertToJSON(pet_t *pet) {
79 cJSON *item = cJSON_CreateObject();
80
81 // pet->id
82 if(pet->id) {
83 if(cJSON_AddNumberToObject(item, "id", pet->id) == NULL((void*)0)) {
84 goto fail; //Numeric
85 }
86 }
87
88
89 // pet->category
90 if(pet->category) {
91 cJSON *category_local_JSON = category_convertToJSON(pet->category);
92 if(category_local_JSON == NULL((void*)0)) {
93 goto fail; //model
94 }
95 cJSON_AddItemToObject(item, "category", category_local_JSON);
96 if(item->child == NULL((void*)0)) {
97 goto fail;
98 }
99 }
100
101
102 // pet->name
103 if (!pet->name) {
104 goto fail;
105 }
106
107 if(cJSON_AddStringToObject(item, "name", pet->name) == NULL((void*)0)) {
108 goto fail; //String
109 }
110
111
112 // pet->photo_urls
113 if (!pet->photo_urls) {
114 goto fail;
115 }
116
117 cJSON *photo_urls = cJSON_AddArrayToObject(item, "photoUrls");
118 if(photo_urls == NULL((void*)0)) {
119 goto fail; //primitive container
120 }
121
122 listEntry_t *photo_urlsListEntry;
123 list_ForEach(photo_urlsListEntry, pet->photo_urls)for(photo_urlsListEntry = (pet->photo_urls != ((void*)0)) ?
(pet->photo_urls)->firstEntry : ((void*)0); photo_urlsListEntry
!= ((void*)0); photo_urlsListEntry = photo_urlsListEntry->
nextListEntry)
{
124 if(cJSON_AddStringToObject(photo_urls, "", (char*)photo_urlsListEntry->data) == NULL((void*)0))
125 {
126 goto fail;
127 }
128 }
129
130
131 // pet->tags
132 if(pet->tags) {
133 cJSON *tags = cJSON_AddArrayToObject(item, "tags");
134 if(tags == NULL((void*)0)) {
135 goto fail; //nonprimitive container
136 }
137
138 listEntry_t *tagsListEntry;
139 if (pet->tags) {
140 list_ForEach(tagsListEntry, pet->tags)for(tagsListEntry = (pet->tags != ((void*)0)) ? (pet->tags
)->firstEntry : ((void*)0); tagsListEntry != ((void*)0); tagsListEntry
= tagsListEntry->nextListEntry)
{
141 cJSON *itemLocal = tag_convertToJSON(tagsListEntry->data);
142 if(itemLocal == NULL((void*)0)) {
143 goto fail;
144 }
145 cJSON_AddItemToArray(tags, itemLocal);
146 }
147 }
148 }
149
150
151 // pet->status
152
153 if(cJSON_AddStringToObject(item, "status", statuspet_ToString(pet->status)) == NULL((void*)0))
154 {
155 goto fail; //Enum
156 }
157
158
159 return item;
160fail:
161 if (item) {
162 cJSON_Delete(item);
163 }
164 return NULL((void*)0);
165}
166
167pet_t *pet_parseFromJSON(cJSON *petJSON){
168
169 pet_t *pet_local_var = NULL((void*)0);
170
171 // pet->id
172 cJSON *id = cJSON_GetObjectItemCaseSensitive(petJSON, "id");
173 if (id) {
1
Assuming 'id' is null
2
Taking false branch
174 if(!cJSON_IsNumber(id))
175 {
176 goto end; //Numeric
177 }
178 }
179
180 // pet->category
181 cJSON *category = cJSON_GetObjectItemCaseSensitive(petJSON, "category");
182 category_t *category_local_nonprim = NULL((void*)0);
183 if (category) {
3
Assuming 'category' is null
4
Taking false branch
184 category_local_nonprim = category_parseFromJSON(category); //nonprimitive
185 }
186
187 // pet->name
188 cJSON *name = cJSON_GetObjectItemCaseSensitive(petJSON, "name");
189 if (!name) {
5
Assuming 'name' is non-null
6
Taking false branch
190 goto end;
191 }
192
193
194 if(!cJSON_IsString(name))
7
Assuming the condition is false
8
Taking false branch
195 {
196 goto end; //String
197 }
198
199 // pet->photo_urls
200 cJSON *photo_urls = cJSON_GetObjectItemCaseSensitive(petJSON, "photoUrls");
201 if (!photo_urls) {
9
Assuming 'photo_urls' is non-null
10
Taking false branch
202 goto end;
203 }
204
205 list_t *photo_urlsList;
206
207 cJSON *photo_urls_local;
208 if(!cJSON_IsArray(photo_urls)) {
11
Assuming the condition is false
12
Taking false branch
209 goto end;//primitive container
210 }
211 photo_urlsList = list_create();
212
213
12.1
'photo_urls' is not equal to null
cJSON_ArrayForEach(photo_urls_local, photo_urls)for(photo_urls_local = (photo_urls != ((void*)0)) ? (photo_urls
)->child : ((void*)0); photo_urls_local != ((void*)0); photo_urls_local
= photo_urls_local->next)
13
'?' condition is true
14
Assuming 'photo_urls_local' is equal to null
15
Loop condition is false. Execution continues on line 223
214 {
215 if(!cJSON_IsString(photo_urls_local))
216 {
217 goto end;
218 }
219 list_addElement(photo_urlsList , strdup(photo_urls_local->valuestring));
220 }
221
222 // pet->tags
223 cJSON *tags = cJSON_GetObjectItemCaseSensitive(petJSON, "tags");
224 list_t *tagsList;
225 if (tags) {
16
Assuming 'tags' is null
17
Taking false branch
226 cJSON *tags_local_nonprimitive;
227 if(!cJSON_IsArray(tags)){
228 goto end; //nonprimitive container
229 }
230
231 tagsList = list_create();
232
233 cJSON_ArrayForEach(tags_local_nonprimitive,tags )for(tags_local_nonprimitive = (tags != ((void*)0)) ? (tags)->
child : ((void*)0); tags_local_nonprimitive != ((void*)0); tags_local_nonprimitive
= tags_local_nonprimitive->next)
234 {
235 if(!cJSON_IsObject(tags_local_nonprimitive)){
236 goto end;
237 }
238 tag_t *tagsItem = tag_parseFromJSON(tags_local_nonprimitive);
239
240 list_addElement(tagsList, tagsItem);
241 }
242 }
243
244 // pet->status
245 cJSON *status = cJSON_GetObjectItemCaseSensitive(petJSON, "status");
246 openapi_petstore_pet_STATUS_e statusVariable;
247 if (status) {
18
Assuming 'status' is null
19
Taking false branch
248 if(!cJSON_IsString(status))
249 {
250 goto end; //Enum
251 }
252 statusVariable = statuspet_FromString(status->valuestring);
253 }
254
255
256 pet_local_var = pet_create (
25
Potential memory leak
257
19.1
'id' is null
id ? id->valuedouble : 0,
20
'?' condition is false
258
20.1
'category' is null
category ? category_local_nonprim : NULL((void*)0),
21
'?' condition is false
259 strdup(name->valuestring),
22
Memory is allocated
260 photo_urlsList,
261
22.1
'tags' is null
tags ? tagsList : NULL((void*)0),
23
'?' condition is false
262
23.1
'status' is null
status ? statusVariable : -1
24
'?' condition is false
263 );
264
265 return pet_local_var;
266end:
267 return NULL((void*)0);
268
269}