summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Santtu Lakkala2025-02-13 08:36:34 +0100
committerGravatar default2025-02-13 08:36:34 +0100
commitaf8a95bbf0b3709d605175da92b93ad1d0aa0bd3 (patch)
treee42906a03f0d8cd45cad46803512b50bafc4956a
parentDon't show the bookmark emoji to strangers. (diff)
downloadpenes-snac2-af8a95bbf0b3709d605175da92b93ad1d0aa0bd3.tar.gz
penes-snac2-af8a95bbf0b3709d605175da92b93ad1d0aa0bd3.tar.xz
penes-snac2-af8a95bbf0b3709d605175da92b93ad1d0aa0bd3.zip
Limit JSON depth
-rw-r--r--snac.h4
-rw-r--r--xs_json.h36
2 files changed, 23 insertions, 17 deletions
diff --git a/snac.h b/snac.h
index 9d349fd..d2d73af 100644
--- a/snac.h
+++ b/snac.h
@@ -16,6 +16,10 @@
16#define MAX_THREADS 256 16#define MAX_THREADS 256
17#endif 17#endif
18 18
19#ifndef MAX_JSON_DEPTH
20#define MAX_JSON_DEPTH 8
21#endif
22
19#ifndef MAX_CONVERSATION_LEVELS 23#ifndef MAX_CONVERSATION_LEVELS
20#define MAX_CONVERSATION_LEVELS 48 24#define MAX_CONVERSATION_LEVELS 48
21#endif 25#endif
diff --git a/xs_json.h b/xs_json.h
index d1b18e4..b9421e4 100644
--- a/xs_json.h
+++ b/xs_json.h
@@ -7,14 +7,16 @@
7int xs_json_dump(const xs_val *data, int indent, FILE *f); 7int xs_json_dump(const xs_val *data, int indent, FILE *f);
8xs_str *xs_json_dumps(const xs_val *data, int indent); 8xs_str *xs_json_dumps(const xs_val *data, int indent);
9 9
10xs_val *xs_json_load(FILE *f); 10xs_val *xs_json_load_full(FILE *f, int maxdepth);
11xs_val *xs_json_loads(const xs_str *json); 11xs_val *xs_json_loads_full(const xs_str *json, int maxdepth);
12#define xs_json_load(f) xs_json_load_full(f, MAX_JSON_DEPTH)
13#define xs_json_loads(s) xs_json_loads_full(s, MAX_JSON_DEPTH)
12 14
13xstype xs_json_load_type(FILE *f); 15xstype xs_json_load_type(FILE *f);
14int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c); 16int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c);
15int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c); 17int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c);
16xs_list *xs_json_load_array(FILE *f); 18xs_list *xs_json_load_array(FILE *f, int maxdepth);
17xs_dict *xs_json_load_object(FILE *f); 19xs_dict *xs_json_load_object(FILE *f, int maxdepth);
18 20
19 21
20#ifdef XS_IMPLEMENTATION 22#ifdef XS_IMPLEMENTATION
@@ -371,7 +373,7 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
371} 373}
372 374
373 375
374xs_list *xs_json_load_array(FILE *f) 376xs_list *xs_json_load_array(FILE *f, int maxdepth)
375/* loads a full JSON array (after the initial OBRACK) */ 377/* loads a full JSON array (after the initial OBRACK) */
376{ 378{
377 xstype t; 379 xstype t;
@@ -387,12 +389,12 @@ xs_list *xs_json_load_array(FILE *f)
387 389
388 if (r == 1) { 390 if (r == 1) {
389 /* partial load? */ 391 /* partial load? */
390 if (v == NULL) { 392 if (v == NULL && maxdepth != 0) {
391 if (t == XSTYPE_LIST) 393 if (t == XSTYPE_LIST)
392 v = xs_json_load_array(f); 394 v = xs_json_load_array(f, maxdepth - 1);
393 else 395 else
394 if (t == XSTYPE_DICT) 396 if (t == XSTYPE_DICT)
395 v = xs_json_load_object(f); 397 v = xs_json_load_object(f, maxdepth - 1);
396 } 398 }
397 399
398 /* still null? fail */ 400 /* still null? fail */
@@ -459,7 +461,7 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt,
459} 461}
460 462
461 463
462xs_dict *xs_json_load_object(FILE *f) 464xs_dict *xs_json_load_object(FILE *f, int maxdepth)
463/* loads a full JSON object (after the initial OCURLY) */ 465/* loads a full JSON object (after the initial OCURLY) */
464{ 466{
465 xstype t; 467 xstype t;
@@ -476,12 +478,12 @@ xs_dict *xs_json_load_object(FILE *f)
476 478
477 if (r == 1) { 479 if (r == 1) {
478 /* partial load? */ 480 /* partial load? */
479 if (v == NULL) { 481 if (v == NULL && maxdepth != 0) {
480 if (t == XSTYPE_LIST) 482 if (t == XSTYPE_LIST)
481 v = xs_json_load_array(f); 483 v = xs_json_load_array(f, maxdepth - 1);
482 else 484 else
483 if (t == XSTYPE_DICT) 485 if (t == XSTYPE_DICT)
484 v = xs_json_load_object(f); 486 v = xs_json_load_object(f, maxdepth - 1);
485 } 487 }
486 488
487 /* still null? fail */ 489 /* still null? fail */
@@ -500,14 +502,14 @@ xs_dict *xs_json_load_object(FILE *f)
500} 502}
501 503
502 504
503xs_val *xs_json_loads(const xs_str *json) 505xs_val *xs_json_loads_full(const xs_str *json, int maxdepth)
504/* loads a string in JSON format and converts to a multiple data */ 506/* loads a string in JSON format and converts to a multiple data */
505{ 507{
506 FILE *f; 508 FILE *f;
507 xs_val *v = NULL; 509 xs_val *v = NULL;
508 510
509 if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) { 511 if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) {
510 v = xs_json_load(f); 512 v = xs_json_load_full(f, maxdepth);
511 fclose(f); 513 fclose(f);
512 } 514 }
513 515
@@ -533,17 +535,17 @@ xstype xs_json_load_type(FILE *f)
533} 535}
534 536
535 537
536xs_val *xs_json_load(FILE *f) 538xs_val *xs_json_load_full(FILE *f, int maxdepth)
537/* loads a JSON file */ 539/* loads a JSON file */
538{ 540{
539 xs_val *v = NULL; 541 xs_val *v = NULL;
540 xstype t = xs_json_load_type(f); 542 xstype t = xs_json_load_type(f);
541 543
542 if (t == XSTYPE_LIST) 544 if (t == XSTYPE_LIST)
543 v = xs_json_load_array(f); 545 v = xs_json_load_array(f, maxdepth);
544 else 546 else
545 if (t == XSTYPE_DICT) 547 if (t == XSTYPE_DICT)
546 v = xs_json_load_object(f); 548 v = xs_json_load_object(f, maxdepth);
547 549
548 return v; 550 return v;
549} 551}