oj 3.17.3 → 3.17.8

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.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +146 -0
  3. data/ext/oj/cache.c +72 -39
  4. data/ext/oj/cache.h +5 -0
  5. data/ext/oj/circarray.c +6 -2
  6. data/ext/oj/code.c +38 -28
  7. data/ext/oj/code.h +2 -0
  8. data/ext/oj/compat.c +63 -31
  9. data/ext/oj/custom.c +43 -8
  10. data/ext/oj/dump.c +120 -41
  11. data/ext/oj/dump.h +10 -0
  12. data/ext/oj/dump_compat.c +19 -15
  13. data/ext/oj/dump_object.c +38 -12
  14. data/ext/oj/dump_strict.c +2 -1
  15. data/ext/oj/extconf.rb +7 -0
  16. data/ext/oj/fast.c +132 -72
  17. data/ext/oj/intern.c +80 -77
  18. data/ext/oj/mimic_json.c +5 -29
  19. data/ext/oj/object.c +47 -17
  20. data/ext/oj/odd.c +6 -2
  21. data/ext/oj/oj.c +393 -44
  22. data/ext/oj/oj.h +10 -3
  23. data/ext/oj/parse.c +75 -7
  24. data/ext/oj/parse.h +23 -2
  25. data/ext/oj/parser.c +219 -71
  26. data/ext/oj/parser.h +1 -1
  27. data/ext/oj/rails.c +188 -45
  28. data/ext/oj/reader.c +22 -6
  29. data/ext/oj/reader.h +1 -0
  30. data/ext/oj/resolve.c +16 -6
  31. data/ext/oj/resolve.h +9 -0
  32. data/ext/oj/rxclass.c +16 -0
  33. data/ext/oj/rxclass.h +1 -0
  34. data/ext/oj/safe.c +62 -48
  35. data/ext/oj/safe.h +14 -2
  36. data/ext/oj/saj.c +60 -17
  37. data/ext/oj/saj2.c +44 -28
  38. data/ext/oj/simd.h +5 -3
  39. data/ext/oj/sparse.c +23 -6
  40. data/ext/oj/stream_writer.c +19 -1
  41. data/ext/oj/strict.c +42 -4
  42. data/ext/oj/string_writer.c +40 -10
  43. data/ext/oj/usual.c +17 -5
  44. data/ext/oj/util.c +27 -0
  45. data/ext/oj/util.h +2 -1
  46. data/ext/oj/val_stack.c +11 -22
  47. data/ext/oj/val_stack.h +70 -30
  48. data/ext/oj/wab.c +17 -26
  49. data/lib/oj/generic_object.rb +82 -0
  50. data/lib/oj/json.rb +1 -71
  51. data/lib/oj/mimic.rb +0 -1
  52. data/lib/oj/version.rb +1 -1
  53. data/pages/Compatibility.md +1 -1
  54. data/pages/Custom.md +2 -3
  55. data/pages/InstallOptions.md +8 -6
  56. data/pages/Modes.md +1 -1
  57. data/pages/Options.md +5 -6
  58. data/pages/Parser.md +2 -2
  59. data/pages/Rails.md +6 -0
  60. data/pages/Security.md +21 -7
  61. metadata +17 -16
data/ext/oj/parse.c CHANGED
@@ -390,6 +390,32 @@ void oj_scanner_init(void) {
390
390
  }
391
391
  }
392
392
 
393
+ // JSON does not allow an unescaped control character (below 0x20) in a
394
+ // string. Like a trailing decimal point, that is only rejected in strict mode
395
+ // and when mimicking the JSON gem.
396
+ static inline bool reject_control_chars(ParseInfo pi) {
397
+ return StrictMode == pi->options.mode || CompatMode == pi->options.mode;
398
+ }
399
+
400
+ static const char *find_control_char(const char *s, const char *end) {
401
+ // Eight bytes at a time first: a byte below 0x20 sets its high bit in
402
+ // (w - 0x20...) & ~w.
403
+ for (; s + sizeof(uint64_t) <= end; s += sizeof(uint64_t)) {
404
+ uint64_t w;
405
+
406
+ memcpy(&w, s, sizeof(w));
407
+ if (0 != ((w - 0x2020202020202020ULL) & ~w & 0x8080808080808080ULL)) {
408
+ break;
409
+ }
410
+ }
411
+ for (; s < end; s++) {
412
+ if ((unsigned char)*s < 0x20) {
413
+ return s;
414
+ }
415
+ }
416
+ return NULL;
417
+ }
418
+
393
419
  // entered at /
394
420
  static void read_escaped_str(ParseInfo pi, const char *start) {
395
421
  struct _buf buf;
@@ -409,11 +435,26 @@ static void read_escaped_str(ParseInfo pi, const char *start) {
409
435
  buf_cleanup(&buf);
410
436
  return;
411
437
  }
438
+ if (reject_control_chars(pi)) {
439
+ const char *ctrl = find_control_char(s, scanned);
440
+
441
+ if (NULL != ctrl) {
442
+ pi->cur = ctrl;
443
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "invalid control character in string");
444
+ buf_cleanup(&buf);
445
+ return;
446
+ }
447
+ }
412
448
  buf_append_string(&buf, s, (size_t)(scanned - s));
413
449
  s = scanned;
414
450
 
415
451
  if ('\\' == *s) {
416
452
  s++;
453
+ if (pi->end <= s) {
454
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "quoted string not terminated");
455
+ buf_cleanup(&buf);
456
+ return;
457
+ }
417
458
  switch (*s) {
418
459
  case 'n': buf_append(&buf, '\n'); break;
419
460
  case 'r': buf_append(&buf, '\r'); break;
@@ -538,6 +579,15 @@ static void read_str(ParseInfo pi) {
538
579
  oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "NULL byte in string");
539
580
  return;
540
581
  }
582
+ if (reject_control_chars(pi)) {
583
+ const char *ctrl = find_control_char(str, pi->cur);
584
+
585
+ if (NULL != ctrl) {
586
+ pi->cur = ctrl;
587
+ oj_set_error_at(pi, oj_parse_error_class, __FILE__, __LINE__, "invalid control character in string");
588
+ return;
589
+ }
590
+ }
541
591
  if ('\\' == *pi->cur) {
542
592
  read_escaped_str(pi, str);
543
593
  return;
@@ -1096,7 +1146,9 @@ void oj_set_error_at(ParseInfo pi, VALUE err_clas, const char *file, int line, c
1096
1146
  }
1097
1147
  va_end(ap);
1098
1148
  pi->err.clas = err_clas;
1099
- if (p + 3 < end) {
1149
+ // The eight bytes below, plus the ')' and the terminator that the two bytes
1150
+ // held back from end are for.
1151
+ if (p + 8 <= end) {
1100
1152
  *p++ = ' ';
1101
1153
  *p++ = '(';
1102
1154
  *p++ = 'a';
@@ -1198,17 +1250,20 @@ oj_pi_parse(int argc, VALUE *argv, ParseInfo pi, char *json, size_t len, int yie
1198
1250
  }
1199
1251
  } else {
1200
1252
  VALUE clas = rb_obj_class(input);
1201
- VALUE s;
1202
1253
 
1203
1254
  if (oj_stringio_class == clas) {
1204
- s = rb_funcall2(input, oj_string_id, 0, 0);
1205
- oj_pi_set_input_str(pi, &s);
1255
+ input = rb_funcall2(input, oj_string_id, 0, 0);
1256
+ StringValue(input);
1257
+ oj_pi_set_input_str(pi, &input);
1206
1258
  #if !IS_WINDOWS
1207
1259
  } else if (rb_cFile == clas && 0 == FIX2INT(rb_funcall(input, oj_pos_id, 0))) {
1208
1260
  int fd = FIX2INT(rb_funcall(input, oj_fileno_id, 0));
1209
1261
  ssize_t cnt;
1210
1262
  size_t len = lseek(fd, 0, SEEK_END);
1211
1263
 
1264
+ if ((size_t)-1 == len) {
1265
+ rb_raise(rb_eIOError, "failed to read from IO Object.");
1266
+ }
1212
1267
  lseek(fd, 0, SEEK_SET);
1213
1268
  buf = OJ_R_ALLOC_N(char, len + 1);
1214
1269
  pi->json = buf;
@@ -1254,7 +1309,11 @@ oj_pi_parse(int argc, VALUE *argv, ParseInfo pi, char *json, size_t len, int yie
1254
1309
  // value stack (while it is in scope).
1255
1310
  wrapped_stack = oj_stack_init(&pi->stack);
1256
1311
  rb_protect(protect_parse, (VALUE)pi, &line);
1257
- if (Qundef == pi->stack.head->val && !empty_ok(&pi->options)) {
1312
+ // Only when nothing is on the stack was the input actually empty. A
1313
+ // deferred hash (built at its closing brace from buffered pairs) leaves
1314
+ // head->val Qundef while its entry is still on the stack, e.g. when a
1315
+ // parse error cut the document short; the real error is already set.
1316
+ if (Qundef == pi->stack.head->val && stack_empty(&pi->stack) && !empty_ok(&pi->options)) {
1258
1317
  if (No == pi->options.nilnil || (CompatMode == pi->options.mode && 0 < pi->cur - pi->json)) {
1259
1318
  oj_set_error_at(pi, oj_json_parser_error_class, __FILE__, __LINE__, "Empty input");
1260
1319
  }
@@ -1306,9 +1365,16 @@ CLEANUP:
1306
1365
  OJ_R_FREE(json);
1307
1366
  }
1308
1367
  stack_cleanup(&pi->stack);
1309
- if (pi->str_rx.head != oj_default_options.str_rx.head) {
1310
- oj_rxclass_cleanup(&pi->str_rx);
1368
+ // The parsers match against pi->options.str_rx. A :match_string option
1369
+ // builds a chain of its own for this call, which has to be freed. Without
1370
+ // the option the member still aliases the chain owned by the defaults and
1371
+ // must be left alone.
1372
+ if (pi->options.str_rx.head != oj_default_options.str_rx.head) {
1373
+ oj_rxclass_cleanup(&pi->options.str_rx);
1374
+ pi->options.str_rx.head = NULL;
1375
+ pi->options.str_rx.tail = NULL;
1311
1376
  }
1377
+ oj_free_call_options(&pi->options);
1312
1378
  if (err_has(&pi->err)) {
1313
1379
  rb_set_errinfo(Qnil);
1314
1380
  if (Qnil != pi->err_class) {
@@ -1324,6 +1390,7 @@ CLEANUP:
1324
1390
  if (NULL != pi->json) {
1325
1391
  msg = rb_str_append(msg, rb_utf8_str_new_cstr(" in '"));
1326
1392
  msg = rb_str_append(msg, rb_utf8_str_new_cstr(pi->json));
1393
+ RB_GC_GUARD(input);
1327
1394
  }
1328
1395
  args[0] = msg;
1329
1396
  if (pi->err.clas == oj_parse_error_class) {
@@ -1363,5 +1430,6 @@ CLEANUP:
1363
1430
  break;
1364
1431
  }
1365
1432
  }
1433
+ RB_GC_GUARD(input);
1366
1434
  return result;
1367
1435
  }
data/ext/oj/parse.h CHANGED
@@ -5,6 +5,7 @@
5
5
  #define OJ_PARSE_H
6
6
 
7
7
  #include <stdarg.h>
8
+ #include <stddef.h>
8
9
  #include <stdio.h>
9
10
  #include <string.h>
10
11
 
@@ -49,7 +50,6 @@ typedef struct _parseInfo {
49
50
  VALUE handler;
50
51
  struct _valStack stack;
51
52
  CircArray circ_array;
52
- struct _rxClass str_rx;
53
53
  int expect_value;
54
54
  int max_depth; // just for the json gem
55
55
  VALUE proc;
@@ -81,6 +81,7 @@ extern VALUE oj_num_as_value(NumInfo ni);
81
81
  extern void oj_set_strict_callbacks(ParseInfo pi);
82
82
  extern void oj_set_object_callbacks(ParseInfo pi);
83
83
  extern void oj_set_compat_callbacks(ParseInfo pi);
84
+ extern void oj_compat_array_append_other(VALUE a, VALUE value);
84
85
  extern void oj_set_custom_callbacks(ParseInfo pi);
85
86
  extern void oj_set_wab_callbacks(ParseInfo pi);
86
87
 
@@ -91,11 +92,31 @@ extern VALUE oj_cstr_to_value(const char *str, size_t len, size_t cache_str);
91
92
  extern VALUE oj_calc_hash_key(ParseInfo pi, Val parent);
92
93
 
93
94
  static inline void parse_info_init(ParseInfo pi) {
94
- memset(pi, 0, sizeof(struct _parseInfo));
95
+ // Zeroing the whole struct costs more than parsing a small document: the
96
+ // embedded reader buffer (rd.base, 4KB) and value stack entries
97
+ // (stack.base, 6.5KB) dominate its 11KB. Both are fully initialized
98
+ // before use — oj_reader_init() for stream parses, oj_stack_init() and
99
+ // stack_push() for the value stack — so zero only the scalars around
100
+ // them.
101
+ memset(pi, 0, offsetof(struct _parseInfo, rd));
102
+ memset((char *)pi + offsetof(struct _parseInfo, rd) + offsetof(struct _reader, head),
103
+ 0,
104
+ offsetof(struct _parseInfo, stack) - offsetof(struct _parseInfo, rd) - offsetof(struct _reader, head));
105
+ memset((char *)pi + offsetof(struct _parseInfo, stack) + offsetof(struct _valStack, head),
106
+ 0,
107
+ sizeof(struct _parseInfo) - offsetof(struct _parseInfo, stack) - offsetof(struct _valStack, head));
95
108
  }
96
109
 
97
110
  extern void oj_scanner_init(void);
98
111
 
112
+ static inline void oj_compat_array_append(VALUE a, VALUE value) {
113
+ if (rb_cArray == RBASIC_CLASS(a)) {
114
+ rb_ary_push(a, value);
115
+ } else {
116
+ oj_compat_array_append_other(a, value);
117
+ }
118
+ }
119
+
99
120
  static inline bool empty_ok(Options options) {
100
121
  switch (options->mode) {
101
122
  case ObjectMode: