commit af8a95b

Santtu Lakkala  ·  2025-02-13 07:36:34 +0000 UTC
parent 0980302
Limit JSON depth
2 files changed,  +23, -17
M snac.h
M snac.h
+4, -0
 1@@ -16,6 +16,10 @@
 2 #define MAX_THREADS 256
 3 #endif
 4 
 5+#ifndef MAX_JSON_DEPTH
 6+#define MAX_JSON_DEPTH 8
 7+#endif
 8+
 9 #ifndef MAX_CONVERSATION_LEVELS
10 #define MAX_CONVERSATION_LEVELS 48
11 #endif
+19, -17
  1@@ -7,14 +7,16 @@
  2 int xs_json_dump(const xs_val *data, int indent, FILE *f);
  3 xs_str *xs_json_dumps(const xs_val *data, int indent);
  4 
  5-xs_val *xs_json_load(FILE *f);
  6-xs_val *xs_json_loads(const xs_str *json);
  7+xs_val *xs_json_load_full(FILE *f, int maxdepth);
  8+xs_val *xs_json_loads_full(const xs_str *json, int maxdepth);
  9+#define xs_json_load(f) xs_json_load_full(f, MAX_JSON_DEPTH)
 10+#define xs_json_loads(s) xs_json_loads_full(s, MAX_JSON_DEPTH)
 11 
 12 xstype xs_json_load_type(FILE *f);
 13 int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c);
 14 int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt, int *c);
 15-xs_list *xs_json_load_array(FILE *f);
 16-xs_dict *xs_json_load_object(FILE *f);
 17+xs_list *xs_json_load_array(FILE *f, int maxdepth);
 18+xs_dict *xs_json_load_object(FILE *f, int maxdepth);
 19 
 20 
 21 #ifdef XS_IMPLEMENTATION
 22@@ -371,7 +373,7 @@ int xs_json_load_array_iter(FILE *f, xs_val **value, xstype *pt, int *c)
 23 }
 24 
 25 
 26-xs_list *xs_json_load_array(FILE *f)
 27+xs_list *xs_json_load_array(FILE *f, int maxdepth)
 28 /* loads a full JSON array (after the initial OBRACK) */
 29 {
 30     xstype t;
 31@@ -387,12 +389,12 @@ xs_list *xs_json_load_array(FILE *f)
 32 
 33         if (r == 1) {
 34             /* partial load? */
 35-            if (v == NULL) {
 36+            if (v == NULL && maxdepth != 0) {
 37                 if (t == XSTYPE_LIST)
 38-                    v = xs_json_load_array(f);
 39+                    v = xs_json_load_array(f, maxdepth - 1);
 40                 else
 41                 if (t == XSTYPE_DICT)
 42-                    v = xs_json_load_object(f);
 43+                    v = xs_json_load_object(f, maxdepth - 1);
 44             }
 45 
 46             /* still null? fail */
 47@@ -459,7 +461,7 @@ int xs_json_load_object_iter(FILE *f, xs_str **key, xs_val **value, xstype *pt,
 48 }
 49 
 50 
 51-xs_dict *xs_json_load_object(FILE *f)
 52+xs_dict *xs_json_load_object(FILE *f, int maxdepth)
 53 /* loads a full JSON object (after the initial OCURLY) */
 54 {
 55     xstype t;
 56@@ -476,12 +478,12 @@ xs_dict *xs_json_load_object(FILE *f)
 57 
 58         if (r == 1) {
 59             /* partial load? */
 60-            if (v == NULL) {
 61+            if (v == NULL && maxdepth != 0) {
 62                 if (t == XSTYPE_LIST)
 63-                    v = xs_json_load_array(f);
 64+                    v = xs_json_load_array(f, maxdepth - 1);
 65                 else
 66                 if (t == XSTYPE_DICT)
 67-                    v = xs_json_load_object(f);
 68+                    v = xs_json_load_object(f, maxdepth - 1);
 69             }
 70 
 71             /* still null? fail */
 72@@ -500,14 +502,14 @@ xs_dict *xs_json_load_object(FILE *f)
 73 }
 74 
 75 
 76-xs_val *xs_json_loads(const xs_str *json)
 77+xs_val *xs_json_loads_full(const xs_str *json, int maxdepth)
 78 /* loads a string in JSON format and converts to a multiple data */
 79 {
 80     FILE *f;
 81     xs_val *v = NULL;
 82 
 83     if ((f = fmemopen((char *)json, strlen(json), "r")) != NULL) {
 84-        v = xs_json_load(f);
 85+        v = xs_json_load_full(f, maxdepth);
 86         fclose(f);
 87     }
 88 
 89@@ -533,17 +535,17 @@ xstype xs_json_load_type(FILE *f)
 90 }
 91 
 92 
 93-xs_val *xs_json_load(FILE *f)
 94+xs_val *xs_json_load_full(FILE *f, int maxdepth)
 95 /* loads a JSON file */
 96 {
 97     xs_val *v = NULL;
 98     xstype t = xs_json_load_type(f);
 99 
100     if (t == XSTYPE_LIST)
101-        v = xs_json_load_array(f);
102+        v = xs_json_load_array(f, maxdepth);
103     else
104     if (t == XSTYPE_DICT)
105-        v = xs_json_load_object(f);
106+        v = xs_json_load_object(f, maxdepth);
107 
108     return v;
109 }