aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/analyzer/infinite-recursion-pr108524-qobject-json-parser.c
blob: b40326fc252dcf146a9248569c04da5a2e6b7375 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Reduced from qemu-7.2.0's qobject/json-parser.c, which
   is licensed under LGPLv2.1 or later.  */

/* { dg-additional-options "-fno-analyzer-call-summaries -Wno-analyzer-too-complex" } */

#define NULL ((void *)0)
typedef __builtin_va_list va_list;
typedef __SIZE_TYPE__ size_t;

typedef struct _GString GString;
typedef struct _GQueue GQueue;
typedef struct Error Error;
typedef struct QDict QDict;
typedef struct QList QList;
typedef struct QObject QObject;
typedef struct QString QString;

typedef enum QType {
  /* [...snip...] */
  QTYPE_QSTRING,
  /* [...snip...] */
} QType;

struct QObjectBase_ {
  QType type;
};

struct QObject {
  struct QObjectBase_ base;
};

#define QOBJECT(obj) ((QObject *)obj)
#define qobject_unref(OBJ) /* [...snip...] */

void qobject_ref_impl(QObject *obj);
QType qobject_type(const QObject *obj);
QObject *qobject_check_type(const QObject *obj, QType type);

typedef struct QTailQLink {
  void *tql_next;
  struct QTailQLink *tql_prev;
} QTailQLink;

typedef struct QDictEntry {
  char *key;
  QObject *value;
  struct {
    struct QDictEntry *le_next;
    struct QDictEntry **le_prev;
  } next;
} QDictEntry;

struct QDict {
  struct QObjectBase_ base;
  size_t size;
  struct {
    struct QDictEntry *lh_first;
  } table[512];
};

QDict *qdict_new(void);
void qdict_put_obj(QDict *qdict, const char *key, QObject *value);
int qdict_haskey(const QDict *qdict, const char *key);
typedef struct QListEntry {
  QObject *value;
  union {
    struct QListEntry *tqe_next;
    QTailQLink tqe_circ;
  } next;
} QListEntry;

struct QList {
  struct QObjectBase_ base;
  union {
    struct QListEntry *tqh_first;
    QTailQLink tqh_circ;
  } head;
};
QList *qlist_new(void);
void qlist_append_obj(QList *qlist, QObject *obj);

struct QString {
  struct QObjectBase_ base;
  const char *string;
};
QString *qstring_from_str(const char *str);
const char *qstring_get_str(const QString *qstring);

typedef enum json_token_type {
  JSON_ERROR = 0,

  JSON_LCURLY = 100,
  JSON_MIN = JSON_LCURLY,
  JSON_RCURLY,
  JSON_LSQUARE,
  JSON_RSQUARE,
  JSON_COLON,
  JSON_COMMA,
  JSON_INTEGER,
  JSON_FLOAT,
  JSON_KEYWORD,
  JSON_STRING,
  JSON_INTERP,
  JSON_END_OF_INPUT,
  JSON_MAX = JSON_END_OF_INPUT
} JSONTokenType;
typedef struct JSONToken JSONToken;

struct JSONToken {
  JSONTokenType type;
  int x;
  int y;
  char str[];
};

typedef struct JSONParserContext {
  Error *err;
  JSONToken *current;
  GQueue *buf;
  va_list *ap;
} JSONParserContext;
static QObject *parse_value(JSONParserContext *ctxt);

void __attribute__((__format__(gnu_printf, 3, 4)))
parse_error(JSONParserContext *ctxt, JSONToken *token, const char *msg, ...);

JSONToken *parser_context_pop_token(JSONParserContext *ctxt);
JSONToken *parser_context_peek_token(JSONParserContext *ctxt);

static int parse_pair(JSONParserContext *ctxt, QDict *dict) {
  QObject *key_obj = NULL;
  QString *key;
  QObject *value;
  JSONToken *peek, *token;

  peek = parser_context_peek_token(ctxt);
  if (peek == NULL) {
    parse_error(ctxt, NULL, "premature EOI");
    goto out;
  }

  key_obj = parse_value(ctxt); /* { dg-bogus "infinite recursion" } */
  key = ((QString *)qobject_check_type(key_obj, QTYPE_QSTRING));
  if (!key) {
    parse_error(ctxt, peek, "key is not a string in object");
    goto out;
  }

  token = parser_context_pop_token(ctxt);
  if (token == NULL) {
    parse_error(ctxt, NULL, "premature EOI");
    goto out;
  }

  if (token->type != JSON_COLON) {
    parse_error(ctxt, token, "missing : in object pair");
    goto out;
  }

  value = parse_value(ctxt);
  if (value == NULL) {
    parse_error(ctxt, token, "Missing value in dict");
    goto out;
  }

  if (qdict_haskey(dict, qstring_get_str(key))) {
    parse_error(ctxt, token, "duplicate key");
    goto out;
  }

  qdict_put_obj(dict, qstring_get_str(key), value);

  qobject_unref(key_obj);
  return 0;

out:
  qobject_unref(key_obj);
  return -1;
}

static QObject *parse_object(JSONParserContext *ctxt) {
  QDict *dict = NULL;
  JSONToken *token, *peek;

  token = parser_context_pop_token(ctxt);

  dict = qdict_new();

  peek = parser_context_peek_token(ctxt);
  if (peek == NULL) {
    parse_error(ctxt, NULL, "premature EOI");
    goto out;
  }

  if (peek->type != JSON_RCURLY) {
    if (parse_pair(ctxt, dict) == -1) {
      goto out;
    }

    token = parser_context_pop_token(ctxt);
    if (token == NULL) {
      parse_error(ctxt, NULL, "premature EOI");
      goto out;
    }

    while (token->type != JSON_RCURLY) {
      if (token->type != JSON_COMMA) {
        parse_error(ctxt, token, "expected separator in dict");
        goto out;
      }

      if (parse_pair(ctxt, dict) == -1) {
        goto out;
      }

      token = parser_context_pop_token(ctxt);
      if (token == NULL) {
        parse_error(ctxt, NULL, "premature EOI");
        goto out;
      }
    }
  } else {
    (void)parser_context_pop_token(ctxt);
  }

  return QOBJECT(dict);

out:
  qobject_unref (dict);
  return NULL;
}

static QObject *parse_array(JSONParserContext *ctxt) {
  QList *list = NULL;
  JSONToken *token, *peek;

  token = parser_context_pop_token(ctxt);

  list = qlist_new();

  peek = parser_context_peek_token(ctxt);
  if (peek == NULL) {
    parse_error(ctxt, NULL, "premature EOI");
    goto out;
  }

  if (peek->type != JSON_RSQUARE) {
    QObject *obj;

    obj = parse_value(ctxt); /* { dg-bogus "infinite recursion" } */
    if (obj == NULL) {
      parse_error(ctxt, token, "expecting value");
      goto out;
    }

    qlist_append_obj(list, obj);

    token = parser_context_pop_token(ctxt);
    if (token == NULL) {
      parse_error(ctxt, NULL, "premature EOI");
      goto out;
    }

    while (token->type != JSON_RSQUARE) {
      if (token->type != JSON_COMMA) {
        parse_error(ctxt, token, "expected separator in list");
        goto out;
      }

      obj = parse_value(ctxt);
      if (obj == NULL) {
        parse_error(ctxt, token, "expecting value");
        goto out;
      }

      qlist_append_obj(list, obj);

      token = parser_context_pop_token(ctxt);
      if (token == NULL) {
        parse_error(ctxt, NULL, "premature EOI");
        goto out;
      }
    }
  } else {
    (void)parser_context_pop_token(ctxt);
  }

  return QOBJECT(list);

out:
  qobject_unref(list);
  return NULL;
}

QObject *parse_keyword(JSONParserContext *ctxt);
QObject *parse_literal(JSONParserContext *ctxt);

QObject *parse_value(JSONParserContext *ctxt) {
  JSONToken *token;

  token = parser_context_peek_token(ctxt);
  if (token == NULL) {
    parse_error(ctxt, NULL, "premature EOI");
    return NULL;
  }

  switch (token->type) {
  case JSON_LCURLY:
    return parse_object(ctxt); /* { dg-bogus "infinite recursion" } */
  case JSON_LSQUARE:
    return parse_array(ctxt); /* { dg-bogus "infinite recursion" } */
  case JSON_INTEGER:
  case JSON_FLOAT:
  case JSON_STRING:
    return parse_literal(ctxt);
  case JSON_KEYWORD:
    return parse_keyword(ctxt);
  default:
    parse_error(ctxt, token, "expecting value");
    return NULL;
  }
}