Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/qobject/json-streamer.c
Line
Count
Source
1
/*
2
 * JSON parser - callback interface and error recovery
3
 *
4
 * Copyright IBM, Corp. 2009
5
 *
6
 * Authors:
7
 *  Anthony Liguori   <aliguori@us.ibm.com>
8
 *
9
 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10
 * See the COPYING.LIB file in the top-level directory.
11
 *
12
 */
13
14
#include "qemu/osdep.h"
15
#include "qapi/error.h"
16
#include "json-parser-int.h"
17
18
1.68M
#define MAX_TOKEN_SIZE (64ULL << 20)
19
1.68M
#define MAX_TOKEN_COUNT (2ULL << 20)
20
1.68M
#define MAX_NESTING (1 << 10)
21
22
void json_message_process_token(JSONLexer *lexer, GString *input,
23
                                JSONTokenType type, int x, int y)
24
1.68M
{
25
1.68M
    JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
26
1.68M
    Error *err = NULL;
27
28
1.68M
    parser->token_size += input->len;
29
1.68M
    parser->token_count++;
30
31
    /* Detect message boundaries for error recovery purposes.  */
32
1.68M
    switch (type) {
33
168k
    case JSON_LCURLY:
34
168k
        parser->brace_count++;
35
168k
        break;
36
168k
    case JSON_RCURLY:
37
168k
        if (!parser->brace_count) {
38
0
            goto end_error_recovery;
39
0
        }
40
168k
        parser->brace_count--;
41
168k
        break;
42
0
    case JSON_LSQUARE:
43
0
        parser->bracket_count++;
44
0
        break;
45
0
    case JSON_RSQUARE:
46
0
        if (!parser->bracket_count) {
47
0
            goto end_error_recovery;
48
0
        }
49
0
        parser->bracket_count--;
50
0
        break;
51
0
    case JSON_ERROR:
52
0
    end_error_recovery:
53
        /*
54
         * We come here due to receiving either JSON_ERROR or a
55
         * JSON_R{CURLY,SQUARE}) that is known to be unbalanced.
56
         * If in error recovery, end it immediately.  If not in
57
         * error recovery, json_parser_feed() will raise an error
58
         * but error recovery won't be entered at all.
59
         */
60
0
        parser->brace_count = 0;
61
0
        parser->bracket_count = 0;
62
0
        break;
63
1.34M
    default:
64
1.34M
        break;
65
1.68M
    }
66
67
1.68M
    if (parser->error) {
68
        /* error recovery, eat tokens until parentheses balance */
69
1.68M
    } else {
70
        /*
71
         * Safety consideration, we limit total memory allocated per object
72
         * and the maximum nesting depth that a message can force.
73
         */
74
1.68M
        if (parser->token_size >= MAX_TOKEN_SIZE) {
75
0
            error_setg(&err, "JSON token size limit exceeded");
76
1.68M
        } else if (parser->token_count > MAX_TOKEN_COUNT) {
77
0
            error_setg(&err, "JSON token count limit exceeded");
78
1.68M
        } else if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
79
0
            error_setg(&err, "JSON nesting depth limit exceeded");
80
1.68M
        } else {
81
1.68M
            JSONToken token = (JSONToken) {
82
1.68M
                .type = type,
83
1.68M
                .x = x,
84
1.68M
                .y = y,
85
1.68M
                .str = input->str
86
1.68M
            };
87
1.68M
            QObject *json = json_parser_feed(&parser->parser, &token, &err);
88
1.68M
            if (json) {
89
168k
                parser->emit(parser->opaque, json, NULL);
90
168k
            }
91
1.68M
        }
92
93
1.68M
        if (err) {
94
0
            parser->emit(parser->opaque, NULL, err);
95
            /* start recovery */
96
0
            parser->error = true;
97
0
        }
98
1.68M
    }
99
100
1.68M
    if ((parser->brace_count == 0 && parser->bracket_count == 0)
101
1.34M
        || type == JSON_END_OF_INPUT) {
102
336k
        json_parser_reset(&parser->parser);
103
336k
        parser->error = false;
104
336k
        parser->brace_count = 0;
105
336k
        parser->bracket_count = 0;
106
336k
        parser->token_count = 0;
107
336k
        parser->token_size = 0;
108
336k
    }
109
1.68M
}
110
111
void json_message_parser_init(JSONMessageParser *parser,
112
                              void (*emit)(void *opaque, QObject *json,
113
                                           Error *err),
114
                              void *opaque, va_list *ap)
115
168k
{
116
168k
    parser->emit = emit;
117
168k
    parser->opaque = opaque;
118
168k
    parser->error = false;
119
168k
    parser->brace_count = 0;
120
168k
    parser->bracket_count = 0;
121
168k
    parser->token_count = 0;
122
168k
    parser->token_size = 0;
123
124
168k
    json_parser_init(&parser->parser, ap);
125
168k
    json_lexer_init(&parser->lexer, !!ap);
126
168k
}
127
128
void json_message_parser_feed(JSONMessageParser *parser,
129
                             const char *buffer, size_t size)
130
168k
{
131
168k
    json_lexer_feed(&parser->lexer, buffer, size);
132
168k
}
133
134
void json_message_parser_flush(JSONMessageParser *parser)
135
168k
{
136
168k
    json_lexer_flush(&parser->lexer);
137
168k
}
138
139
void json_message_parser_destroy(JSONMessageParser *parser)
140
168k
{
141
168k
    json_lexer_destroy(&parser->lexer);
142
168k
    json_parser_destroy(&parser->parser);
143
168k
}