Debian Codegraph @

gcc/testsuite/c-c++-common/analyzer/infinite-recursion-pr108524-qobject-json-parser.c

raw
1 /* Reduced from qemu-7.2.0's qobject/json-parser.c, which
2 is licensed under LGPLv2.1 or later. */
3
4 /* { dg-additional-options "-fno-analyzer-call-summaries -Wno-analyzer-too-complex" } */
5
6 #include "../../gcc.dg/analyzer/analyzer-decls.h"
7
8
9 typedef __builtin_va_list va_list;
10 typedef __SIZE_TYPE__ size_t;
11
12 typedef struct _GString GString;
13 typedef struct _GQueue GQueue;
14 typedef struct Error Error;
15 typedef struct QDict QDict;
16 typedef struct QList QList;
17 typedef struct QObject QObject;
18 typedef struct QString QString;
19
20 typedef enum QType {
21 /* [...snip...] */
22 QTYPE_QSTRING,
23 /* [...snip...] */
24 } QType;
25
26 struct QObjectBase_ {
27 QType type;
28 };
29
30 struct QObject {
31 struct QObjectBase_ base;
32 };
33
34 #define QOBJECT(obj) ((QObject *)obj)
35 #define qobject_unref(OBJ) /* [...snip...] */
36
37 void qobject_ref_impl(QObject *obj);
38 QType qobject_type(const QObject *obj);
39 QObject *qobject_check_type(const QObject *obj, QType type);
40
41 typedef struct QTailQLink {
42 void *tql_next;
43 struct QTailQLink *tql_prev;
44 } QTailQLink;
45
46 typedef struct QDictEntry {
47 char *key;
48 QObject *value;
49 struct {
50 struct QDictEntry *le_next;
51 struct QDictEntry **le_prev;
52 } next;
53 } QDictEntry;
54
55 struct QDict {
56 struct QObjectBase_ base;
57 size_t size;
58 struct {
59 struct QDictEntry *lh_first;
60 } table[512];
61 };
62
63 QDict *qdict_new(void);
64 void qdict_put_obj(QDict *qdict, const char *key, QObject *value);
65 int qdict_haskey(const QDict *qdict, const char *key);
66 typedef struct QListEntry {
67 QObject *value;
68 union {
69 struct QListEntry *tqe_next;
70 QTailQLink tqe_circ;
71 } next;
72 } QListEntry;
73
74 struct QList {
75 struct QObjectBase_ base;
76 union {
77 struct QListEntry *tqh_first;
78 QTailQLink tqh_circ;
79 } head;
80 };
81 QList *qlist_new(void);
82 void qlist_append_obj(QList *qlist, QObject *obj);
83
84 struct QString {
85 struct QObjectBase_ base;
86 const char *string;
87 };
88 QString *qstring_from_str(const char *str);
89 const char *qstring_get_str(const QString *qstring);
90
91 typedef enum json_token_type {
92 JSON_ERROR = 0,
93
94 JSON_LCURLY = 100,
95 JSON_MIN = JSON_LCURLY,
96 JSON_RCURLY,
97 JSON_LSQUARE,
98 JSON_RSQUARE,
99 JSON_COLON,
100 JSON_COMMA,
101 JSON_INTEGER,
102 JSON_FLOAT,
103 JSON_KEYWORD,
104 JSON_STRING,
105 JSON_INTERP,
106 JSON_END_OF_INPUT,
107 JSON_MAX = JSON_END_OF_INPUT
108 } JSONTokenType;
109 typedef struct JSONToken JSONToken;
110
111 struct JSONToken {
112 JSONTokenType type;
113 int x;
114 int y;
115 char str[];
116 };
117
118 typedef struct JSONParserContext {
119 Error *err;
120 JSONToken *current;
121 GQueue *buf;
122 va_list *ap;
123 } JSONParserContext;
124 static QObject *parse_value(JSONParserContext *ctxt);
125
126 void __attribute__((__format__(gnu_printf, 3, 4)))
127 parse_error(JSONParserContext *ctxt, JSONToken *token, const char *msg, ...);
128
129 JSONToken *parser_context_pop_token(JSONParserContext *ctxt);
130 JSONToken *parser_context_peek_token(JSONParserContext *ctxt);
131
132 static int parse_pair(JSONParserContext *ctxt, QDict *dict) {
133 QObject *key_obj = NULL;
134 QString *key;
135 QObject *value;
136 JSONToken *peek, *token;
137
138 peek = parser_context_peek_token(ctxt);
139 if (peek == NULL) {
140 parse_error(ctxt, NULL, "premature EOI");
141 goto out;
142 }
143
144 key_obj = parse_value(ctxt); /* { dg-bogus "infinite recursion" } */
145 key = ((QString *)qobject_check_type(key_obj, QTYPE_QSTRING));
146 if (!key) {
147 parse_error(ctxt, peek, "key is not a string in object");
148 goto out;
149 }
150
151 token = parser_context_pop_token(ctxt);
152 if (token == NULL) {
153 parse_error(ctxt, NULL, "premature EOI");
154 goto out;
155 }
156
157 if (token->type != JSON_COLON) {
158 parse_error(ctxt, token, "missing : in object pair");
159 goto out;
160 }
161
162 value = parse_value(ctxt);
163 if (value == NULL) {
164 parse_error(ctxt, token, "Missing value in dict");
165 goto out;
166 }
167
168 if (qdict_haskey(dict, qstring_get_str(key))) {
169 parse_error(ctxt, token, "duplicate key");
170 goto out;
171 }
172
173 qdict_put_obj(dict, qstring_get_str(key), value);
174
175 qobject_unref(key_obj);
176 return 0;
177
178 out:
179 qobject_unref(key_obj);
180 return -1;
181 }
182
183 static QObject *parse_object(JSONParserContext *ctxt) {
184 QDict *dict = NULL;
185 JSONToken *token, *peek;
186
187 token = parser_context_pop_token(ctxt);
188
189 dict = qdict_new();
190
191 peek = parser_context_peek_token(ctxt);
192 if (peek == NULL) {
193 parse_error(ctxt, NULL, "premature EOI");
194 goto out;
195 }
196
197 if (peek->type != JSON_RCURLY) {
198 if (parse_pair(ctxt, dict) == -1) {
199 goto out;
200 }
201
202 token = parser_context_pop_token(ctxt);
203 if (token == NULL) {
204 parse_error(ctxt, NULL, "premature EOI");
205 goto out;
206 }
207
208 while (token->type != JSON_RCURLY) {
209 if (token->type != JSON_COMMA) {
210 parse_error(ctxt, token, "expected separator in dict");
211 goto out;
212 }
213
214 if (parse_pair(ctxt, dict) == -1) {
215 goto out;
216 }
217
218 token = parser_context_pop_token(ctxt);
219 if (token == NULL) {
220 parse_error(ctxt, NULL, "premature EOI");
221 goto out;
222 }
223 }
224 } else {
225 (void)parser_context_pop_token(ctxt);
226 }
227
228 return QOBJECT(dict);
229
230 out:
231 qobject_unref (dict);
232 return NULL;
233 }
234
235 static QObject *parse_array(JSONParserContext *ctxt) {
236 QList *list = NULL;
237 JSONToken *token, *peek;
238
239 token = parser_context_pop_token(ctxt);
240
241 list = qlist_new();
242
243 peek = parser_context_peek_token(ctxt);
244 if (peek == NULL) {
245 parse_error(ctxt, NULL, "premature EOI");
246 goto out;
247 }
248
249 if (peek->type != JSON_RSQUARE) {
250 QObject *obj;
251
252 obj = parse_value(ctxt); /* { dg-bogus "infinite recursion" } */
253 if (obj == NULL) {
254 parse_error(ctxt, token, "expecting value");
255 goto out;
256 }
257
258 qlist_append_obj(list, obj);
259
260 token = parser_context_pop_token(ctxt);
261 if (token == NULL) {
262 parse_error(ctxt, NULL, "premature EOI");
263 goto out;
264 }
265
266 while (token->type != JSON_RSQUARE) {
267 if (token->type != JSON_COMMA) {
268 parse_error(ctxt, token, "expected separator in list");
269 goto out;
270 }
271
272 obj = parse_value(ctxt);
273 if (obj == NULL) {
274 parse_error(ctxt, token, "expecting value");
275 goto out;
276 }
277
278 qlist_append_obj(list, obj);
279
280 token = parser_context_pop_token(ctxt);
281 if (token == NULL) {
282 parse_error(ctxt, NULL, "premature EOI");
283 goto out;
284 }
285 }
286 } else {
287 (void)parser_context_pop_token(ctxt);
288 }
289
290 return QOBJECT(list);
291
292 out:
293 qobject_unref(list);
294 return NULL;
295 }
296
297 QObject *parse_keyword(JSONParserContext *ctxt);
298 QObject *parse_literal(JSONParserContext *ctxt);
299
300 QObject *parse_value(JSONParserContext *ctxt) {
301 JSONToken *token;
302
303 token = parser_context_peek_token(ctxt);
304 if (token == NULL) {
305 parse_error(ctxt, NULL, "premature EOI");
306 return NULL;
307 }
308
309 switch (token->type) {
310 case JSON_LCURLY:
311 return parse_object(ctxt); /* { dg-bogus "infinite recursion" } */
312 case JSON_LSQUARE:
313 return parse_array(ctxt); /* { dg-bogus "infinite recursion" } */
314 case JSON_INTEGER:
315 case JSON_FLOAT:
316 case JSON_STRING:
317 return parse_literal(ctxt);
318 case JSON_KEYWORD:
319 return parse_keyword(ctxt);
320 default:
321 parse_error(ctxt, token, "expecting value");
322 return NULL;
323 }
324 }