Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/qapi/qobject-input-visitor.c
Line
Count
Source
1
/*
2
 * Input Visitor
3
 *
4
 * Copyright (C) 2012-2017 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 <math.h>
17
#include "qapi/compat-policy.h"
18
#include "qapi/error.h"
19
#include "qapi/qobject-input-visitor.h"
20
#include "qapi/visitor-impl.h"
21
#include "qemu/queue.h"
22
#include "qobject/qjson.h"
23
#include "qobject/qbool.h"
24
#include "qobject/qdict.h"
25
#include "qapi/qmp/qerror.h"
26
#include "qobject/qlist.h"
27
#include "qobject/qnull.h"
28
#include "qobject/qnum.h"
29
#include "qobject/qstring.h"
30
#include "qemu/cutils.h"
31
#include "qemu/keyval.h"
32
33
typedef struct StackObject {
34
    const char *name;            /* Name of @obj in its parent, if any */
35
    QObject *obj;                /* QDict or QList being visited */
36
    void *qapi; /* sanity check that caller uses same pointer */
37
38
    GHashTable *h;              /* If @obj is QDict: unvisited keys */
39
    const QListEntry *entry;    /* If @obj is QList: unvisited tail */
40
    unsigned index;             /* If @obj is QList: list index of @entry */
41
42
    QSLIST_ENTRY(StackObject) node; /* parent */
43
} StackObject;
44
45
struct QObjectInputVisitor {
46
    Visitor visitor;
47
48
    /* Root of visit at visitor creation. */
49
    QObject *root;
50
    bool keyval;                /* Assume @root made with keyval_parse() */
51
52
    /* Stack of objects being visited (all entries will be either
53
     * QDict or QList). */
54
    QSLIST_HEAD(, StackObject) stack;
55
56
    GString *errname;           /* Accumulator for full_name() */
57
};
58
59
static QObjectInputVisitor *to_qiv(Visitor *v)
60
128k
{
61
128k
    return container_of(v, QObjectInputVisitor, visitor);
62
128k
}
63
64
/*
65
 * Find the full name of something @qiv is currently visiting.
66
 * @qiv is visiting something named @name in the stack of containers
67
 * @qiv->stack.
68
 * If @n is zero, return its full name.
69
 * If @n is positive, return the full name of the @n-th container
70
 * counting from the top.  The stack of containers must have at least
71
 * @n elements.
72
 * The returned string is valid until the next full_name_nth(@v) or
73
 * destruction of @v.
74
 */
75
static const char *full_name_nth(QObjectInputVisitor *qiv, const char *name,
76
                                 int n)
77
0
{
78
0
    StackObject *so;
79
0
    char buf[32];
80
81
0
    if (qiv->errname) {
82
0
        g_string_truncate(qiv->errname, 0);
83
0
    } else {
84
0
        qiv->errname = g_string_new("");
85
0
    }
86
87
0
    QSLIST_FOREACH(so , &qiv->stack, node) {
88
0
        if (n) {
89
0
            n--;
90
0
        } else if (qobject_type(so->obj) == QTYPE_QDICT) {
91
0
            g_string_prepend(qiv->errname, name ?: "<anonymous>");
92
0
            g_string_prepend_c(qiv->errname, '.');
93
0
        } else {
94
0
            snprintf(buf, sizeof(buf),
95
0
                     qiv->keyval ? ".%u" : "[%u]",
96
0
                     so->index);
97
0
            g_string_prepend(qiv->errname, buf);
98
0
        }
99
0
        name = so->name;
100
0
    }
101
0
    assert(!n);
102
103
0
    if (name) {
104
0
        g_string_prepend(qiv->errname, name);
105
0
    } else if (qiv->errname->str[0] == '.') {
106
0
        g_string_erase(qiv->errname, 0, 1);
107
0
    } else if (!qiv->errname->str[0]) {
108
0
        return "<anonymous>";
109
0
    }
110
111
0
    return qiv->errname->str;
112
0
}
113
114
static const char *full_name(QObjectInputVisitor *qiv, const char *name)
115
0
{
116
0
    return full_name_nth(qiv, name, 0);
117
0
}
118
119
static QObject *qobject_input_try_get_object(QObjectInputVisitor *qiv,
120
                                             const char *name,
121
                                             bool consume)
122
64.8k
{
123
64.8k
    StackObject *tos;
124
64.8k
    QObject *qobj;
125
64.8k
    QObject *ret;
126
127
64.8k
    if (QSLIST_EMPTY(&qiv->stack)) {
128
        /* Starting at root, name is ignored. */
129
63.8k
        assert(qiv->root);
130
63.8k
        return qiv->root;
131
63.8k
    }
132
133
    /* We are in a container; find the next element. */
134
934
    tos = QSLIST_FIRST(&qiv->stack);
135
934
    qobj = tos->obj;
136
934
    assert(qobj);
137
138
934
    if (qobject_type(qobj) == QTYPE_QDICT) {
139
934
        assert(name);
140
934
        ret = qdict_get(qobject_to(QDict, qobj), name);
141
934
        if (tos->h && consume && ret) {
142
390
            bool removed = g_hash_table_remove(tos->h, name);
143
390
            assert(removed);
144
390
        }
145
934
    } else {
146
0
        assert(qobject_type(qobj) == QTYPE_QLIST);
147
0
        assert(!name);
148
0
        if (tos->entry) {
149
0
            ret = qlist_entry_obj(tos->entry);
150
0
            if (consume) {
151
0
                tos->entry = qlist_next(tos->entry);
152
0
            }
153
0
        } else {
154
0
            ret = NULL;
155
0
        }
156
0
        if (consume) {
157
0
            tos->index++;
158
0
        }
159
0
    }
160
161
934
    return ret;
162
934
}
163
164
static QObject *qobject_input_get_object(QObjectInputVisitor *qiv,
165
                                         const char *name,
166
                                         bool consume, Error **errp)
167
64.2k
{
168
64.2k
    QObject *obj = qobject_input_try_get_object(qiv, name, consume);
169
170
64.2k
    if (!obj) {
171
0
        error_setg(errp, QERR_MISSING_PARAMETER, full_name(qiv, name));
172
0
    }
173
64.2k
    return obj;
174
64.2k
}
175
176
static const char *qobject_input_get_keyval(QObjectInputVisitor *qiv,
177
                                            const char *name,
178
                                            Error **errp)
179
329
{
180
329
    QObject *qobj;
181
329
    QString *qstr;
182
183
329
    qobj = qobject_input_get_object(qiv, name, true, errp);
184
329
    if (!qobj) {
185
0
        return NULL;
186
0
    }
187
188
329
    qstr = qobject_to(QString, qobj);
189
329
    if (!qstr) {
190
0
        switch (qobject_type(qobj)) {
191
0
        case QTYPE_QDICT:
192
0
        case QTYPE_QLIST:
193
0
            error_setg(errp, "Parameters '%s.*' are unexpected",
194
0
                       full_name(qiv, name));
195
0
            return NULL;
196
0
        default:
197
            /* Non-string scalar (should this be an assertion?) */
198
0
            error_setg(errp, "Internal error: parameter %s invalid",
199
0
                       full_name(qiv, name));
200
0
            return NULL;
201
0
        }
202
0
    }
203
204
329
    return qstring_get_str(qstr);
205
329
}
206
207
static const QListEntry *qobject_input_push(QObjectInputVisitor *qiv,
208
                                            const char *name,
209
                                            QObject *obj, void *qapi)
210
645
{
211
645
    GHashTable *h;
212
645
    StackObject *tos = g_new0(StackObject, 1);
213
645
    QDict *qdict = qobject_to(QDict, obj);
214
645
    QList *qlist = qobject_to(QList, obj);
215
645
    const QDictEntry *entry;
216
217
645
    assert(obj);
218
645
    tos->name = name;
219
645
    tos->obj = obj;
220
645
    tos->qapi = qapi;
221
222
645
    if (qdict) {
223
372
        h = g_hash_table_new(g_str_hash, g_str_equal);
224
372
        for (entry = qdict_first(qdict);
225
762
             entry;
226
390
             entry = qdict_next(qdict, entry)) {
227
390
            g_hash_table_insert(h, (void *)qdict_entry_key(entry), NULL);
228
390
        }
229
372
        tos->h = h;
230
372
    } else {
231
273
        assert(qlist);
232
273
        tos->entry = qlist_first(qlist);
233
273
        tos->index = -1;
234
273
    }
235
236
645
    QSLIST_INSERT_HEAD(&qiv->stack, tos, node);
237
645
    return tos->entry;
238
645
}
239
240
241
static bool qobject_input_check_struct(Visitor *v, Error **errp)
242
372
{
243
372
    QObjectInputVisitor *qiv = to_qiv(v);
244
372
    StackObject *tos = QSLIST_FIRST(&qiv->stack);
245
372
    GHashTableIter iter;
246
372
    const char *key;
247
248
372
    assert(tos && !tos->entry);
249
250
372
    g_hash_table_iter_init(&iter, tos->h);
251
372
    if (g_hash_table_iter_next(&iter, (void **)&key, NULL)) {
252
0
        error_setg(errp, "Parameter '%s' is unexpected",
253
0
                   full_name(qiv, key));
254
0
        return false;
255
0
    }
256
372
    return true;
257
372
}
258
259
static void qobject_input_stack_object_free(StackObject *tos)
260
645
{
261
645
    if (tos->h) {
262
372
        g_hash_table_unref(tos->h);
263
372
    }
264
265
645
    g_free(tos);
266
645
}
267
268
static void qobject_input_pop(Visitor *v, void **obj)
269
645
{
270
645
    QObjectInputVisitor *qiv = to_qiv(v);
271
645
    StackObject *tos = QSLIST_FIRST(&qiv->stack);
272
273
645
    assert(tos && tos->qapi == obj);
274
645
    QSLIST_REMOVE_HEAD(&qiv->stack, node);
275
645
    qobject_input_stack_object_free(tos);
276
645
}
277
278
static bool qobject_input_start_struct(Visitor *v, const char *name, void **obj,
279
                                       size_t size, Error **errp)
280
372
{
281
372
    QObjectInputVisitor *qiv = to_qiv(v);
282
372
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
283
284
372
    if (obj) {
285
149
        *obj = NULL;
286
149
    }
287
372
    if (!qobj) {
288
0
        return false;
289
0
    }
290
372
    if (qobject_type(qobj) != QTYPE_QDICT) {
291
0
        error_setg(errp, "Invalid parameter type for '%s', expected: object",
292
0
                   full_name(qiv, name));
293
0
        return false;
294
0
    }
295
296
372
    qobject_input_push(qiv, name, qobj, obj);
297
298
372
    if (obj) {
299
149
        *obj = g_malloc0(size);
300
149
    }
301
372
    return true;
302
372
}
303
304
static void qobject_input_end_struct(Visitor *v, void **obj)
305
372
{
306
372
    QObjectInputVisitor *qiv = to_qiv(v);
307
372
    StackObject *tos = QSLIST_FIRST(&qiv->stack);
308
309
372
    assert(qobject_type(tos->obj) == QTYPE_QDICT && tos->h);
310
372
    qobject_input_pop(v, obj);
311
372
}
312
313
314
static bool qobject_input_start_list(Visitor *v, const char *name,
315
                                     GenericList **list, size_t size,
316
                                     Error **errp)
317
273
{
318
273
    QObjectInputVisitor *qiv = to_qiv(v);
319
273
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
320
273
    const QListEntry *entry;
321
322
273
    if (list) {
323
273
        *list = NULL;
324
273
    }
325
273
    if (!qobj) {
326
0
        return false;
327
0
    }
328
273
    if (qobject_type(qobj) != QTYPE_QLIST) {
329
0
        error_setg(errp, "Invalid parameter type for '%s', expected: array",
330
0
                   full_name(qiv, name));
331
0
        return false;
332
0
    }
333
334
273
    entry = qobject_input_push(qiv, name, qobj, list);
335
273
    if (entry && list) {
336
0
        *list = g_malloc0(size);
337
0
    }
338
273
    return true;
339
273
}
340
341
static GenericList *qobject_input_next_list(Visitor *v, GenericList *tail,
342
                                            size_t size)
343
0
{
344
0
    QObjectInputVisitor *qiv = to_qiv(v);
345
0
    StackObject *tos = QSLIST_FIRST(&qiv->stack);
346
347
0
    assert(tos && qobject_to(QList, tos->obj));
348
349
0
    if (!tos->entry) {
350
0
        return NULL;
351
0
    }
352
0
    tail->next = g_malloc0(size);
353
0
    return tail->next;
354
0
}
355
356
static bool qobject_input_check_list(Visitor *v, Error **errp)
357
273
{
358
273
    QObjectInputVisitor *qiv = to_qiv(v);
359
273
    StackObject *tos = QSLIST_FIRST(&qiv->stack);
360
361
273
    assert(tos && qobject_to(QList, tos->obj));
362
363
273
    if (tos->entry) {
364
0
        error_setg(errp, "Only %u list elements expected in %s",
365
0
                   tos->index + 1, full_name_nth(qiv, NULL, 1));
366
0
        return false;
367
0
    }
368
273
    return true;
369
273
}
370
371
static void qobject_input_end_list(Visitor *v, void **obj)
372
273
{
373
273
    QObjectInputVisitor *qiv = to_qiv(v);
374
273
    StackObject *tos = QSLIST_FIRST(&qiv->stack);
375
376
273
    assert(qobject_type(tos->obj) == QTYPE_QLIST && !tos->h);
377
273
    qobject_input_pop(v, obj);
378
273
}
379
380
static bool qobject_input_start_alternate(Visitor *v, const char *name,
381
                                          GenericAlternate **obj, size_t size,
382
                                          Error **errp)
383
2.24k
{
384
2.24k
    QObjectInputVisitor *qiv = to_qiv(v);
385
2.24k
    QObject *qobj = qobject_input_get_object(qiv, name, false, errp);
386
387
2.24k
    if (!qobj) {
388
0
        *obj = NULL;
389
0
        return false;
390
0
    }
391
2.24k
    *obj = g_malloc0(size);
392
2.24k
    (*obj)->type = qobject_type(qobj);
393
2.24k
    return true;
394
2.24k
}
395
396
static bool qobject_input_type_int64(Visitor *v, const char *name, int64_t *obj,
397
                                     Error **errp)
398
7.75k
{
399
7.75k
    QObjectInputVisitor *qiv = to_qiv(v);
400
7.75k
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
401
7.75k
    QNum *qnum;
402
403
7.75k
    if (!qobj) {
404
0
        return false;
405
0
    }
406
7.75k
    qnum = qobject_to(QNum, qobj);
407
7.75k
    if (!qnum || !qnum_get_try_int(qnum, obj)) {
408
0
        error_setg(errp, "Invalid parameter type for '%s', expected: integer",
409
0
                   full_name(qiv, name));
410
0
        return false;
411
0
    }
412
7.75k
    return true;
413
7.75k
}
414
415
static bool qobject_input_type_int64_keyval(Visitor *v, const char *name,
416
                                            int64_t *obj, Error **errp)
417
0
{
418
0
    QObjectInputVisitor *qiv = to_qiv(v);
419
0
    const char *str = qobject_input_get_keyval(qiv, name, errp);
420
421
0
    if (!str) {
422
0
        return false;
423
0
    }
424
425
0
    if (qemu_strtoi64(str, NULL, 0, obj) < 0) {
426
        /* TODO report -ERANGE more nicely */
427
0
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
428
0
                   full_name(qiv, name), "integer");
429
0
        return false;
430
0
    }
431
0
    return true;
432
0
}
433
434
static bool qobject_input_type_uint64(Visitor *v, const char *name,
435
                                      uint64_t *obj, Error **errp)
436
15.4k
{
437
15.4k
    QObjectInputVisitor *qiv = to_qiv(v);
438
15.4k
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
439
15.4k
    QNum *qnum;
440
15.4k
    int64_t val;
441
442
15.4k
    if (!qobj) {
443
0
        return false;
444
0
    }
445
15.4k
    qnum = qobject_to(QNum, qobj);
446
15.4k
    if (!qnum) {
447
0
        goto err;
448
0
    }
449
450
15.4k
    if (qnum_get_try_uint(qnum, obj)) {
451
15.4k
        return true;
452
15.4k
    }
453
454
    /* Need to accept negative values for backward compatibility */
455
23
    if (qnum_get_try_int(qnum, &val)) {
456
23
        *obj = val;
457
23
        return true;
458
23
    }
459
460
0
err:
461
0
    error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
462
0
               full_name(qiv, name), "uint64");
463
0
    return false;
464
23
}
465
466
static bool qobject_input_type_uint64_keyval(Visitor *v, const char *name,
467
                                             uint64_t *obj, Error **errp)
468
11
{
469
11
    QObjectInputVisitor *qiv = to_qiv(v);
470
11
    const char *str = qobject_input_get_keyval(qiv, name, errp);
471
472
11
    if (!str) {
473
0
        return false;
474
0
    }
475
476
11
    if (qemu_strtou64(str, NULL, 0, obj) < 0) {
477
        /* TODO report -ERANGE more nicely */
478
0
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
479
0
                   full_name(qiv, name), "integer");
480
0
        return false;
481
0
    }
482
11
    return true;
483
11
}
484
485
static bool qobject_input_type_bool(Visitor *v, const char *name, bool *obj,
486
                                    Error **errp)
487
30.5k
{
488
30.5k
    QObjectInputVisitor *qiv = to_qiv(v);
489
30.5k
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
490
30.5k
    QBool *qbool;
491
492
30.5k
    if (!qobj) {
493
0
        return false;
494
0
    }
495
30.5k
    qbool = qobject_to(QBool, qobj);
496
30.5k
    if (!qbool) {
497
0
        error_setg(errp, "Invalid parameter type for '%s', expected: boolean",
498
0
                   full_name(qiv, name));
499
0
        return false;
500
0
    }
501
502
30.5k
    *obj = qbool_get_bool(qbool);
503
30.5k
    return true;
504
30.5k
}
505
506
static bool qobject_input_type_bool_keyval(Visitor *v, const char *name,
507
                                           bool *obj, Error **errp)
508
10
{
509
10
    QObjectInputVisitor *qiv = to_qiv(v);
510
10
    const char *str = qobject_input_get_keyval(qiv, name, errp);
511
512
10
    if (!str) {
513
0
        return false;
514
0
    }
515
516
10
    if (!qapi_bool_parse(name, str, obj, NULL)) {
517
0
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
518
0
                   full_name(qiv, name), "'on' or 'off'");
519
0
        return false;
520
0
    }
521
10
    return true;
522
10
}
523
524
static bool qobject_input_type_str(Visitor *v, const char *name, char **obj,
525
                                   Error **errp)
526
7.27k
{
527
7.27k
    QObjectInputVisitor *qiv = to_qiv(v);
528
7.27k
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
529
7.27k
    QString *qstr;
530
531
7.27k
    *obj = NULL;
532
7.27k
    if (!qobj) {
533
0
        return false;
534
0
    }
535
7.27k
    qstr = qobject_to(QString, qobj);
536
7.27k
    if (!qstr) {
537
0
        error_setg(errp, "Invalid parameter type for '%s', expected: string",
538
0
                   full_name(qiv, name));
539
0
        return false;
540
0
    }
541
542
7.27k
    *obj = g_strdup(qstring_get_str(qstr));
543
7.27k
    return true;
544
7.27k
}
545
546
static bool qobject_input_type_str_keyval(Visitor *v, const char *name,
547
                                          char **obj, Error **errp)
548
247
{
549
247
    QObjectInputVisitor *qiv = to_qiv(v);
550
247
    const char *str = qobject_input_get_keyval(qiv, name, errp);
551
552
247
    *obj = g_strdup(str);
553
247
    return !!str;
554
247
}
555
556
static bool qobject_input_type_number(Visitor *v, const char *name, double *obj,
557
                                      Error **errp)
558
0
{
559
0
    QObjectInputVisitor *qiv = to_qiv(v);
560
0
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
561
0
    QNum *qnum;
562
563
0
    if (!qobj) {
564
0
        return false;
565
0
    }
566
0
    qnum = qobject_to(QNum, qobj);
567
0
    if (!qnum) {
568
0
        error_setg(errp, "Invalid parameter type for '%s', expected: number",
569
0
                   full_name(qiv, name));
570
0
        return false;
571
0
    }
572
573
0
    *obj = qnum_get_double(qnum);
574
0
    return true;
575
0
}
576
577
static bool qobject_input_type_number_keyval(Visitor *v, const char *name,
578
                                             double *obj, Error **errp)
579
0
{
580
0
    QObjectInputVisitor *qiv = to_qiv(v);
581
0
    const char *str = qobject_input_get_keyval(qiv, name, errp);
582
0
    double val;
583
584
0
    if (!str) {
585
0
        return false;
586
0
    }
587
588
0
    if (qemu_strtod_finite(str, NULL, &val)) {
589
        /* TODO report -ERANGE more nicely */
590
0
        error_setg(errp, "Invalid parameter type for '%s', expected: number",
591
0
                   full_name(qiv, name));
592
0
        return false;
593
0
    }
594
595
0
    *obj = val;
596
0
    return true;
597
0
}
598
599
static bool qobject_input_type_any(Visitor *v, const char *name, QObject **obj,
600
                                   Error **errp)
601
0
{
602
0
    QObjectInputVisitor *qiv = to_qiv(v);
603
0
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
604
605
0
    *obj = NULL;
606
0
    if (!qobj) {
607
0
        return false;
608
0
    }
609
610
0
    *obj = qobject_ref(qobj);
611
0
    return true;
612
0
}
613
614
static bool qobject_input_type_null(Visitor *v, const char *name,
615
                                    QNull **obj, Error **errp)
616
0
{
617
0
    QObjectInputVisitor *qiv = to_qiv(v);
618
0
    QObject *qobj = qobject_input_get_object(qiv, name, true, errp);
619
620
0
    *obj = NULL;
621
0
    if (!qobj) {
622
0
        return false;
623
0
    }
624
625
0
    if (qobject_type(qobj) != QTYPE_QNULL) {
626
0
        error_setg(errp, "Invalid parameter type for '%s', expected: null",
627
0
                   full_name(qiv, name));
628
0
        return false;
629
0
    }
630
0
    *obj = qnull();
631
0
    return true;
632
0
}
633
634
static bool qobject_input_type_size_keyval(Visitor *v, const char *name,
635
                                           uint64_t *obj, Error **errp)
636
61
{
637
61
    QObjectInputVisitor *qiv = to_qiv(v);
638
61
    const char *str = qobject_input_get_keyval(qiv, name, errp);
639
640
61
    if (!str) {
641
0
        return false;
642
0
    }
643
644
61
    if (qemu_strtosz(str, NULL, obj) < 0) {
645
        /* TODO report -ERANGE more nicely */
646
0
        error_setg(errp, QERR_INVALID_PARAMETER_VALUE,
647
0
                   full_name(qiv, name), "size");
648
0
        return false;
649
0
    }
650
61
    return true;
651
61
}
652
653
static void qobject_input_optional(Visitor *v, const char *name, bool *present)
654
536
{
655
536
    QObjectInputVisitor *qiv = to_qiv(v);
656
536
    QObject *qobj = qobject_input_try_get_object(qiv, name, false);
657
658
536
    if (!qobj) {
659
473
        *present = false;
660
473
        return;
661
473
    }
662
663
63
    *present = true;
664
63
}
665
666
static bool qobject_input_policy_reject(Visitor *v, const char *name,
667
                                        uint64_t features,
668
                                        Error **errp)
669
0
{
670
0
    return !compat_policy_input_ok(features, &v->compat_policy,
671
0
                                   ERROR_CLASS_GENERIC_ERROR,
672
0
                                   "parameter", name, errp);
673
0
}
674
675
static void qobject_input_free(Visitor *v)
676
61.6k
{
677
61.6k
    QObjectInputVisitor *qiv = to_qiv(v);
678
679
61.6k
    while (!QSLIST_EMPTY(&qiv->stack)) {
680
0
        StackObject *tos = QSLIST_FIRST(&qiv->stack);
681
682
0
        QSLIST_REMOVE_HEAD(&qiv->stack, node);
683
0
        qobject_input_stack_object_free(tos);
684
0
    }
685
686
61.6k
    qobject_unref(qiv->root);
687
61.6k
    if (qiv->errname) {
688
0
        g_string_free(qiv->errname, TRUE);
689
0
    }
690
61.6k
    g_free(qiv);
691
61.6k
}
692
693
static QObjectInputVisitor *qobject_input_visitor_base_new(QObject *obj)
694
61.6k
{
695
61.6k
    QObjectInputVisitor *v = g_malloc0(sizeof(*v));
696
697
61.6k
    assert(obj);
698
699
61.6k
    v->visitor.type = VISITOR_INPUT;
700
61.6k
    v->visitor.start_struct = qobject_input_start_struct;
701
61.6k
    v->visitor.check_struct = qobject_input_check_struct;
702
61.6k
    v->visitor.end_struct = qobject_input_end_struct;
703
61.6k
    v->visitor.start_list = qobject_input_start_list;
704
61.6k
    v->visitor.next_list = qobject_input_next_list;
705
61.6k
    v->visitor.check_list = qobject_input_check_list;
706
61.6k
    v->visitor.end_list = qobject_input_end_list;
707
61.6k
    v->visitor.start_alternate = qobject_input_start_alternate;
708
61.6k
    v->visitor.optional = qobject_input_optional;
709
61.6k
    v->visitor.policy_reject = qobject_input_policy_reject;
710
61.6k
    v->visitor.free = qobject_input_free;
711
712
61.6k
    v->root = qobject_ref(obj);
713
714
61.6k
    return v;
715
61.6k
}
716
717
Visitor *qobject_input_visitor_new(QObject *obj)
718
61.3k
{
719
61.3k
    QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
720
721
61.3k
    v->visitor.type_int64 = qobject_input_type_int64;
722
61.3k
    v->visitor.type_uint64 = qobject_input_type_uint64;
723
61.3k
    v->visitor.type_bool = qobject_input_type_bool;
724
61.3k
    v->visitor.type_str = qobject_input_type_str;
725
61.3k
    v->visitor.type_number = qobject_input_type_number;
726
61.3k
    v->visitor.type_any = qobject_input_type_any;
727
61.3k
    v->visitor.type_null = qobject_input_type_null;
728
729
61.3k
    return &v->visitor;
730
61.3k
}
731
732
Visitor *qobject_input_visitor_new_keyval(QObject *obj)
733
311
{
734
311
    QObjectInputVisitor *v = qobject_input_visitor_base_new(obj);
735
736
311
    v->visitor.type_int64 = qobject_input_type_int64_keyval;
737
311
    v->visitor.type_uint64 = qobject_input_type_uint64_keyval;
738
311
    v->visitor.type_bool = qobject_input_type_bool_keyval;
739
311
    v->visitor.type_str = qobject_input_type_str_keyval;
740
311
    v->visitor.type_number = qobject_input_type_number_keyval;
741
311
    v->visitor.type_any = qobject_input_type_any;
742
311
    v->visitor.type_null = qobject_input_type_null;
743
311
    v->visitor.type_size = qobject_input_type_size_keyval;
744
311
    v->keyval = true;
745
746
311
    return &v->visitor;
747
311
}
748
749
Visitor *qobject_input_visitor_new_str(const char *str,
750
                                       const char *implied_key,
751
                                       Error **errp)
752
73
{
753
73
    bool is_json = str[0] == '{';
754
73
    QObject *obj;
755
73
    QDict *args;
756
73
    Visitor *v;
757
758
73
    if (is_json) {
759
0
        obj = qobject_from_json(str, errp);
760
0
        if (!obj) {
761
0
            return NULL;
762
0
        }
763
0
        args = qobject_to(QDict, obj);
764
0
        assert(args);
765
0
        v = qobject_input_visitor_new(QOBJECT(args));
766
73
    } else {
767
73
        args = keyval_parse(str, implied_key, NULL, errp);
768
73
        if (!args) {
769
0
            return NULL;
770
0
        }
771
73
        v = qobject_input_visitor_new_keyval(QOBJECT(args));
772
73
    }
773
73
    qobject_unref(args);
774
775
73
    return v;
776
73
}