oj 3.16.11 → 3.17.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +133 -0
- data/README.md +0 -16
- data/ext/oj/cache.c +17 -1
- data/ext/oj/cache.h +5 -0
- data/ext/oj/circarray.c +6 -2
- data/ext/oj/compat.c +5 -5
- data/ext/oj/custom.c +12 -5
- data/ext/oj/dump.c +433 -206
- data/ext/oj/dump.h +12 -0
- data/ext/oj/dump_compat.c +17 -5
- data/ext/oj/dump_object.c +25 -10
- data/ext/oj/dump_strict.c +13 -8
- data/ext/oj/extconf.rb +11 -7
- data/ext/oj/fast.c +123 -60
- data/ext/oj/intern.c +7 -2
- data/ext/oj/mimic_json.c +3 -0
- data/ext/oj/object.c +26 -15
- data/ext/oj/odd.c +6 -2
- data/ext/oj/oj.c +511 -22
- data/ext/oj/oj.h +61 -53
- data/ext/oj/parse.c +229 -30
- data/ext/oj/parse.h +0 -1
- data/ext/oj/parser.c +226 -65
- data/ext/oj/rails.c +197 -57
- data/ext/oj/reader.c +18 -6
- data/ext/oj/resolve.c +11 -2
- data/ext/oj/rxclass.c +17 -1
- data/ext/oj/rxclass.h +2 -1
- data/ext/oj/safe.c +244 -0
- data/ext/oj/safe.h +91 -0
- data/ext/oj/saj.c +56 -12
- data/ext/oj/simd.h +210 -1
- data/ext/oj/sparse.c +22 -9
- data/ext/oj/stream_writer.c +16 -1
- data/ext/oj/string_writer.c +23 -7
- data/ext/oj/usual.c +23 -6
- data/ext/oj/util.c +27 -0
- data/ext/oj/util.h +2 -1
- data/ext/oj/val_stack.h +24 -5
- data/ext/oj/wab.c +15 -27
- data/lib/oj/version.rb +1 -1
- data/pages/Compatibility.md +1 -1
- data/pages/Custom.md +2 -3
- data/pages/InstallOptions.md +8 -6
- data/pages/Modes.md +1 -1
- data/pages/Options.md +5 -6
- data/pages/Parser.md +2 -2
- data/pages/Rails.md +6 -0
- data/pages/Security.md +21 -7
- metadata +21 -19
data/ext/oj/oj.h
CHANGED
|
@@ -104,61 +104,64 @@ typedef enum {
|
|
|
104
104
|
} StreamWriterType;
|
|
105
105
|
|
|
106
106
|
typedef struct _dumpOpts {
|
|
107
|
-
bool
|
|
108
|
-
char
|
|
109
|
-
char
|
|
110
|
-
char
|
|
111
|
-
char
|
|
112
|
-
char
|
|
113
|
-
uint8_t
|
|
114
|
-
uint8_t
|
|
115
|
-
uint8_t
|
|
116
|
-
uint8_t
|
|
117
|
-
uint8_t
|
|
118
|
-
char
|
|
119
|
-
bool
|
|
120
|
-
bool
|
|
121
|
-
int
|
|
107
|
+
bool use;
|
|
108
|
+
char indent_str[16];
|
|
109
|
+
char before_sep[16];
|
|
110
|
+
char after_sep[16];
|
|
111
|
+
char hash_nl[16];
|
|
112
|
+
char array_nl[16];
|
|
113
|
+
uint8_t indent_size;
|
|
114
|
+
uint8_t before_size;
|
|
115
|
+
uint8_t after_size;
|
|
116
|
+
uint8_t hash_size;
|
|
117
|
+
uint8_t array_size;
|
|
118
|
+
char nan_dump; // NanDump
|
|
119
|
+
bool omit_nil;
|
|
120
|
+
bool omit_null_byte;
|
|
121
|
+
int max_depth;
|
|
122
|
+
const char *only;
|
|
123
|
+
const char *except;
|
|
122
124
|
} *DumpOpts;
|
|
123
125
|
|
|
124
126
|
typedef struct _options {
|
|
125
|
-
int indent;
|
|
126
|
-
char circular;
|
|
127
|
-
char auto_define;
|
|
128
|
-
char sym_key;
|
|
129
|
-
char escape_mode;
|
|
130
|
-
char mode;
|
|
131
|
-
char class_cache;
|
|
132
|
-
char time_format;
|
|
133
|
-
char bigdec_as_num;
|
|
134
|
-
char bigdec_load;
|
|
135
|
-
char compat_bigdec;
|
|
136
|
-
char to_hash;
|
|
137
|
-
char to_json;
|
|
138
|
-
char as_json;
|
|
139
|
-
char raw_json;
|
|
140
|
-
char nilnil;
|
|
141
|
-
char empty_string;
|
|
142
|
-
char allow_gc;
|
|
143
|
-
char quirks_mode;
|
|
144
|
-
char allow_invalid;
|
|
145
|
-
char create_ok;
|
|
146
|
-
char allow_nan;
|
|
147
|
-
char trace;
|
|
148
|
-
char safe;
|
|
149
|
-
char sec_prec_set;
|
|
150
|
-
char ignore_under;
|
|
151
|
-
char cache_keys;
|
|
152
|
-
char cache_str;
|
|
153
|
-
int64_t int_range_min;
|
|
154
|
-
int64_t int_range_max;
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
char
|
|
160
|
-
|
|
161
|
-
VALUE
|
|
127
|
+
int indent; // indention for dump, default 2
|
|
128
|
+
char circular; // YesNo
|
|
129
|
+
char auto_define; // YesNo
|
|
130
|
+
char sym_key; // YesNo
|
|
131
|
+
char escape_mode; // Escape_Mode
|
|
132
|
+
char mode; // Mode
|
|
133
|
+
char class_cache; // YesNo
|
|
134
|
+
char time_format; // TimeFormat
|
|
135
|
+
char bigdec_as_num; // YesNo
|
|
136
|
+
char bigdec_load; // BigLoad
|
|
137
|
+
char compat_bigdec; // boolean (0 or 1)
|
|
138
|
+
char to_hash; // YesNo
|
|
139
|
+
char to_json; // YesNo
|
|
140
|
+
char as_json; // YesNo
|
|
141
|
+
char raw_json; // YesNo
|
|
142
|
+
char nilnil; // YesNo
|
|
143
|
+
char empty_string; // YesNo
|
|
144
|
+
char allow_gc; // allow GC during parse
|
|
145
|
+
char quirks_mode; // allow single JSON values instead of documents
|
|
146
|
+
char allow_invalid; // YesNo - allow invalid unicode
|
|
147
|
+
char create_ok; // YesNo allow create_id
|
|
148
|
+
char allow_nan; // YesNo for parsing only
|
|
149
|
+
char trace; // YesNo
|
|
150
|
+
char safe; // YesNo
|
|
151
|
+
char sec_prec_set; // boolean (0 or 1)
|
|
152
|
+
char ignore_under; // YesNo - ignore attrs starting with _ if true in object and custom modes
|
|
153
|
+
char cache_keys; // YesNo
|
|
154
|
+
char cache_str; // string short than or equal to this are cache
|
|
155
|
+
int64_t int_range_min; // dump numbers below as string
|
|
156
|
+
int64_t int_range_max; // dump numbers above as string
|
|
157
|
+
size_t max_integer_digits; // 0 = unlimited; max decimal digits for parsed integers
|
|
158
|
+
const char *create_id; // 0 or string
|
|
159
|
+
size_t create_id_len; // length of create_id
|
|
160
|
+
int sec_prec; // second precision when dumping time
|
|
161
|
+
char float_prec; // float precision, linked to float_fmt
|
|
162
|
+
char float_fmt[7]; // float format for dumping, if empty use Ruby
|
|
163
|
+
VALUE hash_class; // class to use in place of Hash on load
|
|
164
|
+
VALUE array_class; // class to use in place of Array on load
|
|
162
165
|
struct _dumpOpts dump_opts;
|
|
163
166
|
struct _rxClass str_rx;
|
|
164
167
|
VALUE *ignore; // Qnil terminated array of classes or NULL
|
|
@@ -194,6 +197,7 @@ typedef struct _out {
|
|
|
194
197
|
bool allocated;
|
|
195
198
|
bool omit_nil;
|
|
196
199
|
bool omit_null_byte;
|
|
200
|
+
bool key_filter_off; // rails: suppress the :only/:except key filter for an as_json result
|
|
197
201
|
int argc;
|
|
198
202
|
VALUE *argv;
|
|
199
203
|
ROptTable ropts;
|
|
@@ -253,6 +257,11 @@ extern VALUE oj_custom_parse_cstr(int argc, VALUE *argv, char *json, size_t len)
|
|
|
253
257
|
|
|
254
258
|
extern bool oj_hash_has_key(VALUE hash, VALUE key);
|
|
255
259
|
extern void oj_parse_options(VALUE ropts, Options copts);
|
|
260
|
+
extern bool oj_parse_options_consumed(VALUE ropts, Options copts);
|
|
261
|
+
extern void oj_free_call_options(Options copts);
|
|
262
|
+
extern void oj_options_take_ownership(Options copts);
|
|
263
|
+
extern void oj_options_release(Options copts);
|
|
264
|
+
extern void oj_options_mark(Options copts);
|
|
256
265
|
|
|
257
266
|
extern void oj_dump_obj_to_json(VALUE obj, Options copts, Out out);
|
|
258
267
|
extern void oj_dump_obj_to_json_using_params(VALUE obj, Options copts, Out out, int argc, VALUE *argv);
|
|
@@ -318,7 +327,6 @@ extern VALUE oj_max_nesting_sym;
|
|
|
318
327
|
extern VALUE oj_object_class_sym;
|
|
319
328
|
extern VALUE oj_object_nl_sym;
|
|
320
329
|
extern VALUE oj_quirks_mode_sym;
|
|
321
|
-
extern VALUE oj_skip_null_byte_sym;
|
|
322
330
|
extern VALUE oj_space_before_sym;
|
|
323
331
|
extern VALUE oj_space_sym;
|
|
324
332
|
extern VALUE oj_symbolize_names_sym;
|
data/ext/oj/parse.c
CHANGED
|
@@ -15,12 +15,9 @@
|
|
|
15
15
|
#include "mem.h"
|
|
16
16
|
#include "oj.h"
|
|
17
17
|
#include "rxclass.h"
|
|
18
|
+
#include "simd.h"
|
|
18
19
|
#include "val_stack.h"
|
|
19
20
|
|
|
20
|
-
#ifdef OJ_USE_SSE4_2
|
|
21
|
-
#include <nmmintrin.h>
|
|
22
|
-
#endif
|
|
23
|
-
|
|
24
21
|
// Workaround in case INFINITY is not defined in math.h or if the OS is CentOS
|
|
25
22
|
#define OJ_INFINITY (1.0 / 0.0)
|
|
26
23
|
|
|
@@ -202,23 +199,170 @@ static inline const char *scan_string_noSIMD(const char *str, const char *end) {
|
|
|
202
199
|
return str;
|
|
203
200
|
}
|
|
204
201
|
|
|
205
|
-
#ifdef
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const
|
|
209
|
-
const
|
|
202
|
+
#ifdef HAVE_SIMD_NEON
|
|
203
|
+
|
|
204
|
+
static inline const char *string_scan_neon(const char *str, const char *end) {
|
|
205
|
+
const uint8x16_t null_char = vdupq_n_u8(0);
|
|
206
|
+
const uint8x16_t backslash = vdupq_n_u8('\\');
|
|
207
|
+
const uint8x16_t quote = vdupq_n_u8('"');
|
|
208
|
+
|
|
209
|
+
while (str + sizeof(uint8x16_t) <= end) {
|
|
210
|
+
uint8x16_t chunk = vld1q_u8((const uint8_t *)str);
|
|
211
|
+
uint8x16_t tmp = vorrq_u8(vorrq_u8(vceqq_u8(chunk, null_char), vceqq_u8(chunk, backslash)),
|
|
212
|
+
vceqq_u8(chunk, quote));
|
|
213
|
+
const uint8x8_t res = vshrn_n_u16(vreinterpretq_u16_u8(tmp), 4);
|
|
214
|
+
uint64_t mask = vget_lane_u64(vreinterpret_u64_u8(res), 0);
|
|
215
|
+
if (mask != 0) {
|
|
216
|
+
mask &= 0x8888888888888888ull;
|
|
217
|
+
return str + (OJ_CTZ64(mask) >> 2);
|
|
218
|
+
}
|
|
219
|
+
str += sizeof(uint8x16_t);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return scan_string_noSIMD(str, end);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
#endif
|
|
210
226
|
|
|
211
|
-
|
|
227
|
+
#ifdef HAVE_SIMD_SSE4_2
|
|
228
|
+
// Optimized SIMD string scanner using SSE4.2 instructions
|
|
229
|
+
// Uses prefetching and processes multiple chunks in parallel to reduce latency
|
|
230
|
+
// Note: OJ_TARGET_SSE42 attribute allows this to compile even without global -msse4.2
|
|
231
|
+
static OJ_TARGET_SSE42 const char *scan_string_SSE42(const char *str, const char *end) {
|
|
232
|
+
static const char chars[16] = "\x00\\\"";
|
|
233
|
+
const __m128i terminate = _mm_loadu_si128((const __m128i *)&chars[0]);
|
|
234
|
+
const char *safe_end_64 = end - 64;
|
|
235
|
+
const char *safe_end_16 = end - 16;
|
|
236
|
+
|
|
237
|
+
// Process 64 bytes at a time with parallel SIMD operations
|
|
238
|
+
// This reduces pipeline stalls and improves instruction-level parallelism
|
|
239
|
+
while (str <= safe_end_64) {
|
|
240
|
+
// Prefetch next cache line for better memory throughput
|
|
241
|
+
OJ_PREFETCH(str + 64);
|
|
242
|
+
|
|
243
|
+
// Load and compare 4 chunks in parallel
|
|
244
|
+
const __m128i chunk0 = _mm_loadu_si128((const __m128i *)(str));
|
|
245
|
+
const __m128i chunk1 = _mm_loadu_si128((const __m128i *)(str + 16));
|
|
246
|
+
const __m128i chunk2 = _mm_loadu_si128((const __m128i *)(str + 32));
|
|
247
|
+
const __m128i chunk3 = _mm_loadu_si128((const __m128i *)(str + 48));
|
|
248
|
+
|
|
249
|
+
const int r0 = _mm_cmpestri(terminate,
|
|
250
|
+
3,
|
|
251
|
+
chunk0,
|
|
252
|
+
16,
|
|
253
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
254
|
+
if (OJ_UNLIKELY(r0 != 16))
|
|
255
|
+
return str + r0;
|
|
256
|
+
|
|
257
|
+
const int r1 = _mm_cmpestri(terminate,
|
|
258
|
+
3,
|
|
259
|
+
chunk1,
|
|
260
|
+
16,
|
|
261
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
262
|
+
if (OJ_UNLIKELY(r1 != 16))
|
|
263
|
+
return str + 16 + r1;
|
|
264
|
+
|
|
265
|
+
const int r2 = _mm_cmpestri(terminate,
|
|
266
|
+
3,
|
|
267
|
+
chunk2,
|
|
268
|
+
16,
|
|
269
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
270
|
+
if (OJ_UNLIKELY(r2 != 16))
|
|
271
|
+
return str + 32 + r2;
|
|
272
|
+
|
|
273
|
+
const int r3 = _mm_cmpestri(terminate,
|
|
274
|
+
3,
|
|
275
|
+
chunk3,
|
|
276
|
+
16,
|
|
277
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
278
|
+
if (OJ_UNLIKELY(r3 != 16))
|
|
279
|
+
return str + 48 + r3;
|
|
280
|
+
|
|
281
|
+
str += 64;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Handle remaining 16-byte chunks
|
|
285
|
+
for (; str <= safe_end_16; str += 16) {
|
|
212
286
|
const __m128i string = _mm_loadu_si128((const __m128i *)str);
|
|
213
287
|
const int r = _mm_cmpestri(terminate,
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
if (r != 16)
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
288
|
+
3,
|
|
289
|
+
string,
|
|
290
|
+
16,
|
|
291
|
+
_SIDD_UBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_LEAST_SIGNIFICANT);
|
|
292
|
+
if (r != 16)
|
|
293
|
+
return str + r;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
return scan_string_noSIMD(str, end);
|
|
297
|
+
}
|
|
298
|
+
#endif
|
|
299
|
+
|
|
300
|
+
#ifdef HAVE_SIMD_SSE2
|
|
301
|
+
// Optimized SSE2 string scanner (fallback for older x86_64 CPUs)
|
|
302
|
+
// Uses SSE2 instructions with prefetching and parallel processing
|
|
303
|
+
// Note: OJ_TARGET_SSE2 attribute allows this to compile even without global -msse2
|
|
304
|
+
static OJ_TARGET_SSE2 const char *scan_string_SSE2(const char *str, const char *end) {
|
|
305
|
+
const char *safe_end_64 = end - 64;
|
|
306
|
+
const char *safe_end_16 = end - 16;
|
|
307
|
+
|
|
308
|
+
// Create comparison vectors for our three special characters
|
|
309
|
+
const __m128i null_char = _mm_setzero_si128();
|
|
310
|
+
const __m128i backslash = _mm_set1_epi8('\\');
|
|
311
|
+
const __m128i quote = _mm_set1_epi8('"');
|
|
312
|
+
|
|
313
|
+
// Process 64 bytes at a time for better throughput
|
|
314
|
+
while (str <= safe_end_64) {
|
|
315
|
+
OJ_PREFETCH(str + 64);
|
|
316
|
+
|
|
317
|
+
// Load 4 chunks
|
|
318
|
+
const __m128i chunk0 = _mm_loadu_si128((const __m128i *)(str));
|
|
319
|
+
const __m128i chunk1 = _mm_loadu_si128((const __m128i *)(str + 16));
|
|
320
|
+
const __m128i chunk2 = _mm_loadu_si128((const __m128i *)(str + 32));
|
|
321
|
+
const __m128i chunk3 = _mm_loadu_si128((const __m128i *)(str + 48));
|
|
322
|
+
|
|
323
|
+
// Compare all chunks (allows CPU to parallelize)
|
|
324
|
+
const __m128i cmp0 = _mm_or_si128(
|
|
325
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk0, null_char), _mm_cmpeq_epi8(chunk0, backslash)),
|
|
326
|
+
_mm_cmpeq_epi8(chunk0, quote));
|
|
327
|
+
const __m128i cmp1 = _mm_or_si128(
|
|
328
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk1, null_char), _mm_cmpeq_epi8(chunk1, backslash)),
|
|
329
|
+
_mm_cmpeq_epi8(chunk1, quote));
|
|
330
|
+
const __m128i cmp2 = _mm_or_si128(
|
|
331
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk2, null_char), _mm_cmpeq_epi8(chunk2, backslash)),
|
|
332
|
+
_mm_cmpeq_epi8(chunk2, quote));
|
|
333
|
+
const __m128i cmp3 = _mm_or_si128(
|
|
334
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk3, null_char), _mm_cmpeq_epi8(chunk3, backslash)),
|
|
335
|
+
_mm_cmpeq_epi8(chunk3, quote));
|
|
336
|
+
|
|
337
|
+
// Convert to masks
|
|
338
|
+
int mask0 = _mm_movemask_epi8(cmp0);
|
|
339
|
+
if (OJ_UNLIKELY(mask0 != 0))
|
|
340
|
+
return str + OJ_CTZ(mask0);
|
|
341
|
+
|
|
342
|
+
int mask1 = _mm_movemask_epi8(cmp1);
|
|
343
|
+
if (OJ_UNLIKELY(mask1 != 0))
|
|
344
|
+
return str + 16 + OJ_CTZ(mask1);
|
|
345
|
+
|
|
346
|
+
int mask2 = _mm_movemask_epi8(cmp2);
|
|
347
|
+
if (OJ_UNLIKELY(mask2 != 0))
|
|
348
|
+
return str + 32 + OJ_CTZ(mask2);
|
|
349
|
+
|
|
350
|
+
int mask3 = _mm_movemask_epi8(cmp3);
|
|
351
|
+
if (OJ_UNLIKELY(mask3 != 0))
|
|
352
|
+
return str + 48 + OJ_CTZ(mask3);
|
|
353
|
+
|
|
354
|
+
str += 64;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
// Handle remaining 16-byte chunks
|
|
358
|
+
for (; str <= safe_end_16; str += 16) {
|
|
359
|
+
const __m128i chunk = _mm_loadu_si128((const __m128i *)str);
|
|
360
|
+
const __m128i matches = _mm_or_si128(
|
|
361
|
+
_mm_or_si128(_mm_cmpeq_epi8(chunk, null_char), _mm_cmpeq_epi8(chunk, backslash)),
|
|
362
|
+
_mm_cmpeq_epi8(chunk, quote));
|
|
363
|
+
int mask = _mm_movemask_epi8(matches);
|
|
364
|
+
if (mask != 0)
|
|
365
|
+
return str + OJ_CTZ(mask);
|
|
222
366
|
}
|
|
223
367
|
|
|
224
368
|
return scan_string_noSIMD(str, end);
|
|
@@ -228,16 +372,29 @@ static inline const char *scan_string_SIMD(const char *str, const char *end) {
|
|
|
228
372
|
static const char *(*scan_func)(const char *str, const char *end) = scan_string_noSIMD;
|
|
229
373
|
|
|
230
374
|
void oj_scanner_init(void) {
|
|
231
|
-
|
|
232
|
-
|
|
375
|
+
// Use runtime CPU detection to select the best SIMD implementation
|
|
376
|
+
// This ensures we don't crash on CPUs that don't support SSE4.2
|
|
377
|
+
SIMD_Implementation impl = oj_get_simd_implementation();
|
|
378
|
+
|
|
379
|
+
switch (impl) {
|
|
380
|
+
#ifdef HAVE_SIMD_SSE4_2
|
|
381
|
+
case SIMD_SSE42: scan_func = scan_string_SSE42; break;
|
|
382
|
+
#endif
|
|
383
|
+
#ifdef HAVE_SIMD_SSE2
|
|
384
|
+
case SIMD_SSE2: scan_func = scan_string_SSE2; break;
|
|
233
385
|
#endif
|
|
386
|
+
#ifdef HAVE_SIMD_NEON
|
|
387
|
+
case SIMD_NEON: scan_func = string_scan_neon; break;
|
|
388
|
+
#endif
|
|
389
|
+
default: scan_func = scan_string_noSIMD; break;
|
|
390
|
+
}
|
|
234
391
|
}
|
|
235
392
|
|
|
236
393
|
// entered at /
|
|
237
394
|
static void read_escaped_str(ParseInfo pi, const char *start) {
|
|
238
395
|
struct _buf buf;
|
|
239
396
|
const char *s;
|
|
240
|
-
|
|
397
|
+
size_t cnt = pi->cur - start;
|
|
241
398
|
uint32_t code;
|
|
242
399
|
Val parent = stack_peek(&pi->stack);
|
|
243
400
|
|
|
@@ -257,6 +414,11 @@ static void read_escaped_str(ParseInfo pi, const char *start) {
|
|
|
257
414
|
|
|
258
415
|
if ('\\' == *s) {
|
|
259
416
|
s++;
|
|
417
|
+
if (pi->end <= s) {
|
|
418
|
+
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "quoted string not terminated");
|
|
419
|
+
buf_cleanup(&buf);
|
|
420
|
+
return;
|
|
421
|
+
}
|
|
260
422
|
switch (*s) {
|
|
261
423
|
case 'n': buf_append(&buf, '\n'); break;
|
|
262
424
|
case 'r': buf_append(&buf, '\r'); break;
|
|
@@ -454,7 +616,7 @@ static void read_num(ParseInfo pi) {
|
|
|
454
616
|
ni.bigdec_load = pi->options.compat_bigdec;
|
|
455
617
|
} else {
|
|
456
618
|
ni.no_big = (FloatDec == pi->options.bigdec_load || FastDec == pi->options.bigdec_load ||
|
|
457
|
-
|
|
619
|
+
RubyDec == pi->options.bigdec_load);
|
|
458
620
|
ni.bigdec_load = pi->options.bigdec_load;
|
|
459
621
|
}
|
|
460
622
|
|
|
@@ -512,7 +674,7 @@ static void read_num(ParseInfo pi) {
|
|
|
512
674
|
// A trailing . is not a valid decimal but if encountered allow it
|
|
513
675
|
// except when mimicking the JSON gem or in strict mode.
|
|
514
676
|
if (StrictMode == pi->options.mode || CompatMode == pi->options.mode) {
|
|
515
|
-
|
|
677
|
+
size_t pos = pi->cur - ni.str;
|
|
516
678
|
|
|
517
679
|
if (1 == pos || (2 == pos && ni.neg)) {
|
|
518
680
|
oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "not a number");
|
|
@@ -814,6 +976,20 @@ static long double exp_plus[] = {
|
|
|
814
976
|
1.0e39, 1.0e40, 1.0e41, 1.0e42, 1.0e43, 1.0e44, 1.0e45, 1.0e46, 1.0e47, 1.0e48, 1.0e49,
|
|
815
977
|
};
|
|
816
978
|
|
|
979
|
+
static void validate_integer_size(size_t limit, NumInfo ni) {
|
|
980
|
+
size_t digit_count = ni->len - (ni->neg ? 1 : 0);
|
|
981
|
+
|
|
982
|
+
if (digit_count > limit) {
|
|
983
|
+
oj_set_error_at(ni->pi,
|
|
984
|
+
(Qnil != ni->pi->err_class) ? ni->pi->err_class : oj_parse_error_class,
|
|
985
|
+
__FILE__,
|
|
986
|
+
__LINE__,
|
|
987
|
+
"integer exceeds :max_integer_digits (%lu > %lu)",
|
|
988
|
+
(unsigned long)digit_count,
|
|
989
|
+
(unsigned long)limit);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
|
|
817
993
|
VALUE
|
|
818
994
|
oj_num_as_value(NumInfo ni) {
|
|
819
995
|
VALUE rnum = Qnil;
|
|
@@ -827,6 +1003,12 @@ oj_num_as_value(NumInfo ni) {
|
|
|
827
1003
|
} else if (ni->nan) {
|
|
828
1004
|
rnum = rb_float_new(0.0 / 0.0);
|
|
829
1005
|
} else if (1 == ni->div && 0 == ni->exp && !ni->has_exp) { // fixnum
|
|
1006
|
+
size_t limit = (NULL != ni->pi) ? ni->pi->options.max_integer_digits : 0;
|
|
1007
|
+
|
|
1008
|
+
if (0 < limit) {
|
|
1009
|
+
validate_integer_size(limit, ni);
|
|
1010
|
+
}
|
|
1011
|
+
|
|
830
1012
|
if (ni->big) {
|
|
831
1013
|
if (256 > ni->len) {
|
|
832
1014
|
char buf[256];
|
|
@@ -919,7 +1101,9 @@ void oj_set_error_at(ParseInfo pi, VALUE err_clas, const char *file, int line, c
|
|
|
919
1101
|
}
|
|
920
1102
|
va_end(ap);
|
|
921
1103
|
pi->err.clas = err_clas;
|
|
922
|
-
|
|
1104
|
+
// The eight bytes below, plus the ')' and the terminator that the two bytes
|
|
1105
|
+
// held back from end are for.
|
|
1106
|
+
if (p + 8 <= end) {
|
|
923
1107
|
*p++ = ' ';
|
|
924
1108
|
*p++ = '(';
|
|
925
1109
|
*p++ = 'a';
|
|
@@ -1036,11 +1220,19 @@ oj_pi_parse(int argc, VALUE *argv, ParseInfo pi, char *json, size_t len, int yie
|
|
|
1036
1220
|
buf = OJ_R_ALLOC_N(char, len + 1);
|
|
1037
1221
|
pi->json = buf;
|
|
1038
1222
|
pi->end = buf + len;
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1223
|
+
{
|
|
1224
|
+
size_t total = 0;
|
|
1225
|
+
|
|
1226
|
+
while (total < len) {
|
|
1227
|
+
cnt = read(fd, (char *)pi->json + total, len - total);
|
|
1228
|
+
if (cnt <= 0) {
|
|
1229
|
+
if (0 != buf) {
|
|
1230
|
+
OJ_R_FREE(buf);
|
|
1231
|
+
}
|
|
1232
|
+
rb_raise(rb_eIOError, "failed to read from IO Object.");
|
|
1233
|
+
}
|
|
1234
|
+
total += cnt;
|
|
1042
1235
|
}
|
|
1043
|
-
rb_raise(rb_eIOError, "failed to read from IO Object.");
|
|
1044
1236
|
}
|
|
1045
1237
|
((char *)pi->json)[len] = '\0';
|
|
1046
1238
|
/* skip UTF-8 BOM if present */
|
|
@@ -1121,9 +1313,16 @@ CLEANUP:
|
|
|
1121
1313
|
OJ_R_FREE(json);
|
|
1122
1314
|
}
|
|
1123
1315
|
stack_cleanup(&pi->stack);
|
|
1124
|
-
|
|
1125
|
-
|
|
1316
|
+
// The parsers match against pi->options.str_rx. A :match_string option
|
|
1317
|
+
// builds a chain of its own for this call, which has to be freed. Without
|
|
1318
|
+
// the option the member still aliases the chain owned by the defaults and
|
|
1319
|
+
// must be left alone.
|
|
1320
|
+
if (pi->options.str_rx.head != oj_default_options.str_rx.head) {
|
|
1321
|
+
oj_rxclass_cleanup(&pi->options.str_rx);
|
|
1322
|
+
pi->options.str_rx.head = NULL;
|
|
1323
|
+
pi->options.str_rx.tail = NULL;
|
|
1126
1324
|
}
|
|
1325
|
+
oj_free_call_options(&pi->options);
|
|
1127
1326
|
if (err_has(&pi->err)) {
|
|
1128
1327
|
rb_set_errinfo(Qnil);
|
|
1129
1328
|
if (Qnil != pi->err_class) {
|