]> git.proxmox.com Git - mirror_qemu.git/blob - qapi/qobject-input-visitor.c
qapi: Make input visitors detect unvisited list tails
[mirror_qemu.git] / qapi / qobject-input-visitor.c
1 /*
2 * Input Visitor
3 *
4 * Copyright (C) 2012-2016 Red Hat, Inc.
5 * Copyright IBM, Corp. 2011
6 *
7 * Authors:
8 * Anthony Liguori <aliguori@us.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
15 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qapi/qobject-input-visitor.h"
18 #include "qapi/visitor-impl.h"
19 #include "qemu/queue.h"
20 #include "qemu-common.h"
21 #include "qapi/qmp/types.h"
22 #include "qapi/qmp/qerror.h"
23
24 typedef struct StackObject {
25 const char *name; /* Name of @obj in its parent, if any */
26 QObject *obj; /* QDict or QList being visited */
27 void *qapi; /* sanity check that caller uses same pointer */
28
29 GHashTable *h; /* If @obj is QDict: unvisited keys */
30 const QListEntry *entry; /* If @obj is QList: unvisited tail */
31 unsigned index; /* If @obj is QList: list index of @entry */
32
33 QSLIST_ENTRY(StackObject) node; /* parent */
34 } StackObject;
35
36 struct QObjectInputVisitor {
37 Visitor visitor;
38
39 /* Root of visit at visitor creation. */
40 QObject *root;
41
42 /* Stack of objects being visited (all entries will be either
43 * QDict or QList). */
44 QSLIST_HEAD(, StackObject) stack;
45
46 GString *errname; /* Accumulator for full_name() */
47 };
48
49 static QObjectInputVisitor *to_qiv(Visitor *v)
50 {
51 return container_of(v, QObjectInputVisitor, visitor);
52 }
53
54 static const char *full_name_nth(QObjectInputVisitor *qiv, const char *name,
55 int n)
56 {
57 StackObject *so;
58 char buf[32];
59
60 if (qiv->errname) {
61 g_string_truncate(qiv->errname, 0);
62 } else {
63 qiv->errname = g_string_new("");
64 }
65
66 QSLIST_FOREACH(so , &qiv->stack, node) {
67 if (n) {
68 n--;
69 } else if (qobject_type(so->obj) == QTYPE_QDICT) {
70 g_string_prepend(qiv->errname, name ?: "<anonymous>");
71 g_string_prepend_c(qiv->errname, '.');
72 } else {
73 snprintf(buf, sizeof(buf), "[%u]", so->index);
74 g_string_prepend(qiv->errname, buf);
75 }
76 name = so->name;
77 }
78 assert(!n);
79
80 if (name) {
81 g_string_prepend(qiv->errname, name);
82 } else if (qiv->errname->str[0] == '.') {
83 g_string_erase(qiv->errname, 0, 1);
84 } else if (!qiv->errname->str[0]) {
85 return "<anonymous>";
86 }
87
88 return qiv->errname->str;
89 }
90
91 static const char *full_name(QObjectInputVisitor *qiv, const char *name)
92 {
93 return full_name_nth(qiv, name, 0);
94 }
95
96 static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv,
97 const char *name,
98 bool consume)
99 {
100 StackObject *tos;
101 QObject *qobj;
102 QObject *ret;
103
104 if (QSLIST_EMPTY(&qiv->stack)) {
105 /* Starting at root, name is ignored. */
106 assert(qiv->root);
107 return qiv->root;
108 }
109
110 /* We are in a container; find the next element. */
111 tos = QSLIST_FIRST(&qiv->stack);
112 qobj = tos->obj;
113 assert(qobj);
114
115 if (qobject_type(qobj) == QTYPE_QDICT) {
116 assert(name);
117 ret = qdict_get(qobject_to_qdict(qobj), name);
118 if (tos->h && consume && ret) {
119 bool removed = g_hash_table_remove(tos->h, name);
120 assert(removed);
121 }
122 } else {
123 assert(qobject_type(qobj) == QTYPE_QLIST);
124 assert(!name);
125 ret = qlist_entry_obj(tos->entry);
126 assert(ret);
127 if (consume) {
128 tos->entry = qlist_next(tos->entry);
129 tos->index++;
130 }
131 }
132
133 return ret;
134 }
135
136 static QObject *qobject_input_get_object(QObjectInputVisitor *qiv,
137 const char *name,
138 bool consume, Error **errp)
139 {
140 QObject *obj = qobject_input_try_get_object(qiv, name, consume);
141
142 if (!obj) {
143 error_setg(errp, QERR_MISSING_PARAMETER, full_name(qiv, name));
144 }
145 return obj;
146 }
147
148 static void qdict_add_key(const char *key, QObject *obj, void *opaque)
149 {
150 GHashTable *h = opaque;
151 g_hash_table_insert(h, (gpointer) key, NULL);
152 }
153
154 static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv,
155 const char *name,
156 QObject *obj, void *qapi)
157 {
158 GHashTable *h;
159 StackObject *tos = g_new0(StackObject, 1);
160
161 assert(obj);
162 tos->name = name;
163 tos->obj = obj;
164 tos->qapi = qapi;
165
166 if (qobject_type(obj) == QTYPE_QDICT) {
167 h = g_hash_table_new(g_str_hash, g_str_equal);
168 qdict_iter(qobject_to_qdict(obj), qdict_add_key, h);
169 tos->h = h;
170 } else {
171 assert(qobject_type(obj) == QTYPE_QLIST);
172 tos->entry = qlist_first(qobject_to_qlist(obj));
173 tos->index = -1;
174 }
175
176 QSLIST_INSERT_HEAD(&qiv->stack, tos, node);
177 return tos->entry;
178 }
179
180
181 static void qobject_input_check_struct(Visitor *v, Error **errp)
182 {
183 QObjectInputVisitor *qiv = to_qiv(v);
184 StackObject *tos = QSLIST_FIRST(&qiv->stack);
185 GHashTableIter iter;
186 const char *key;
187
188 assert(tos && !tos->entry);
189
190 g_hash_table_iter_init(&iter, tos->h);
191 if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
192 error_setg(errp, "Parameter '%s' is unexpected",
193 full_name(qiv, key));
194 }
195 }
196
197 static void qobject_input_stack_object_free(StackObject *tos)
198 {
199 if (tos->h) {
200 g_hash_table_unref(tos->h);
201 }
202
203 g_free(tos);
204 }
205
206 static void qobject_input_pop(Visitor *v, void **obj)
207 {
208 QObjectInputVisitor *qiv = to_qiv(v);
209 StackObject *tos = QSLIST_FIRST(&qiv->stack);
210
211 assert(tos && tos->qapi == obj);
212 QSLIST_REMOVE_HEAD(&qiv->stack, node);
213 qobject_input_stack_object_free(tos);
214 }
215
216 static void qobject_input_start_struct(Visitor *v, const char *name, void **obj,
217 size_t size, Error **errp)
218 {
219 QObjectInputVisitor *qiv = to_qiv(v);
220 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
221
222 if (obj) {
223 *obj = NULL;
224 }
225 if (!qobj) {
226 return;
227 }
228 if (qobject_type(qobj) != QTYPE_QDICT) {
229 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
230 full_name(qiv, name), "object");
231 return;
232 }
233
234 qobject_input_push(qiv, name, qobj, obj);
235
236 if (obj) {
237 *obj = g_malloc0(size);
238 }
239 }
240
241
242 static void qobject_input_start_list(Visitor *v, const char *name,
243 GenericList **list, size_t size,
244 Error **errp)
245 {
246 QObjectInputVisitor *qiv = to_qiv(v);
247 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
248 const QListEntry *entry;
249
250 if (list) {
251 *list = NULL;
252 }
253 if (!qobj) {
254 return;
255 }
256 if (qobject_type(qobj) != QTYPE_QLIST) {
257 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
258 full_name(qiv, name), "array");
259 return;
260 }
261
262 entry = qobject_input_push(qiv, name, qobj, list);
263 if (entry && list) {
264 *list = g_malloc0(size);
265 }
266 }
267
268 static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
269 size_t size)
270 {
271 QObjectInputVisitor *qiv = to_qiv(v);
272 StackObject *tos = QSLIST_FIRST(&qiv->stack);
273
274 assert(tos && tos->obj && qobject_type(tos->obj) == QTYPE_QLIST);
275
276 if (!tos->entry) {
277 return NULL;
278 }
279 tail->next = g_malloc0(size);
280 return tail->next;
281 }
282
283 static void qobject_input_check_list(Visitor *v, Error **errp)
284 {
285 QObjectInputVisitor *qiv = to_qiv(v);
286 StackObject *tos = QSLIST_FIRST(&qiv->stack);
287
288 assert(tos && tos->obj && qobject_type(tos->obj) == QTYPE_QLIST);
289
290 if (tos->entry) {
291 error_setg(errp, "Only %u list elements expected in %s",
292 tos->index + 1, full_name_nth(qiv, NULL, 1));
293 }
294 }
295
296
297 static void qobject_input_start_alternate(Visitor *v, const char *name,
298 GenericAlternate **obj, size_t size,
299 bool promote_int, Error **errp)
300 {
301 QObjectInputVisitor *qiv = to_qiv(v);
302 QObject *qobj = qobject_input_get_object(qiv, name, false, errp);
303
304 if (!qobj) {
305 *obj = NULL;
306 return;
307 }
308 *obj = g_malloc0(size);
309 (*obj)->type = qobject_type(qobj);
310 if (promote_int && (*obj)->type == QTYPE_QINT) {
311 (*obj)->type = QTYPE_QFLOAT;
312 }
313 }
314
315 static void qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj,
316 Error **errp)
317 {
318 QObjectInputVisitor *qiv = to_qiv(v);
319 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
320 QInt *qint;
321
322 if (!qobj) {
323 return;
324 }
325 qint = qobject_to_qint(qobj);
326 if (!qint) {
327 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
328 full_name(qiv, name), "integer");
329 return;
330 }
331
332 *obj = qint_get_int(qint);
333 }
334
335 static void qobject_input_type_uint64(Visitor *v, const char *name,
336 uint64_t *obj, Error **errp)
337 {
338 /* FIXME: qobject_to_qint mishandles values over INT64_MAX */
339 QObjectInputVisitor *qiv = to_qiv(v);
340 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
341 QInt *qint;
342
343 if (!qobj) {
344 return;
345 }
346 qint = qobject_to_qint(qobj);
347 if (!qint) {
348 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
349 full_name(qiv, name), "integer");
350 return;
351 }
352
353 *obj = qint_get_int(qint);
354 }
355
356 static void qobject_input_type_bool(Visitor *v, const char *name, bool *obj,
357 Error **errp)
358 {
359 QObjectInputVisitor *qiv = to_qiv(v);
360 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
361 QBool *qbool;
362
363 if (!qobj) {
364 return;
365 }
366 qbool = qobject_to_qbool(qobj);
367 if (!qbool) {
368 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
369 full_name(qiv, name), "boolean");
370 return;
371 }
372
373 *obj = qbool_get_bool(qbool);
374 }
375
376 static void qobject_input_type_str(Visitor *v, const char *name, char **obj,
377 Error **errp)
378 {
379 QObjectInputVisitor *qiv = to_qiv(v);
380 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
381 QString *qstr;
382
383 *obj = NULL;
384 if (!qobj) {
385 return;
386 }
387 qstr = qobject_to_qstring(qobj);
388 if (!qstr) {
389 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
390 full_name(qiv, name), "string");
391 return;
392 }
393
394 *obj = g_strdup(qstring_get_str(qstr));
395 }
396
397 static void qobject_input_type_number(Visitor *v, const char *name, double *obj,
398 Error **errp)
399 {
400 QObjectInputVisitor *qiv = to_qiv(v);
401 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
402 QInt *qint;
403 QFloat *qfloat;
404
405 if (!qobj) {
406 return;
407 }
408 qint = qobject_to_qint(qobj);
409 if (qint) {
410 *obj = qint_get_int(qobject_to_qint(qobj));
411 return;
412 }
413
414 qfloat = qobject_to_qfloat(qobj);
415 if (qfloat) {
416 *obj = qfloat_get_double(qobject_to_qfloat(qobj));
417 return;
418 }
419
420 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
421 full_name(qiv, name), "number");
422 }
423
424 static void qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
425 Error **errp)
426 {
427 QObjectInputVisitor *qiv = to_qiv(v);
428 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
429
430 *obj = NULL;
431 if (!qobj) {
432 return;
433 }
434
435 qobject_incref(qobj);
436 *obj = qobj;
437 }
438
439 static void qobject_input_type_null(Visitor *v, const char *name, Error **errp)
440 {
441 QObjectInputVisitor *qiv = to_qiv(v);
442 QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
443
444 if (!qobj) {
445 return;
446 }
447
448 if (qobject_type(qobj) != QTYPE_QNULL) {
449 error_setg(errp, QERR_INVALID_PARAMETER_TYPE,
450 full_name(qiv, name), "null");
451 }
452 }
453
454 static void qobject_input_optional(Visitor *v, const char *name, bool *present)
455 {
456 QObjectInputVisitor *qiv = to_qiv(v);
457 QObject *qobj = qobject_input_try_get_object(qiv, name, false);
458
459 if (!qobj) {
460 *present = false;
461 return;
462 }
463
464 *present = true;
465 }
466
467 static void qobject_input_free(Visitor *v)
468 {
469 QObjectInputVisitor *qiv = to_qiv(v);
470
471 while (!QSLIST_EMPTY(&qiv->stack)) {
472 StackObject *tos = QSLIST_FIRST(&qiv->stack);
473
474 QSLIST_REMOVE_HEAD(&qiv->stack, node);
475 qobject_input_stack_object_free(tos);
476 }
477
478 qobject_decref(qiv->root);
479 if (qiv->errname) {
480 g_string_free(qiv->errname, TRUE);
481 }
482 g_free(qiv);
483 }
484
485 Visitor *qobject_input_visitor_new(QObject *obj)
486 {
487 QObjectInputVisitor *v;
488
489 assert(obj);
490 v = g_malloc0(sizeof(*v));
491
492 v->visitor.type = VISITOR_INPUT;
493 v->visitor.start_struct = qobject_input_start_struct;
494 v->visitor.check_struct = qobject_input_check_struct;
495 v->visitor.end_struct = qobject_input_pop;
496 v->visitor.start_list = qobject_input_start_list;
497 v->visitor.next_list = qobject_input_next_list;
498 v->visitor.check_list = qobject_input_check_list;
499 v->visitor.end_list = qobject_input_pop;
500 v->visitor.start_alternate = qobject_input_start_alternate;
501 v->visitor.type_int64 = qobject_input_type_int64;
502 v->visitor.type_uint64 = qobject_input_type_uint64;
503 v->visitor.type_bool = qobject_input_type_bool;
504 v->visitor.type_str = qobject_input_type_str;
505 v->visitor.type_number = qobject_input_type_number;
506 v->visitor.type_any = qobject_input_type_any;
507 v->visitor.type_null = qobject_input_type_null;
508 v->visitor.optional = qobject_input_optional;
509 v->visitor.free = qobject_input_free;
510
511 v->root = obj;
512 qobject_incref(obj);
513
514 return &v->visitor;
515 }