1414
1515#include " simdjson/jsonformatutils.h"
1616
17- #define JSONVALUEMASK 0xFFFFFFFFFFFFFF ;
17+ #define JSONVALUEMASK 0xFFFFFFFFFFFFFF
18+
19+ #define DEFAULTMAXDEPTH 1024 // a JSON document with a depth exceeding 1024 is probably de facto invalid
1820
1921struct ParsedJson {
2022public:
21- size_t bytecapacity; // indicates how many bits are meant to be supported by
22- // structurals
23- size_t depthcapacity; // how deep we can go
24- size_t tapecapacity;
25- size_t stringcapacity;
26- u32 current_loc;
27- u8 *structurals;
28- u32 n_structural_indexes;
29- u32 *structural_indexes;
30-
31- u64 *tape;
32- u32 *containing_scope_offset;
33- void **ret_address;
34-
35- u8 *string_buf; // should be at least bytecapacity
36- u8 *current_string_buf_loc;
3723
3824 // create a ParsedJson container with zero capacity, call allocateCapacity to
3925 // allocate memory
4026 ParsedJson ()
4127 : bytecapacity(0 ), depthcapacity(0 ), tapecapacity(0 ), stringcapacity(0 ),
4228 current_loc (0 ), structurals(NULL ), n_structural_indexes(0 ),
4329 structural_indexes(NULL ), tape(NULL ), containing_scope_offset(NULL ),
44- ret_address(NULL ), string_buf(NULL ), current_string_buf_loc(NULL ) {}
30+ ret_address(NULL ), string_buf(NULL ), current_string_buf_loc(NULL ), isvalid( false ) {}
4531
4632 // if needed, allocate memory so that the object is able to process JSON
4733 // documents having up to len butes and maxdepth "depth"
4834 WARN_UNUSED
49- inline bool allocateCapacity (size_t len, size_t maxdepth) {
35+ inline bool allocateCapacity (size_t len, size_t maxdepth = DEFAULTMAXDEPTH ) {
5036 if ((maxdepth == 0 ) || (len == 0 )) {
5137 std::cerr << " capacities must be non-zero " << std::endl;
5238 return false ;
@@ -56,6 +42,7 @@ struct ParsedJson {
5642 return true ;
5743 deallocate ();
5844 }
45+ isvalid = false ;
5946 bytecapacity = 0 ; // will only set it to len after allocations are a success
6047 if (posix_memalign ((void **)&structurals, 8 , ROUNDUP_N (len, 64 ) / 8 )) {
6148 std::cerr << " Could not allocate memory for structurals" << std::endl;
@@ -97,6 +84,10 @@ struct ParsedJson {
9784 return true ;
9885 }
9986
87+ bool isValid () const {
88+ return isvalid;
89+ }
90+
10091 // deallocate memory and set capacity to zero, called automatically by the
10192 // destructor
10293 void deallocate () {
@@ -110,6 +101,7 @@ struct ParsedJson {
110101 delete[] string_buf;
111102 delete[] structural_indexes;
112103 free (structurals);
104+ isvalid = false ;
113105 }
114106
115107 ~ParsedJson () { deallocate (); }
@@ -118,13 +110,15 @@ struct ParsedJson {
118110 void init () {
119111 current_string_buf_loc = string_buf;
120112 current_loc = 0 ;
113+ isvalid = false ;
121114 }
122115
123116 // print the json to stdout (should be valid)
124117 // return false if the tape is likely wrong (e.g., you did not parse a valid
125118 // JSON).
126119 WARN_UNUSED
127120 bool printjson () {
121+ if (!isvalid) return false ;
128122 size_t tapeidx = 0 ;
129123 u64 tape_val = tape[tapeidx];
130124 u8 type = (tape_val >> 56 );
@@ -227,6 +221,7 @@ struct ParsedJson {
227221
228222 WARN_UNUSED
229223 bool dump_raw_tape () {
224+ if (!isvalid) return false ;
230225 size_t tapeidx = 0 ;
231226 u64 tape_val = tape[tapeidx++];
232227 u8 type = (tape_val >> 56 );
@@ -374,6 +369,32 @@ struct ParsedJson {
374369 };
375370
376371#endif
372+
373+ size_t bytecapacity; // indicates how many bits are meant to be supported by
374+ // structurals
375+
376+ size_t depthcapacity; // how deep we can go
377+ size_t tapecapacity;
378+ size_t stringcapacity;
379+ u32 current_loc;
380+ u8 *structurals;
381+ u32 n_structural_indexes;
382+
383+ u32 *structural_indexes;
384+
385+ u64 *tape;
386+ u32 *containing_scope_offset;
387+ void **ret_address;
388+
389+ u8 *string_buf; // should be at least bytecapacity
390+ u8 *current_string_buf_loc;
391+ bool isvalid;
392+ ParsedJson (const ParsedJson && p); // we don't want the default constructor to be called
393+
394+ private :
395+ ParsedJson (const ParsedJson & p); // we don't want the default constructor to be called
396+
397+
377398};
378399
379400#ifdef DEBUG
0 commit comments