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/rails.c CHANGED
@@ -16,6 +16,10 @@ typedef struct _encoder {
16
16
  struct _rOptTable ropts;
17
17
  struct _options opts;
18
18
  VALUE arg;
19
+ // Each is false while the matching member is unused and the global it
20
+ // shadows - ropts or oj_default_options - is read instead.
21
+ bool own_ropts;
22
+ bool own_opts;
19
23
  } *Encoder;
20
24
 
21
25
  bool oj_rails_hash_opt = false;
@@ -83,6 +87,32 @@ static ROptTable copy_opts(ROptTable src, ROptTable dest) {
83
87
  return NULL;
84
88
  }
85
89
 
90
+ // The table an encoder reads from. Until the encoder is asked to optimize or
91
+ // deoptimize a class of its own it has no table and follows the global one.
92
+ // NULL is how oj_rails_get_opt() is told to use the global table, so the NULL
93
+ // has to reach it - handing it &e->ropts with an empty table instead would
94
+ // quietly turn every optimization off for this encoder.
95
+ static ROptTable encoder_ropts(Encoder e) {
96
+ return e->own_ropts ? &e->ropts : NULL;
97
+ }
98
+
99
+ // The options an encoder encodes with. See encoder_new() for why an encoder
100
+ // built without options follows the defaults instead of holding a copy.
101
+ static Options encoder_opts(Encoder e) {
102
+ return e->own_opts ? &e->opts : &oj_default_options;
103
+ }
104
+
105
+ // The table an encoder writes to. Copying the global table is deferred to here
106
+ // because it is a 6KB allocation and ActiveSupport builds an encoder for every
107
+ // encode call, while almost none of them ever optimize a class of their own.
108
+ static ROptTable encoder_own_ropts(Encoder e) {
109
+ if (!e->own_ropts) {
110
+ copy_opts(&ropts, &e->ropts);
111
+ e->own_ropts = true;
112
+ }
113
+ return &e->ropts;
114
+ }
115
+
86
116
  static int dump_attr_cb(ID key, VALUE value, VALUE ov) {
87
117
  Out out = (Out)ov;
88
118
  int depth = out->depth;
@@ -341,11 +371,16 @@ typedef struct _strLen {
341
371
  } *StrLen;
342
372
 
343
373
  static void dump_actioncontroller_parameters(VALUE obj, int depth, Out out, bool as_ok) {
374
+ int saved_argc = out->argc;
375
+ VALUE *saved_argv = out->argv;
376
+
344
377
  if (0 == parameters_id) {
345
378
  parameters_id = rb_intern("@parameters");
346
379
  }
347
380
  out->argc = 0;
348
381
  dump_rails_val(rb_ivar_get(obj, parameters_id), depth, out, true);
382
+ out->argc = saved_argc;
383
+ out->argv = saved_argv;
349
384
  }
350
385
 
351
386
  static StrLen columns_array(VALUE rcols, int *ccnt) {
@@ -375,7 +410,7 @@ static void dump_row(VALUE row, StrLen cols, int ccnt, int depth, Out out) {
375
410
 
376
411
  assure_size(out, 2);
377
412
  *out->cur++ = '{';
378
- size = depth * out->indent + 3;
413
+ size = indent_len(out, d2, out->opts->dump_opts.array_size) + 3;
379
414
  for (i = 0; i < ccnt; i++, cols++) {
380
415
  assure_size(out, size);
381
416
  if (out->opts->dump_opts.use) {
@@ -398,7 +433,7 @@ static void dump_row(VALUE row, StrLen cols, int ccnt, int depth, Out out) {
398
433
  *out->cur++ = ',';
399
434
  }
400
435
  }
401
- size = depth * out->indent + 1;
436
+ size = indent_len(out, depth, out->opts->dump_opts.array_size) + 1;
402
437
  assure_size(out, size);
403
438
  if (out->opts->dump_opts.use) {
404
439
  if (0 < out->opts->dump_opts.array_size) {
@@ -423,7 +458,9 @@ static ID columns_id = 0;
423
458
  static void dump_activerecord_result(VALUE obj, int depth, Out out, bool as_ok) {
424
459
  volatile VALUE rows;
425
460
  StrLen cols;
426
- int ccnt = 0;
461
+ int ccnt = 0;
462
+ int saved_argc = out->argc;
463
+ VALUE *saved_argv = out->argv;
427
464
  size_t i;
428
465
  size_t rcnt;
429
466
  size_t size;
@@ -439,11 +476,7 @@ static void dump_activerecord_result(VALUE obj, int depth, Out out, bool as_ok)
439
476
  rcnt = RARRAY_LEN(rows);
440
477
  assure_size(out, 2);
441
478
  *out->cur++ = '[';
442
- if (out->opts->dump_opts.use) {
443
- size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
444
- } else {
445
- size = d2 * out->indent + 2;
446
- }
479
+ size = indent_len(out, d2, out->opts->dump_opts.array_size) + 1;
447
480
  assure_size(out, 2);
448
481
  for (i = 0; i < rcnt; i++) {
449
482
  assure_size(out, size);
@@ -466,7 +499,7 @@ static void dump_activerecord_result(VALUE obj, int depth, Out out, bool as_ok)
466
499
  }
467
500
  }
468
501
  OJ_R_FREE(cols);
469
- size = depth * out->indent + 1;
502
+ size = indent_len(out, depth, out->opts->dump_opts.array_size) + 1;
470
503
  assure_size(out, size);
471
504
  if (out->opts->dump_opts.use) {
472
505
  if (0 < out->opts->dump_opts.array_size) {
@@ -483,6 +516,8 @@ static void dump_activerecord_result(VALUE obj, int depth, Out out, bool as_ok)
483
516
  fill_indent(out, depth);
484
517
  }
485
518
  *out->cur++ = ']';
519
+ out->argc = saved_argc;
520
+ out->argv = saved_argv;
486
521
  }
487
522
 
488
523
  typedef struct _namedFunc {
@@ -491,8 +526,16 @@ typedef struct _namedFunc {
491
526
  } *NamedFunc;
492
527
 
493
528
  static void dump_as_string(VALUE obj, int depth, Out out, bool as_ok) {
529
+ int saved_argc = out->argc;
530
+ VALUE *saved_argv = out->argv;
531
+
494
532
  if (oj_code_dump(oj_compat_codes, obj, depth, out)) {
495
- out->argc = 0;
533
+ // oj_code_dump() writes the whole value and returns, so there is no
534
+ // subtree here to keep the options away from. Leave them for the next
535
+ // sibling. Restore instead of leaving them alone in case the encode
536
+ // consumed them.
537
+ out->argc = saved_argc;
538
+ out->argv = saved_argv;
496
539
  return;
497
540
  }
498
541
  oj_dump_obj_to_s(obj, out);
@@ -500,31 +543,53 @@ static void dump_as_string(VALUE obj, int depth, Out out, bool as_ok) {
500
543
 
501
544
  static void dump_as_json(VALUE obj, int depth, Out out, bool as_ok) {
502
545
  volatile VALUE ja;
546
+ bool selected;
547
+ int saved_argc = out->argc;
548
+ VALUE *saved_argv = out->argv;
503
549
 
504
550
  TRACE(out->opts->trace, "as_json", obj, depth + 1, TraceRubyIn);
505
551
  // Some classes elect to not take an options argument so check the arity
506
552
  // of as_json.
507
553
  if (0 == rb_obj_method_arity(obj, oj_as_json_id)) {
508
- ja = rb_funcall(obj, oj_as_json_id, 0);
554
+ ja = rb_funcall(obj, oj_as_json_id, 0);
555
+ selected = false; // as_json got no options so it did not do any :only/:except selection
509
556
  } else {
510
- ja = rb_funcall2(obj, oj_as_json_id, out->argc, out->argv);
557
+ selected = (0 < out->argc); // Rails handed the :only/:except options to as_json
558
+ ja = rb_funcall2(obj, oj_as_json_id, out->argc, out->argv);
511
559
  }
512
560
  TRACE(out->opts->trace, "as_json", obj, depth + 1, TraceRubyOut);
513
561
 
514
562
  out->argc = 0;
515
- if (ja == obj || !as_ok) {
516
- // Once as_json is called it should never be called again on the same
517
- // object with as_ok.
518
- dump_rails_val(ja, depth, out, false);
519
- } else {
520
- int type = rb_type(ja);
563
+ // When as_json received the options it has already applied the Rails
564
+ // :only/:except selection at the attribute level (and kept the
565
+ // include_root_in_json wrapper and any :methods keys), so the Oj dump level
566
+ // :only/:except key filter must not be applied to the value it returned - a
567
+ // second pass would drop the wrapper (#1020) and the :methods keys (#1008).
568
+ // Suppress the filter only for this subtree (save and restore) so sibling
569
+ // keys outside the as_json result are still filtered. This is a flag on the
570
+ // dump state, not a change to out->opts: a StringWriter owns its options
571
+ // struct, so clearing only/except there would leak the buffers and kill the
572
+ // filter for every later push_value.
573
+ {
574
+ bool key_filter_off = out->key_filter_off;
521
575
 
522
- if (T_HASH == type || T_ARRAY == type) {
523
- dump_rails_val(ja, depth, out, true);
576
+ if (selected) {
577
+ out->key_filter_off = true;
578
+ }
579
+ if (ja == obj || !as_ok) {
580
+ // Once as_json is called it should never be called again on the same
581
+ // object with as_ok.
582
+ dump_rails_val(ja, depth, out, false);
524
583
  } else {
525
584
  dump_rails_val(ja, depth, out, true);
526
585
  }
586
+ out->key_filter_off = key_filter_off;
527
587
  }
588
+ // The options were consumed for this value only. Restore them so that the
589
+ // next sibling in a container is handed them too, the way ActiveSupport's
590
+ // Array#as_json and Hash#as_json pass them to every element.
591
+ out->argc = saved_argc;
592
+ out->argv = saved_argv;
528
593
  }
529
594
 
530
595
  static void dump_regexp(VALUE obj, int depth, Out out, bool as_ok) {
@@ -551,11 +616,16 @@ static VALUE activerecord_base = Qundef;
551
616
  static ID attributes_id = 0;
552
617
 
553
618
  static void dump_activerecord(VALUE obj, int depth, Out out, bool as_ok) {
619
+ int saved_argc = out->argc;
620
+ VALUE *saved_argv = out->argv;
621
+
554
622
  if (0 == attributes_id) {
555
623
  attributes_id = rb_intern("@attributes");
556
624
  }
557
625
  out->argc = 0;
558
626
  dump_rails_val(rb_ivar_get(obj, attributes_id), depth, out, true);
627
+ out->argc = saved_argc;
628
+ out->argv = saved_argv;
559
629
  }
560
630
 
561
631
  static ROpt create_opt(ROptTable rot, VALUE clas) {
@@ -591,6 +661,12 @@ static ROpt create_opt(ROptTable rot, VALUE clas) {
591
661
  ro->clas = clas;
592
662
  ro->on = true;
593
663
  ro->dump = dump_obj_attrs;
664
+ if (&ropts == rot) {
665
+ // Nothing marks the global table, so a class that lives only in it has
666
+ // to be a root. Encoders used to hold a copy of the table and mark it,
667
+ // which covered this by accident for as long as one was alive.
668
+ rb_gc_register_mark_object(clas);
669
+ }
594
670
  for (nf = dump_map; NULL != nf->name; nf++) {
595
671
  if (0 == strcmp(nf->name, classname)) {
596
672
  ro->dump = nf->func;
@@ -623,6 +699,9 @@ static void encoder_free(void *ptr) {
623
699
  if (NULL != ptr) {
624
700
  Encoder e = (Encoder)ptr;
625
701
 
702
+ if (e->own_opts) {
703
+ oj_options_release(&e->opts);
704
+ }
626
705
  if (NULL != e->ropts.table) {
627
706
  OJ_R_FREE(e->ropts.table);
628
707
  }
@@ -633,10 +712,25 @@ static void encoder_free(void *ptr) {
633
712
  static void encoder_mark(void *ptr) {
634
713
  if (NULL != ptr) {
635
714
  Encoder e = (Encoder)ptr;
715
+ int i;
636
716
 
717
+ if (e->own_opts) {
718
+ oj_options_mark(&e->opts);
719
+ }
637
720
  if (Qnil != e->arg) {
638
721
  rb_gc_mark(e->arg);
639
722
  }
723
+ // The optimized classes in the encoder table are not reachable from
724
+ // anywhere else once optimize() is called on the encoder itself. An
725
+ // encoder that never optimized anything has no table to walk - the
726
+ // classes in the global table are roots registered by create_opt().
727
+ if (NULL != e->ropts.table) {
728
+ for (i = 0; i < e->ropts.len; i++) {
729
+ if (Qnil != e->ropts.table[i].clas) {
730
+ rb_gc_mark(e->ropts.table[i].clas);
731
+ }
732
+ }
733
+ }
640
734
  }
641
735
  }
642
736
 
@@ -660,15 +754,43 @@ static const rb_data_type_t oj_encoder_type = {
660
754
  static VALUE encoder_new(int argc, VALUE *argv, VALUE self) {
661
755
  Encoder e = OJ_R_ALLOC(struct _encoder);
662
756
 
663
- e->opts = oj_default_options;
664
- copy_opts(&ropts, &e->ropts);
757
+ e->ropts.len = 0;
758
+ e->ropts.alen = 0;
759
+ e->ropts.table = NULL;
760
+ e->own_ropts = false;
665
761
 
666
762
  if (1 <= argc && Qnil != *argv) {
667
763
  e->arg = *argv;
668
764
  } else {
669
765
  e->arg = rb_hash_new();
670
766
  }
671
- oj_parse_options(e->arg, &e->opts);
767
+ // An encoder given no options of its own reads oj_default_options at encode
768
+ // time rather than copying it here. ActiveSupport keeps such an encoder for
769
+ // the life of the process, so a copy taken now would freeze every later
770
+ // Oj.default_options and ActiveSupport::JSON::Encoding change out of it -
771
+ // including the time_precision that set_encoder itself writes after
772
+ // ActiveSupport has already built and cached that encoder.
773
+ //
774
+ // A hash counts as options only if at least one key names an option Oj
775
+ // knows. Non-empty is not enough: ActiveSupport 8.1 caches a second encoder
776
+ // built with {escape: false}, a key Oj has never had.
777
+ e->own_opts = T_HASH == rb_type(e->arg) && 0 < RHASH_SIZE(e->arg);
778
+ if (e->own_opts) {
779
+ e->opts = oj_default_options;
780
+ // Detach from the match_string regexps owned by the defaults before the
781
+ // options are parsed so that a :match_string option builds a chain of
782
+ // its own that is freed with the encoder.
783
+ e->opts.str_rx.head = NULL;
784
+ e->opts.str_rx.tail = NULL;
785
+ e->own_opts = oj_parse_options_consumed(e->arg, &e->opts);
786
+ }
787
+ if (e->own_opts) {
788
+ oj_options_take_ownership(&e->opts);
789
+ } else {
790
+ // Nothing to free: every option that allocates is reached through a
791
+ // recognized key, and ownership is not taken until after that check.
792
+ memset(&e->opts, 0, sizeof(e->opts));
793
+ }
672
794
 
673
795
  return TypedData_Wrap_Struct(encoder_class, &oj_encoder_type, e);
674
796
  }
@@ -765,7 +887,7 @@ static VALUE encoder_optimize(int argc, VALUE *argv, VALUE self) {
765
887
  Encoder e;
766
888
  TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
767
889
 
768
- optimize(argc, argv, &e->ropts, true);
890
+ optimize(argc, argv, encoder_own_ropts(e), true);
769
891
 
770
892
  return Qnil;
771
893
  }
@@ -822,7 +944,7 @@ static VALUE encoder_deoptimize(int argc, VALUE *argv, VALUE self) {
822
944
  Encoder e;
823
945
  TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
824
946
 
825
- optimize(argc, argv, &e->ropts, false);
947
+ optimize(argc, argv, encoder_own_ropts(e), false);
826
948
 
827
949
  return Qnil;
828
950
  }
@@ -853,7 +975,7 @@ static VALUE encoder_optimized(VALUE self, VALUE clas) {
853
975
  ROpt ro;
854
976
 
855
977
  TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
856
- ro = oj_rails_get_opt(&e->ropts, clas);
978
+ ro = oj_rails_get_opt(encoder_ropts(e), clas);
857
979
 
858
980
  if (NULL == ro) {
859
981
  return Qfalse;
@@ -940,6 +1062,7 @@ static VALUE encode(VALUE obj, ROptTable ropts, Options opts, int argc, VALUE *a
940
1062
  }
941
1063
  if (Yes == copts.circular) {
942
1064
  oj_cache8_delete(out.circ_cache);
1065
+ out.circ_cache = NULL;
943
1066
  }
944
1067
 
945
1068
  oj_out_free(&out);
@@ -962,11 +1085,22 @@ static VALUE encoder_encode(VALUE self, VALUE obj) {
962
1085
  TypedData_Get_Struct(self, struct _encoder, &oj_encoder_type, e);
963
1086
 
964
1087
  if (Qnil != e->arg) {
965
- VALUE argv[1] = {e->arg};
966
-
967
- return encode(obj, &e->ropts, &e->opts, 1, argv);
968
- }
969
- return encode(obj, &e->ropts, &e->opts, 0, NULL);
1088
+ // as_json is allowed to write into the options hash it is handed, and
1089
+ // the ActiveSupport suite has a test that does exactly that. Handing
1090
+ // out the encoder's own hash lets one such call poison every later
1091
+ // encode by the same encoder, which on ActiveSupport 8.1 is every
1092
+ // option-less to_json in the process because json_encoder= caches one
1093
+ // encoder and keeps it. Rails never exposes a hash it will reuse:
1094
+ // JSONGemEncoder#encode passes options.dup, and only when there are
1095
+ // options at all, so with none as_json writes into its own default.
1096
+ // The copy is not frozen the way Rails 8.0 and later freeze theirs,
1097
+ // because Oj hands as_json the hash even when it is empty and that is
1098
+ // exactly the case Rails leaves writable.
1099
+ VALUE argv[1] = {T_HASH == rb_type(e->arg) ? rb_hash_dup(e->arg) : e->arg};
1100
+
1101
+ return encode(obj, encoder_ropts(e), encoder_opts(e), 1, argv);
1102
+ }
1103
+ return encode(obj, encoder_ropts(e), encoder_opts(e), 0, NULL);
970
1104
  }
971
1105
 
972
1106
  /* Document-method: encode
@@ -1051,8 +1185,6 @@ static VALUE rails_set_encoder(VALUE self) {
1051
1185
  } else {
1052
1186
  rb_raise(rb_eStandardError, "ActiveSupport not loaded.");
1053
1187
  }
1054
- rb_funcall(active, rb_intern("json_encoder="), 1, encoder_class);
1055
-
1056
1188
  json = rb_const_get_at(active, rb_intern("JSON"));
1057
1189
  encoding = rb_const_get_at(json, rb_intern("Encoding"));
1058
1190
 
@@ -1079,6 +1211,12 @@ static VALUE rails_set_encoder(VALUE self) {
1079
1211
  rb_define_module_function(encoding, "time_precision=", rails_time_precision, 1);
1080
1212
  rb_gv_set("$VERBOSE", verbose);
1081
1213
 
1214
+ // Last, because ActiveSupport builds and caches encoders inside
1215
+ // json_encoder= and they should see the settings above already in the
1216
+ // defaults. escape_html and xml_time are read ahead of it for the same
1217
+ // reason.
1218
+ rb_funcall(active, rb_intern("json_encoder="), 1, encoder_class);
1219
+
1082
1220
  return Qnil;
1083
1221
  }
1084
1222
 
@@ -1179,12 +1317,15 @@ static void dump_float(VALUE obj, int depth, Out out, bool as_ok) {
1179
1317
  size_t cnt = 0;
1180
1318
 
1181
1319
  if (0.0 == d) {
1182
- b = buf;
1320
+ b = buf;
1321
+ if (signbit(d)) {
1322
+ *b++ = '-';
1323
+ }
1183
1324
  *b++ = '0';
1184
1325
  *b++ = '.';
1185
1326
  *b++ = '0';
1186
- *b++ = '\0';
1187
- cnt = 3;
1327
+ *b = '\0';
1328
+ cnt = b - buf;
1188
1329
  } else {
1189
1330
  if (isnan(d) || OJ_INFINITY == d || -OJ_INFINITY == d) {
1190
1331
  strcpy(buf, "null");
@@ -1230,15 +1371,12 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
1230
1371
  if (0 == cnt) {
1231
1372
  *out->cur++ = ']';
1232
1373
  } else {
1233
- if (out->opts->dump_opts.use) {
1234
- size = d2 * out->opts->dump_opts.indent_size + out->opts->dump_opts.array_size + 1;
1235
- } else {
1236
- size = d2 * out->indent + 2;
1237
- }
1374
+ size = indent_len(out, d2, out->opts->dump_opts.array_size) + 1;
1238
1375
  assure_size(out, size * cnt);
1239
1376
  cnt--;
1240
1377
  for (i = 0; i <= cnt; i++) {
1241
1378
  if (out->opts->dump_opts.use) {
1379
+ assure_size(out, size);
1242
1380
  if (0 < out->opts->dump_opts.array_size) {
1243
1381
  APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
1244
1382
  }
@@ -1256,7 +1394,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
1256
1394
  *out->cur++ = ',';
1257
1395
  }
1258
1396
  }
1259
- size = depth * out->indent + 1;
1397
+ size = indent_len(out, depth, out->opts->dump_opts.array_size) + 1;
1260
1398
  assure_size(out, size);
1261
1399
  if (out->opts->dump_opts.use) {
1262
1400
  if (0 < out->opts->dump_opts.array_size) {
@@ -1286,7 +1424,7 @@ static int hash_cb(VALUE key, VALUE value, VALUE ov) {
1286
1424
  if (out->omit_nil && Qnil == value) {
1287
1425
  return ST_CONTINUE;
1288
1426
  }
1289
- if (NULL != out->opts->dump_opts.only || NULL != out->opts->dump_opts.except) {
1427
+ if (!out->key_filter_off && (NULL != out->opts->dump_opts.only || NULL != out->opts->dump_opts.except)) {
1290
1428
  if (oj_key_skip(key, out->opts->dump_opts.only, out->opts->dump_opts.except)) {
1291
1429
  return ST_CONTINUE;
1292
1430
  }
@@ -1384,10 +1522,15 @@ static void dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
1384
1522
  }
1385
1523
 
1386
1524
  static void dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
1387
- VALUE clas;
1525
+ VALUE clas;
1526
+ int saved_argc = out->argc;
1527
+ VALUE *saved_argv = out->argv;
1388
1528
 
1389
1529
  if (oj_code_dump(oj_compat_codes, obj, depth, out)) {
1390
- out->argc = 0;
1530
+ // Same as in dump_as_string(): the value is complete, so the options
1531
+ // stay available for the next sibling.
1532
+ out->argc = saved_argc;
1533
+ out->argv = saved_argv;
1391
1534
  return;
1392
1535
  }
1393
1536
  clas = rb_obj_class(obj);
data/ext/oj/reader.c CHANGED
@@ -45,6 +45,7 @@ void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s) {
45
45
  reader->line = 1;
46
46
  reader->col = 0;
47
47
  reader->free_head = 0;
48
+ reader->in_val = Qnil;
48
49
 
49
50
  if (0 != fd) {
50
51
  reader->read_func = read_from_fd;
@@ -52,6 +53,7 @@ void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s) {
52
53
  } else if (rb_cString == io_class) {
53
54
  reader->read_func = 0;
54
55
  reader->in_str = StringValuePtr(io);
56
+ reader->in_val = io;
55
57
  reader->head = (char *)reader->in_str;
56
58
  reader->tail = reader->head;
57
59
  reader->read_end = reader->head + RSTRING_LEN(io);
@@ -60,6 +62,7 @@ void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s) {
60
62
 
61
63
  reader->read_func = 0;
62
64
  reader->in_str = StringValuePtr(s);
65
+ reader->in_val = s;
63
66
  reader->head = (char *)reader->in_str;
64
67
  reader->tail = reader->head;
65
68
  reader->read_end = reader->head + RSTRING_LEN(s);
@@ -79,6 +82,7 @@ void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s) {
79
82
 
80
83
  reader->read_func = 0;
81
84
  reader->in_str = StringValuePtr(rstr);
85
+ reader->in_val = rstr;
82
86
  reader->head = (char *)reader->in_str;
83
87
  reader->tail = reader->head;
84
88
  reader->read_end = reader->head + RSTRING_LEN(rstr);
@@ -88,8 +92,8 @@ void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s) {
88
92
  }
89
93
 
90
94
  int oj_reader_read(Reader reader) {
91
- int err;
92
- size_t shift = 0;
95
+ int err;
96
+ ptrdiff_t shift = 0;
93
97
 
94
98
  if (0 == reader->read_func) {
95
99
  return -1;
@@ -164,8 +168,14 @@ static VALUE partial_io_cb(VALUE rbuf) {
164
168
  }
165
169
  str = StringValuePtr(rstr);
166
170
  cnt = RSTRING_LEN(rstr);
167
- strcpy(reader->tail, str);
168
- reader->read_end = reader->tail + cnt;
171
+ if (cnt > (size_t)(reader->end - reader->tail)) {
172
+ // A misbehaving IO returned more than the requested number of bytes.
173
+ // Copying it in with strcpy() would overflow the reader buffer.
174
+ rb_raise(rb_eIOError, "read returned more than the requested number of bytes");
175
+ }
176
+ memcpy(reader->tail, str, cnt);
177
+ reader->tail[cnt] = '\0';
178
+ reader->read_end = reader->tail + cnt;
169
179
 
170
180
  return Qtrue;
171
181
  }
@@ -184,8 +194,14 @@ static VALUE io_cb(VALUE rbuf) {
184
194
  }
185
195
  str = StringValuePtr(rstr);
186
196
  cnt = RSTRING_LEN(rstr);
187
- strcpy(reader->tail, str);
188
- reader->read_end = reader->tail + cnt;
197
+ if (cnt > (size_t)(reader->end - reader->tail)) {
198
+ // A misbehaving IO returned more than the requested number of bytes.
199
+ // Copying it in with strcpy() would overflow the reader buffer.
200
+ rb_raise(rb_eIOError, "read returned more than the requested number of bytes");
201
+ }
202
+ memcpy(reader->tail, str, cnt);
203
+ reader->tail[cnt] = '\0';
204
+ reader->read_end = reader->tail + cnt;
189
205
 
190
206
  return Qtrue;
191
207
  }
data/ext/oj/reader.h CHANGED
@@ -24,6 +24,7 @@ typedef struct _reader {
24
24
  VALUE io;
25
25
  const char *in_str;
26
26
  };
27
+ VALUE in_val;
27
28
  } *Reader;
28
29
 
29
30
  extern void oj_reader_init(Reader reader, VALUE io, int fd, bool to_s);
data/ext/oj/resolve.c CHANGED
@@ -19,6 +19,9 @@ inline static VALUE resolve_classname(VALUE mod, const char *classname, int auto
19
19
 
20
20
  if (rb_const_defined_at(mod, ci)) {
21
21
  clas = rb_const_get_at(mod, ci);
22
+ if (!RB_TYPE_P(clas, T_CLASS) && !RB_TYPE_P(clas, T_MODULE)) {
23
+ clas = Qundef;
24
+ }
22
25
  } else if (auto_define) {
23
26
  clas = rb_define_class_under(mod, classname, oj_bag_class);
24
27
  } else {
@@ -32,7 +35,8 @@ static VALUE resolve_classpath(ParseInfo pi, const char *name, size_t len, int a
32
35
  VALUE clas;
33
36
  char *end = class_name + sizeof(class_name) - 1;
34
37
  char *s;
35
- const char *n = name;
38
+ const char *n = name;
39
+ size_t nlen = len;
36
40
 
37
41
  clas = rb_cObject;
38
42
  for (s = class_name; 0 < len; n++, len--) {
@@ -55,7 +59,12 @@ static VALUE resolve_classpath(ParseInfo pi, const char *name, size_t len, int a
55
59
  }
56
60
  *s = '\0';
57
61
  if (Qundef == (clas = resolve_classname(clas, class_name, auto_define))) {
58
- oj_set_error_at(pi, error_class, __FILE__, __LINE__, "class %s is not defined", name);
62
+ if (sizeof(class_name) <= nlen) {
63
+ nlen = sizeof(class_name) - 1;
64
+ }
65
+ memcpy(class_name, name, nlen);
66
+ class_name[nlen] = '\0';
67
+ oj_set_error_at(pi, error_class, __FILE__, __LINE__, "class '%s' is not defined", class_name);
59
68
  if (Qnil != error_class) {
60
69
  pi->err_class = error_class;
61
70
  }
@@ -73,8 +82,9 @@ oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define, VALUE
73
82
 
74
83
  VALUE
75
84
  oj_name2struct(ParseInfo pi, VALUE nameVal, VALUE error_class) {
76
- size_t len = RSTRING_LEN(nameVal);
77
- const char *str = StringValuePtr(nameVal);
78
-
79
- return resolve_classpath(pi, str, len, 0, error_class);
85
+ if (T_STRING != rb_type(nameVal)) {
86
+ oj_set_error_at(pi, error_class, __FILE__, __LINE__, "Invalid struct data");
87
+ return Qundef;
88
+ }
89
+ return resolve_classpath(pi, RSTRING_PTR(nameVal), RSTRING_LEN(nameVal), 0, error_class);
80
90
  }
data/ext/oj/resolve.h CHANGED
@@ -4,9 +4,18 @@
4
4
  #ifndef OJ_RESOLVE_H
5
5
  #define OJ_RESOLVE_H
6
6
 
7
+ #include <stdbool.h>
8
+ #include <string.h>
9
+
7
10
  #include "ruby.h"
8
11
 
9
12
  extern VALUE oj_name2class(ParseInfo pi, const char *name, size_t len, int auto_define, VALUE error_class);
10
13
  extern VALUE oj_name2struct(ParseInfo pi, VALUE nameVal, VALUE error_class);
11
14
 
15
+ inline static bool oj_is_backtrace_name(const char *name, size_t len) {
16
+ static const char backtrace[] = "Thread::Backtrace";
17
+
18
+ return sizeof(backtrace) - 1 == len && 0 == memcmp(backtrace, name, len);
19
+ }
20
+
12
21
  #endif /* OJ_RESOLVE_H */
data/ext/oj/rxclass.c CHANGED
@@ -142,3 +142,19 @@ void oj_rxclass_copy(RxClass src, RxClass dest) {
142
142
  }
143
143
  }
144
144
  }
145
+
146
+ // Add the Ruby objects in the chain to an array. The regexps and the classes
147
+ // can be referenced from nowhere else, as with a default match_string option,
148
+ // and are collected unless something holds onto them.
149
+ void oj_rxclass_keep(RxClass rc, VALUE keep) {
150
+ RxC rxc;
151
+
152
+ for (rxc = rc->head; NULL != rxc; rxc = rxc->next) {
153
+ if (Qnil != rxc->rrx) {
154
+ rb_ary_push(keep, rxc->rrx);
155
+ }
156
+ if (Qnil != rxc->clas) {
157
+ rb_ary_push(keep, rxc->clas);
158
+ }
159
+ }
160
+ }
data/ext/oj/rxclass.h CHANGED
@@ -22,5 +22,6 @@ extern int oj_rxclass_append(RxClass rc, const char *expr, VALUE clas);
22
22
  extern VALUE oj_rxclass_match(RxClass rc, const char *str, size_t len);
23
23
  extern void oj_rxclass_copy(RxClass src, RxClass dest);
24
24
  extern void oj_rxclass_rappend(RxClass rc, VALUE rx, VALUE clas);
25
+ extern void oj_rxclass_keep(RxClass rc, VALUE keep);
25
26
 
26
27
  #endif /* OJ_RXCLASS_H */