]> git.proxmox.com Git - mirror_qemu.git/blob - qobject/json-streamer.c
json: Unbox tokens queue in JSONMessageParser
[mirror_qemu.git] / qobject / json-streamer.c
1 /*
2 * JSON streaming support
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 "qemu-common.h"
16 #include "qapi/error.h"
17 #include "qapi/qmp/json-lexer.h"
18 #include "qapi/qmp/json-parser.h"
19 #include "qapi/qmp/json-streamer.h"
20
21 #define MAX_TOKEN_SIZE (64ULL << 20)
22 #define MAX_TOKEN_COUNT (2ULL << 20)
23 #define MAX_NESTING (1 << 10)
24
25 static void json_message_free_tokens(JSONMessageParser *parser)
26 {
27 JSONToken *token;
28
29 while ((token = g_queue_pop_head(&parser->tokens))) {
30 g_free(token);
31 }
32 }
33
34 void json_message_process_token(JSONLexer *lexer, GString *input,
35 JSONTokenType type, int x, int y)
36 {
37 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
38 QObject *json = NULL;
39 Error *err = NULL;
40 JSONToken *token;
41
42 switch (type) {
43 case JSON_LCURLY:
44 parser->brace_count++;
45 break;
46 case JSON_RCURLY:
47 parser->brace_count--;
48 break;
49 case JSON_LSQUARE:
50 parser->bracket_count++;
51 break;
52 case JSON_RSQUARE:
53 parser->bracket_count--;
54 break;
55 case JSON_ERROR:
56 error_setg(&err, "JSON parse error, stray '%s'", input->str);
57 goto out_emit;
58 case JSON_END_OF_INPUT:
59 if (g_queue_is_empty(&parser->tokens)) {
60 return;
61 }
62 json = json_parser_parse(&parser->tokens, parser->ap, &err);
63 goto out_emit;
64 default:
65 break;
66 }
67
68 /*
69 * Security consideration, we limit total memory allocated per object
70 * and the maximum recursion depth that a message can force.
71 */
72 if (parser->token_size + input->len + 1 > MAX_TOKEN_SIZE) {
73 error_setg(&err, "JSON token size limit exceeded");
74 goto out_emit;
75 }
76 if (g_queue_get_length(&parser->tokens) + 1 > MAX_TOKEN_COUNT) {
77 error_setg(&err, "JSON token count limit exceeded");
78 goto out_emit;
79 }
80 if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
81 error_setg(&err, "JSON nesting depth limit exceeded");
82 goto out_emit;
83 }
84
85 token = g_malloc(sizeof(JSONToken) + input->len + 1);
86 token->type = type;
87 memcpy(token->str, input->str, input->len);
88 token->str[input->len] = 0;
89 token->x = x;
90 token->y = y;
91
92 parser->token_size += input->len;
93
94 g_queue_push_tail(&parser->tokens, token);
95
96 if ((parser->brace_count > 0 || parser->bracket_count > 0)
97 && parser->bracket_count >= 0 && parser->bracket_count >= 0) {
98 return;
99 }
100
101 json = json_parser_parse(&parser->tokens, parser->ap, &err);
102
103 out_emit:
104 parser->brace_count = 0;
105 parser->bracket_count = 0;
106 json_message_free_tokens(parser);
107 parser->token_size = 0;
108 parser->emit(parser->opaque, json, err);
109 }
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 {
116 parser->emit = emit;
117 parser->opaque = opaque;
118 parser->ap = ap;
119 parser->brace_count = 0;
120 parser->bracket_count = 0;
121 g_queue_init(&parser->tokens);
122 parser->token_size = 0;
123
124 json_lexer_init(&parser->lexer, !!ap);
125 }
126
127 void json_message_parser_feed(JSONMessageParser *parser,
128 const char *buffer, size_t size)
129 {
130 json_lexer_feed(&parser->lexer, buffer, size);
131 }
132
133 void json_message_parser_flush(JSONMessageParser *parser)
134 {
135 json_lexer_flush(&parser->lexer);
136 assert(g_queue_is_empty(&parser->tokens));
137 }
138
139 void json_message_parser_destroy(JSONMessageParser *parser)
140 {
141 json_lexer_destroy(&parser->lexer);
142 json_message_free_tokens(parser);
143 }