MCPcopy Create free account
hub / github.com/ElementsProject/lightning / test_json_deep_nesting

Function test_json_deep_nesting

common/test/run-json.c:229–270  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

227}
228
229static void test_json_deep_nesting(void)
230{
231 char *buf;
232 size_t d, i;
233
234 /* Arrays nested exactly at the limit still parse. */
235 d = JSON_MAX_NESTING;
236 buf = tal_arr(tmpctx, char, 2 * d + 2);
237 memset(buf, '[', d);
238 buf[d] = '0';
239 memset(buf + d + 1, ']', d);
240 buf[2 * d + 1] = '\0';
241 assert(json_parse_simple(tmpctx, buf, 2 * d + 1));
242
243 /* One level deeper is rejected, not crashed. */
244 d = JSON_MAX_NESTING + 1;
245 buf = tal_arr(tmpctx, char, 2 * d + 2);
246 memset(buf, '[', d);
247 buf[d] = '0';
248 memset(buf + d + 1, ']', d);
249 buf[2 * d + 1] = '\0';
250 assert(!json_parse_simple(tmpctx, buf, 2 * d + 1));
251
252 /* A pathologically deep array is rejected iteratively, without
253 * overflowing the stack. */
254 d = 100000;
255 buf = tal_arr(tmpctx, char, 2 * d + 2);
256 memset(buf, '[', d);
257 buf[d] = '0';
258 memset(buf + d + 1, ']', d);
259 buf[2 * d + 1] = '\0';
260 assert(!json_parse_simple(tmpctx, buf, 2 * d + 1));
261
262 /* Same for deeply nested objects. */
263 buf = tal_strdup(tmpctx, "");
264 for (i = 0; i < 100000; i++)
265 tal_append_fmt(&buf, "{\"a\":");
266 tal_append_fmt(&buf, "1");
267 for (i = 0; i < 100000; i++)
268 tal_append_fmt(&buf, "}");
269 assert(!json_parse_simple(tmpctx, buf, strlen(buf)));
270}
271
272static void do_json_to_s64(const char *val, bool ok, s64 expected)
273{

Callers 1

mainFunction · 0.85

Calls 2

json_parse_simpleFunction · 0.85
tal_append_fmtFunction · 0.85

Tested by

no test coverage detected