Bug Summary

File:model/order.c
Warning:line 180, column 21
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 order.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/order.c
1#include <stdlib.h>
2#include <string.h>
3#include <stdio.h>
4#include "order.h"
5
6
7char* statusorder_ToString(openapi_petstore_order_STATUS_e status) {
8 char* statusArray[] = { "NULL", "placed", "approved", "delivered" };
9 return statusArray[status];
10}
11
12openapi_petstore_order_STATUS_e statusorder_FromString(char* status){
13 int stringToReturn = 0;
14 char *statusArray[] = { "NULL", "placed", "approved", "delivered" };
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
25order_t *order_create(
26 long id,
27 long pet_id,
28 int quantity,
29 char *ship_date,
30 openapi_petstore_order_STATUS_e status,
31 int complete
32 ) {
33 order_t *order_local_var = malloc(sizeof(order_t));
34 if (!order_local_var) {
35 return NULL((void*)0);
36 }
37 order_local_var->id = id;
38 order_local_var->pet_id = pet_id;
39 order_local_var->quantity = quantity;
40 order_local_var->ship_date = ship_date;
41 order_local_var->status = status;
42 order_local_var->complete = complete;
43
44 return order_local_var;
45}
46
47
48void order_free(order_t *order) {
49 if(NULL((void*)0) == order){
50 return ;
51 }
52 listEntry_t *listEntry;
53 if (order->ship_date) {
54 free(order->ship_date);
55 order->ship_date = NULL((void*)0);
56 }
57 free(order);
58}
59
60cJSON *order_convertToJSON(order_t *order) {
61 cJSON *item = cJSON_CreateObject();
62
63 // order->id
64 if(order->id) {
65 if(cJSON_AddNumberToObject(item, "id", order->id) == NULL((void*)0)) {
66 goto fail; //Numeric
67 }
68 }
69
70
71 // order->pet_id
72 if(order->pet_id) {
73 if(cJSON_AddNumberToObject(item, "petId", order->pet_id) == NULL((void*)0)) {
74 goto fail; //Numeric
75 }
76 }
77
78
79 // order->quantity
80 if(order->quantity) {
81 if(cJSON_AddNumberToObject(item, "quantity", order->quantity) == NULL((void*)0)) {
82 goto fail; //Numeric
83 }
84 }
85
86
87 // order->ship_date
88 if(order->ship_date) {
89 if(cJSON_AddStringToObject(item, "shipDate", order->ship_date) == NULL((void*)0)) {
90 goto fail; //Date-Time
91 }
92 }
93
94
95 // order->status
96
97 if(cJSON_AddStringToObject(item, "status", statusorder_ToString(order->status)) == NULL((void*)0))
98 {
99 goto fail; //Enum
100 }
101
102
103
104 // order->complete
105 if(order->complete) {
106 if(cJSON_AddBoolToObject(item, "complete", order->complete) == NULL((void*)0)) {
107 goto fail; //Bool
108 }
109 }
110
111 return item;
112fail:
113 if (item) {
114 cJSON_Delete(item);
115 }
116 return NULL((void*)0);
117}
118
119order_t *order_parseFromJSON(cJSON *orderJSON){
120
121 order_t *order_local_var = NULL((void*)0);
122
123 // order->id
124 cJSON *id = cJSON_GetObjectItemCaseSensitive(orderJSON, "id");
125 if (id) {
1
Assuming 'id' is null
2
Taking false branch
126 if(!cJSON_IsNumber(id))
127 {
128 goto end; //Numeric
129 }
130 }
131
132 // order->pet_id
133 cJSON *pet_id = cJSON_GetObjectItemCaseSensitive(orderJSON, "petId");
134 if (pet_id) {
3
Assuming 'pet_id' is null
4
Taking false branch
135 if(!cJSON_IsNumber(pet_id))
136 {
137 goto end; //Numeric
138 }
139 }
140
141 // order->quantity
142 cJSON *quantity = cJSON_GetObjectItemCaseSensitive(orderJSON, "quantity");
143 if (quantity) {
5
Assuming 'quantity' is null
6
Taking false branch
144 if(!cJSON_IsNumber(quantity))
145 {
146 goto end; //Numeric
147 }
148 }
149
150 // order->ship_date
151 cJSON *ship_date = cJSON_GetObjectItemCaseSensitive(orderJSON, "shipDate");
152 if (ship_date) {
7
Assuming 'ship_date' is non-null
8
Taking true branch
153 if(!cJSON_IsString(ship_date))
9
Assuming the condition is false
10
Taking false branch
154 {
155 goto end; //DateTime
156 }
157 }
158
159 // order->status
160 cJSON *status = cJSON_GetObjectItemCaseSensitive(orderJSON, "status");
161 openapi_petstore_order_STATUS_e statusVariable;
162 if (status) {
11
Assuming 'status' is null
12
Taking false branch
163 if(!cJSON_IsString(status))
164 {
165 goto end; //Enum
166 }
167 statusVariable = statusorder_FromString(status->valuestring);
168 }
169
170 // order->complete
171 cJSON *complete = cJSON_GetObjectItemCaseSensitive(orderJSON, "complete");
172 if (complete) {
13
Assuming 'complete' is null
14
Taking false branch
173 if(!cJSON_IsBool(complete))
174 {
175 goto end; //Bool
176 }
177 }
178
179
180 order_local_var = order_create (
22
Potential memory leak
181
14.1
'id' is null
id ? id->valuedouble : 0,
15
'?' condition is false
182
15.1
'pet_id' is null
pet_id ? pet_id->valuedouble : 0,
16
'?' condition is false
183
16.1
'quantity' is null
quantity ? quantity->valuedouble : 0,
17
'?' condition is false
184
17.1
'ship_date' is non-null
ship_date ? strdup(ship_date->valuestring) : NULL((void*)0),
18
'?' condition is true
19
Memory is allocated
185
19.1
'status' is null
status ? statusVariable : -1,
20
'?' condition is false
186
20.1
'complete' is null
complete ? complete->valueint : 0
21
'?' condition is false
187 );
188
189 return order_local_var;
190end:
191 return NULL((void*)0);
192
193}