]> git.proxmox.com Git - mirror_qemu.git/blame - qobject/json-streamer.c
qjson: Have qobject_from_json() & friends reject empty and blank
[mirror_qemu.git] / qobject / json-streamer.c
CommitLineData
d7ff3acb
AL
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
f2ad72b3 14#include "qemu/osdep.h"
d7ff3acb 15#include "qemu-common.h"
84a56f38 16#include "qapi/error.h"
7b1b5d19 17#include "qapi/qmp/json-lexer.h"
62815d85 18#include "qapi/qmp/json-parser.h"
7b1b5d19 19#include "qapi/qmp/json-streamer.h"
d7ff3acb 20
29c75ddd 21#define MAX_TOKEN_SIZE (64ULL << 20)
df649835 22#define MAX_TOKEN_COUNT (2ULL << 20)
29c75ddd
AL
23#define MAX_NESTING (1ULL << 10)
24
ba4dba54
EB
25static void json_message_free_token(void *token, void *opaque)
26{
27 g_free(token);
28}
29
95385fe9
PB
30static void json_message_free_tokens(JSONMessageParser *parser)
31{
32 if (parser->tokens) {
ba4dba54 33 g_queue_foreach(parser->tokens, json_message_free_token, NULL);
95385fe9
PB
34 g_queue_free(parser->tokens);
35 parser->tokens = NULL;
36 }
37}
38
037f2440
MA
39void json_message_process_token(JSONLexer *lexer, GString *input,
40 JSONTokenType type, int x, int y)
d7ff3acb
AL
41{
42 JSONMessageParser *parser = container_of(lexer, JSONMessageParser, lexer);
ff281a27 43 QObject *json = NULL;
62815d85 44 Error *err = NULL;
9bada897 45 JSONToken *token;
d7ff3acb 46
c5461660
MA
47 switch (type) {
48 case JSON_LCURLY:
49 parser->brace_count++;
50 break;
51 case JSON_RCURLY:
52 parser->brace_count--;
53 break;
54 case JSON_LSQUARE:
55 parser->bracket_count++;
56 break;
57 case JSON_RSQUARE:
58 parser->bracket_count--;
59 break;
269e57ae 60 case JSON_ERROR:
84a56f38 61 error_setg(&err, "JSON parse error, stray '%s'", input->str);
269e57ae 62 goto out_emit;
f9277915
MA
63 case JSON_END_OF_INPUT:
64 if (g_queue_is_empty(parser->tokens)) {
65 return;
66 }
67 json = json_parser_parse(parser->tokens, parser->ap, &err);
68 parser->tokens = NULL;
69 goto out_emit;
c5461660
MA
70 default:
71 break;
d7ff3acb
AL
72 }
73
9bada897
PB
74 token = g_malloc(sizeof(JSONToken) + input->len + 1);
75 token->type = type;
76 memcpy(token->str, input->str, input->len);
77 token->str[input->len] = 0;
78 token->x = x;
79 token->y = y;
d7ff3acb 80
d2ca7c0b 81 parser->token_size += input->len;
29c75ddd 82
9bada897 83 g_queue_push_tail(parser->tokens, token);
d7ff3acb 84
ff281a27 85 if (parser->brace_count < 0 ||
55f8301f
AL
86 parser->bracket_count < 0 ||
87 (parser->brace_count == 0 &&
88 parser->bracket_count == 0)) {
ff281a27
MA
89 json = json_parser_parse(parser->tokens, parser->ap, &err);
90 parser->tokens = NULL;
5e2dafeb 91 goto out_emit;
ff281a27
MA
92 }
93
84a56f38
MA
94 /*
95 * Security consideration, we limit total memory allocated per object
96 * and the maximum recursion depth that a message can force.
97 */
98 if (parser->token_size > MAX_TOKEN_SIZE) {
99 error_setg(&err, "JSON token size limit exceeded");
100 goto out_emit;
101 }
102 if (g_queue_get_length(parser->tokens) > MAX_TOKEN_COUNT) {
103 error_setg(&err, "JSON token count limit exceeded");
104 goto out_emit;
105 }
106 if (parser->bracket_count + parser->brace_count > MAX_NESTING) {
107 error_setg(&err, "JSON nesting depth limit exceeded");
ff281a27 108 goto out_emit;
5e2dafeb
MR
109 }
110
111 return;
112
5e2dafeb 113out_emit:
5e2dafeb
MR
114 parser->brace_count = 0;
115 parser->bracket_count = 0;
ff281a27 116 json_message_free_tokens(parser);
95385fe9 117 parser->tokens = g_queue_new();
5e2dafeb 118 parser->token_size = 0;
62815d85 119 parser->emit(parser->opaque, json, err);
d7ff3acb
AL
120}
121
122void json_message_parser_init(JSONMessageParser *parser,
62815d85
MA
123 void (*emit)(void *opaque, QObject *json,
124 Error *err),
125 void *opaque, va_list *ap)
d7ff3acb 126{
62815d85
MA
127 parser->emit = emit;
128 parser->opaque = opaque;
129 parser->ap = ap;
d7ff3acb
AL
130 parser->brace_count = 0;
131 parser->bracket_count = 0;
95385fe9 132 parser->tokens = g_queue_new();
29c75ddd 133 parser->token_size = 0;
d7ff3acb 134
2cbd15aa 135 json_lexer_init(&parser->lexer, !!ap);
d7ff3acb
AL
136}
137
7c1e1d54 138void json_message_parser_feed(JSONMessageParser *parser,
d7ff3acb
AL
139 const char *buffer, size_t size)
140{
7c1e1d54 141 json_lexer_feed(&parser->lexer, buffer, size);
d7ff3acb
AL
142}
143
7c1e1d54 144void json_message_parser_flush(JSONMessageParser *parser)
d7ff3acb 145{
7c1e1d54 146 json_lexer_flush(&parser->lexer);
f9277915 147 assert(g_queue_is_empty(parser->tokens));
d7ff3acb
AL
148}
149
150void json_message_parser_destroy(JSONMessageParser *parser)
151{
152 json_lexer_destroy(&parser->lexer);
95385fe9 153 json_message_free_tokens(parser);
d7ff3acb 154}