]> git.proxmox.com Git - mirror_qemu.git/blame - qapi/qapi-dealloc-visitor.c
qapi: Fix string input visitor handling of invalid list
[mirror_qemu.git] / qapi / qapi-dealloc-visitor.c
CommitLineData
d5f3c29c
MR
1/*
2 * Dealloc Visitor
3 *
08f9541d 4 * Copyright (C) 2012-2016 Red Hat, Inc.
d5f3c29c
MR
5 * Copyright IBM, Corp. 2011
6 *
7 * Authors:
8 * Michael Roth <mdroth@linux.vnet.ibm.com>
9 *
10 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
11 * See the COPYING.LIB file in the top-level directory.
12 *
13 */
14
cbf21151 15#include "qemu/osdep.h"
7b1b5d19 16#include "qapi/dealloc-visitor.h"
1de7afc9 17#include "qemu/queue.h"
d5f3c29c 18#include "qemu-common.h"
7b1b5d19
PB
19#include "qapi/qmp/types.h"
20#include "qapi/visitor-impl.h"
d5f3c29c
MR
21
22typedef struct StackEntry
23{
24 void *value;
0b9d8542 25 bool is_list_head;
d5f3c29c
MR
26 QTAILQ_ENTRY(StackEntry) node;
27} StackEntry;
28
29struct QapiDeallocVisitor
30{
31 Visitor visitor;
32 QTAILQ_HEAD(, StackEntry) stack;
33};
34
35static QapiDeallocVisitor *to_qov(Visitor *v)
36{
37 return container_of(v, QapiDeallocVisitor, visitor);
38}
39
40static void qapi_dealloc_push(QapiDeallocVisitor *qov, void *value)
41{
7267c094 42 StackEntry *e = g_malloc0(sizeof(*e));
d5f3c29c
MR
43
44 e->value = value;
0b9d8542
MR
45
46 /* see if we're just pushing a list head tracker */
47 if (value == NULL) {
48 e->is_list_head = true;
49 }
d5f3c29c
MR
50 QTAILQ_INSERT_HEAD(&qov->stack, e, node);
51}
52
53static void *qapi_dealloc_pop(QapiDeallocVisitor *qov)
54{
55 StackEntry *e = QTAILQ_FIRST(&qov->stack);
56 QObject *value;
57 QTAILQ_REMOVE(&qov->stack, e, node);
58 value = e->value;
7267c094 59 g_free(e);
d5f3c29c
MR
60 return value;
61}
62
0b2a0d6b 63static void qapi_dealloc_start_struct(Visitor *v, const char *name, void **obj,
337283df 64 size_t unused, Error **errp)
d5f3c29c
MR
65{
66 QapiDeallocVisitor *qov = to_qov(v);
67 qapi_dealloc_push(qov, obj);
68}
69
15c2f669 70static void qapi_dealloc_end_struct(Visitor *v)
d5f3c29c
MR
71{
72 QapiDeallocVisitor *qov = to_qov(v);
73 void **obj = qapi_dealloc_pop(qov);
74 if (obj) {
7267c094 75 g_free(*obj);
d5f3c29c
MR
76 }
77}
78
dbf11922
EB
79static void qapi_dealloc_start_alternate(Visitor *v, const char *name,
80 GenericAlternate **obj, size_t size,
81 bool promote_int, Error **errp)
3dce9cad
WX
82{
83 QapiDeallocVisitor *qov = to_qov(v);
84 qapi_dealloc_push(qov, obj);
85}
86
dbf11922 87static void qapi_dealloc_end_alternate(Visitor *v)
3dce9cad
WX
88{
89 QapiDeallocVisitor *qov = to_qov(v);
90 void **obj = qapi_dealloc_pop(qov);
91 if (obj) {
92 g_free(*obj);
93 }
94}
95
d5f3c29c
MR
96static void qapi_dealloc_start_list(Visitor *v, const char *name, Error **errp)
97{
5666dd19 98 QapiDeallocVisitor *qov = to_qov(v);
0b9d8542 99 qapi_dealloc_push(qov, NULL);
d5f3c29c
MR
100}
101
e65d89bf
EB
102static GenericList *qapi_dealloc_next_list(Visitor *v, GenericList **listp,
103 size_t size)
d5f3c29c 104{
5666dd19
MR
105 GenericList *list = *listp;
106 QapiDeallocVisitor *qov = to_qov(v);
0b9d8542 107 StackEntry *e = QTAILQ_FIRST(&qov->stack);
5666dd19 108
0b9d8542
MR
109 if (e && e->is_list_head) {
110 e->is_list_head = false;
111 return list;
5666dd19
MR
112 }
113
0b9d8542
MR
114 if (list) {
115 list = list->next;
116 g_free(*listp);
117 return list;
118 }
119
120 return NULL;
d5f3c29c
MR
121}
122
08f9541d 123static void qapi_dealloc_end_list(Visitor *v)
d5f3c29c 124{
0b9d8542
MR
125 QapiDeallocVisitor *qov = to_qov(v);
126 void *obj = qapi_dealloc_pop(qov);
127 assert(obj == NULL); /* should've been list head tracker with no payload */
d5f3c29c
MR
128}
129
0b2a0d6b 130static void qapi_dealloc_type_str(Visitor *v, const char *name, char **obj,
d5f3c29c
MR
131 Error **errp)
132{
b690d679
PL
133 if (obj) {
134 g_free(*obj);
135 }
d5f3c29c
MR
136}
137
0b2a0d6b 138static void qapi_dealloc_type_int64(Visitor *v, const char *name, int64_t *obj,
4c40314a 139 Error **errp)
d5f3c29c
MR
140{
141}
142
0b2a0d6b
EB
143static void qapi_dealloc_type_uint64(Visitor *v, const char *name,
144 uint64_t *obj, Error **errp)
f755dea7
EB
145{
146}
147
0b2a0d6b 148static void qapi_dealloc_type_bool(Visitor *v, const char *name, bool *obj,
d5f3c29c
MR
149 Error **errp)
150{
151}
152
0b2a0d6b 153static void qapi_dealloc_type_number(Visitor *v, const char *name, double *obj,
d5f3c29c
MR
154 Error **errp)
155{
156}
157
0b2a0d6b
EB
158static void qapi_dealloc_type_anything(Visitor *v, const char *name,
159 QObject **obj, Error **errp)
28770e05
MA
160{
161 if (obj) {
162 qobject_decref(*obj);
163 }
164}
165
3bc97fd5
EB
166static void qapi_dealloc_type_null(Visitor *v, const char *name, Error **errp)
167{
168}
169
d5f3c29c
MR
170Visitor *qapi_dealloc_get_visitor(QapiDeallocVisitor *v)
171{
172 return &v->visitor;
173}
174
175void qapi_dealloc_visitor_cleanup(QapiDeallocVisitor *v)
176{
7267c094 177 g_free(v);
d5f3c29c
MR
178}
179
180QapiDeallocVisitor *qapi_dealloc_visitor_new(void)
181{
182 QapiDeallocVisitor *v;
183
7267c094 184 v = g_malloc0(sizeof(*v));
d5f3c29c 185
983f52d4 186 v->visitor.type = VISITOR_DEALLOC;
d5f3c29c
MR
187 v->visitor.start_struct = qapi_dealloc_start_struct;
188 v->visitor.end_struct = qapi_dealloc_end_struct;
dbf11922
EB
189 v->visitor.start_alternate = qapi_dealloc_start_alternate;
190 v->visitor.end_alternate = qapi_dealloc_end_alternate;
d5f3c29c
MR
191 v->visitor.start_list = qapi_dealloc_start_list;
192 v->visitor.next_list = qapi_dealloc_next_list;
193 v->visitor.end_list = qapi_dealloc_end_list;
4c40314a 194 v->visitor.type_int64 = qapi_dealloc_type_int64;
f755dea7 195 v->visitor.type_uint64 = qapi_dealloc_type_uint64;
d5f3c29c
MR
196 v->visitor.type_bool = qapi_dealloc_type_bool;
197 v->visitor.type_str = qapi_dealloc_type_str;
198 v->visitor.type_number = qapi_dealloc_type_number;
28770e05 199 v->visitor.type_any = qapi_dealloc_type_anything;
3bc97fd5 200 v->visitor.type_null = qapi_dealloc_type_null;
d5f3c29c
MR
201
202 QTAILQ_INIT(&v->stack);
203
204 return v;
205}