oj 3.17.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7515f34ac9c85f3e60f14c886bf3701f41fd077bdc518c71c6125e6ca7913db
4
- data.tar.gz: f4a5cab57b8fc728e7ec4aac4dbc672d26b69f83f6aa31c98c6d6237892f6bdc
3
+ metadata.gz: af3a1f2b343eefacdaeb1f82d4b48fe67bece49e4aae63ad46aa878f726ae3de
4
+ data.tar.gz: 8cd6e1ac300706f39d434cbbba52ea780a96b761ba2090d3d3624185bc021819
5
5
  SHA512:
6
- metadata.gz: 0aa115483a049bb1a9b406d3d9fa1dda4dcb47cf50236301f5ea9acca4c21b20118a027462e384d60c0cb287be92a80b163dc924f58a93a0f5998d0f2e577f4c
7
- data.tar.gz: 2f31c241a2283f353bf503eb54d89921643ec5fdc0ecb8166c31d68b5deb79fb26f68ea412155cc17dc5734bce9a85451bd798c600365428d5f389eabdc41346
6
+ metadata.gz: e081fafa7bd5af792fef91dc58612d6ea85bdae7b3d709bbd945773a5ad9d1feec4c88e3844aa403098c3fa267814ef61dcbba5fb4d9ee86a239eecf5b3926fa
7
+ data.tar.gz: 339ba6bedb36fb30a382f8cb5037053a4197d4e9364bb11f50b79ec6ec3002cdd687868e140fec1dacabdc9cfa972a45cfcd6cb8a85e4c4148cd484f56278004
data/CHANGELOG.md CHANGED
@@ -1,5 +1,92 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 3.17.6 - 2026-08-10
4
+
5
+ - Fixed issue #1092, where the encoder ActiveSupport 8.1 caches with `escape: false` froze the options as they stood before `set_encoder` wrote `time_precision` into them, so `to_json(escape: false)` emitted 9 fractional digits. An options hash that names no option Oj knows no longer detaches an encoder from the defaults. (#1093)
6
+
7
+ ## 3.17.5 - 2026-07-31
8
+
9
+ Most of this release comes out of a security review of the C extension. There are no API changes.
10
+
11
+ ### Memory safety
12
+
13
+ - Fixed hash flooding by seeding the key hash per process. The hash was decided entirely at compile time, so a document of colliding keys turned key interning into O(n^2) and left the process slow for every later parse. (#1053)
14
+ - Fixed a one byte buffer over-read on a document that ends with a backslash inside an unterminated string. (#1054)
15
+ - Fixed two length underflows that made an empty `^m` value and a key of exactly 65,536 bytes read forward until they left mapped memory. (#1055)
16
+ - Fixed a SEGV from type confusion in class path resolution. A name segment that resolved to something other than a class or module was dereferenced as one. (#1056)
17
+ - Fixed uninitialized stack memory being read into the message of an unresolved class name error. (#1057)
18
+ - Fixed output buffer overflows when writing indentation, where several callers of `fill_indent()` had not assured the buffer first. (#1058)
19
+ - Fixed the `Oj::Doc` path depth limit being checked one level too late. (#1061)
20
+ - Fixed the use-after-free of a key when the value stack grows. (#1062)
21
+ - Fixed the buffer over-read when a document ends where a key belongs. (#1063)
22
+ - Fixed the id from a circular reference being used unchecked. (#1064)
23
+ - Fixed the SEGV dumping an exception that was never raised, where the missing backtrace was read as an Array. (#1068)
24
+ - Fixed the stack buffer overflow writing the error location. (#1071)
25
+ - Fixed a read error in `Oj::Parser#file` being taken as a read of `SIZE_MAX` bytes, which wrote in front of the buffer and then reparsed it forever without checking for interrupts. (#1072)
26
+ - Fixed a `:float_format` too long for the 64 byte buffer copying stack bytes past the end of it into the output. (#1073)
27
+ - Fixed the double free of the default `create_id` when a call passes `:create_id => nil`. (#1074)
28
+ - Fixed the buffer over-read of an odd attribute name and the leak of its arguments. (#1075)
29
+ - Fixed the SEGV reading a truncated document with `Oj::Parser#file` and the descriptor it leaked. (#1076)
30
+ - Fixed the SEGV from a malformed number in `Oj::Doc`. (#1077)
31
+ - Fixed the stack exhaustion from a long key with `:only` or `:except`. (#1078)
32
+ - Fixed the odd module match being decided by uninitialized memory. (#1079)
33
+ - Fixed `:float_format` accepting directives it has no argument for. (#1083)
34
+
35
+ ### Memory leaks
36
+
37
+ - Fixed the leak of an escaped hash key in the streaming parser. (#1067)
38
+ - Fixed the leak of the parser `create_id` when it is set a second time. (#1085)
39
+ - Fixed the leak of the parser when `Oj::Parser.new` or `Oj::Parser.safe` raises on an argument it does not take. (#1086)
40
+
41
+ ### Parsing and dumping
42
+
43
+ - Fixed a safe parser limit of 4 being ignored. (#1065)
44
+ - Fixed the exponent bound being checked after it is narrowed. (#1066)
45
+ - Fixed a document being rejected when it opens with a number of 4093 digits or more read through a file descriptor or an IO, and the reader buffer that leaked when it grew. (#1069)
46
+ - Fixed the safe parser limits being dropped when an option is set after the parser is built. (#1070)
47
+ - Fixed the `:custom` mode dump of an exception that was never raised opening with `{,`. (#1082)
48
+ - Fixed the `:wab` mode time load being an offset out on Windows, where `mktime()` stood in for `timegm()` and returned -1 for any time before the epoch. (#1088)
49
+ - Fixed `Oj::Doc` dropping the exponent of a number written without a decimal point, so `1e3` loaded as `1`. (#1089)
50
+
51
+ ### Rails
52
+
53
+ - Fixed a `to_json(only:)` or `to_json(except:)` dropping the `include_root_in_json` wrapper key and the keys added by `:methods`. (#1039)
54
+ - Fixed the `as_json` options reaching only the first element of a container, so `to_json(only:)`, `to_json(methods:)` and any custom `as_json(opts)` took effect on one row and nowhere else. (#1040)
55
+ - An encoder now reads the global optimized class table and options instead of copying them, saving about 6.7KB of malloc per `ActiveSupport::JSON.encode`, and picks up later `Oj.default_options` and `ActiveSupport::JSON::Encoding` changes. (#1049)
56
+ - Fixed `as_json` being handed the encoder's own options hash. ActiveSupport allows `as_json` to write into it, and ActiveSupport 8.1 caches one encoder per process, so a single such `as_json` could leave `:only` set for every later `to_json`. (#1050)
57
+
58
+ ### Documentation
59
+
60
+ - Renamed the documented `:skip_null_byte` option to `:omit_null_byte`, which is the name that works. (#1041)
61
+ - Dropped the `--with-sse42` install option from InstallOptions.md. SIMD support is detected at runtime. (#1042)
62
+ - Corrected the documented minimum Ruby version from 2.4+ to 2.7+. (#1043)
63
+ - Corrected the Parser.md benchmark conclusions to reference `:usual` rather than `:saj`. (#1044)
64
+ - Corrected Custom.md and Options.md, which called `:custom` the default mode. The default is `:object`. (#1045)
65
+ - Corrected the documented type of the `:safe` option from String to Boolean. (#1046)
66
+ - Corrected Security.md, which said no methods are ever called on an Object created by a load. `exception`, `set_backtrace` and `replace` are. The page now also says which mode `Oj.load` uses. (#1080)
67
+
68
+ ### Tests and CI
69
+
70
+ - Added the ActiveSupport 8 test suite. The Rakefile picked the suite by major version, so the rails_8 job had been running zero ActiveSupport tests. (#1047)
71
+ - Added the ActiveSupport 8.1 test suite and pinned the #985 regression. (#1051)
72
+ - Fixed `rake` never running the `test/test_*.rb` suite. The `Rake::TestTask` that defined `test` was commented out, so `test_all` invoked a synthesized no-op file task. (#1084)
73
+ - Added a job timeout to the workflows. (#1087)
74
+ - Removed the pre-1970 time test skips on Windows, which were left from a 32 bit Ruby limit that no longer applies. (#1090)
75
+
76
+ ## 3.17.4 - 2026-07-14
77
+
78
+ - Fixed issue in fast.c where rescuing from a panic did not reset the where counter.
79
+
80
+ - Multiple fixes thanks to @Watson1978.
81
+
82
+ ## 3.17.3 - 2026-06-04
83
+
84
+ - Fixed issue in intern.c and fast.c.
85
+
86
+ ## 3.17.2 - 2026-05-27
87
+
88
+ - Fixed multiple issues related to extreme sizes.
89
+
3
90
  ## 3.17.1 - 2026-05-15
4
91
 
5
92
  - Fixed "quoted string not terminated" error.
data/ext/oj/cache.c CHANGED
@@ -28,6 +28,22 @@
28
28
  // almost the Murmur hash algorithm
29
29
  #define M 0x5bd1e995
30
30
 
31
+ uint64_t oj_hash_seed = 0;
32
+
33
+ void oj_hash_seed_init(void) {
34
+ static bool seeded = false;
35
+
36
+ if (seeded) {
37
+ return;
38
+ }
39
+ seeded = true;
40
+ #if HAVE_RB_HASH_START
41
+ oj_hash_seed = (uint64_t)rb_hash_start(0);
42
+ #else
43
+ oj_hash_seed = (uint64_t)NUM2LL(rb_funcall(rb_str_new_cstr("oj"), rb_intern("hash"), 0));
44
+ #endif
45
+ }
46
+
31
47
  typedef struct _slot {
32
48
  struct _slot *next;
33
49
  VALUE val;
@@ -62,7 +78,7 @@ void cache_set_form(Cache c, VALUE (*form)(const char *str, size_t len)) {
62
78
  static uint64_t hash_calc(const uint8_t *key, size_t len) {
63
79
  const uint8_t *end = key + len;
64
80
  const uint8_t *endless = key + (len & 0xFFFFFFFC);
65
- uint64_t h = (uint64_t)len;
81
+ uint64_t h = (uint64_t)len ^ oj_hash_seed;
66
82
  uint64_t k;
67
83
 
68
84
  while (key < endless) {
data/ext/oj/cache.h CHANGED
@@ -6,9 +6,14 @@
6
6
 
7
7
  #include <ruby.h>
8
8
  #include <stdbool.h>
9
+ #include <stdint.h>
9
10
 
10
11
  #define CACHE_MAX_KEY 35
11
12
 
13
+ extern uint64_t oj_hash_seed;
14
+
15
+ extern void oj_hash_seed_init(void);
16
+
12
17
  struct _cache;
13
18
  typedef struct _cache *Cache;
14
19
 
data/ext/oj/circarray.c CHANGED
@@ -25,8 +25,12 @@ void oj_circ_array_free(CircArray ca) {
25
25
  OJ_R_FREE(ca);
26
26
  }
27
27
 
28
+ // Objects are numbered in encounter order starting at 1, so the only id that
29
+ // can extend the array is one past the highest already seen. Anything further
30
+ // is not a reference to an object the document can contain and must not be
31
+ // used to size an allocation.
28
32
  void oj_circ_array_set(CircArray ca, VALUE obj, unsigned long id) {
29
- if (0 < id && 0 != ca) {
33
+ if (0 != ca && 0 < id && id <= ca->cnt + 1) {
30
34
  unsigned long i;
31
35
 
32
36
  if (ca->size < id) {
@@ -57,7 +61,7 @@ VALUE
57
61
  oj_circ_array_get(CircArray ca, unsigned long id) {
58
62
  VALUE obj = Qnil;
59
63
 
60
- if (id <= ca->cnt && 0 != ca) {
64
+ if (0 != ca && 0 < id && id <= ca->cnt) {
61
65
  obj = ca->objs[id - 1];
62
66
  }
63
67
  return obj;
data/ext/oj/compat.c CHANGED
@@ -14,11 +14,11 @@
14
14
 
15
15
  static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
16
16
  const char *key = kval->key;
17
- int klen = kval->klen;
17
+ size_t klen = kval->klen;
18
18
  Val parent = stack_peek(&pi->stack);
19
19
 
20
20
  if (Qundef == kval->key_val && Yes == pi->options.create_ok && NULL != pi->options.create_id &&
21
- *pi->options.create_id == *key && (int)pi->options.create_id_len == klen &&
21
+ *pi->options.create_id == *key && (size_t)pi->options.create_id_len == klen &&
22
22
  0 == strncmp(pi->options.create_id, key, klen)) {
23
23
  parent->classname = oj_strndup(str, len);
24
24
  parent->clen = len;
@@ -27,7 +27,7 @@ static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, c
27
27
  volatile VALUE rkey = oj_calc_hash_key(pi, kval);
28
28
 
29
29
  if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
30
- VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
30
+ VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, len);
31
31
 
32
32
  if (Qnil != clas) {
33
33
  rstr = rb_funcall(clas, oj_json_create_id, 1, rstr);
@@ -84,7 +84,7 @@ static void add_cstr(ParseInfo pi, const char *str, size_t len, const char *orig
84
84
  volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
85
85
 
86
86
  if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
87
- VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
87
+ VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, len);
88
88
 
89
89
  if (Qnil != clas) {
90
90
  pi->stack.head->val = rb_funcall(clas, oj_json_create_id, 1, rstr);
@@ -155,7 +155,7 @@ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const c
155
155
  volatile VALUE rstr = oj_cstr_to_value(str, len, (size_t)pi->options.cache_str);
156
156
 
157
157
  if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
158
- VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
158
+ VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, len);
159
159
 
160
160
  if (Qnil != clas) {
161
161
  rb_ary_push(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));
data/ext/oj/custom.c CHANGED
@@ -640,7 +640,9 @@ static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out)
640
640
  if (rb_obj_is_kind_of(obj, rb_eException)) {
641
641
  volatile VALUE rv;
642
642
 
643
- if (',' != *(out->cur - 1)) {
643
+ // An exception that was never raised has no instance variables, so
644
+ // without the class name nothing has been written since the '{'.
645
+ if (',' != *(out->cur - 1) && '{' != *(out->cur - 1)) {
644
646
  *out->cur++ = ',';
645
647
  }
646
648
  // message
@@ -896,11 +898,11 @@ void oj_dump_custom_val(VALUE obj, int depth, Out out, bool as_ok) {
896
898
 
897
899
  static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, const char *orig) {
898
900
  const char *key = kval->key;
899
- int klen = kval->klen;
901
+ size_t klen = kval->klen;
900
902
  Val parent = stack_peek(&pi->stack);
901
903
 
902
904
  if (Qundef == kval->key_val && Yes == pi->options.create_ok && NULL != pi->options.create_id &&
903
- *pi->options.create_id == *key && (int)pi->options.create_id_len == klen &&
905
+ *pi->options.create_id == *key && (size_t)pi->options.create_id_len == klen &&
904
906
  0 == strncmp(pi->options.create_id, key, klen)) {
905
907
  parent->clas = oj_name2class(pi, str, len, false, rb_eArgError);
906
908
  if (2 == klen && '^' == *key && 'o' == key[1]) {
@@ -915,7 +917,7 @@ static void hash_set_cstr(ParseInfo pi, Val kval, const char *str, size_t len, c
915
917
  volatile VALUE rkey = oj_calc_hash_key(pi, kval);
916
918
 
917
919
  if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
918
- VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
920
+ VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, len);
919
921
 
920
922
  if (Qnil != clas) {
921
923
  rstr = rb_funcall(clas, oj_json_create_id, 1, rstr);
@@ -1020,7 +1022,7 @@ static void array_append_cstr(ParseInfo pi, const char *str, size_t len, const c
1020
1022
  volatile VALUE rstr = rb_utf8_str_new(str, len);
1021
1023
 
1022
1024
  if (Yes == pi->options.create_ok && NULL != pi->options.str_rx.head) {
1023
- VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, (int)len);
1025
+ VALUE clas = oj_rxclass_match(&pi->options.str_rx, str, len);
1024
1026
 
1025
1027
  if (Qnil != clas) {
1026
1028
  rb_ary_push(stack_peek(&pi->stack)->val, rb_funcall(clas, oj_json_create_id, 1, rstr));
data/ext/oj/dump.c CHANGED
@@ -1456,10 +1456,11 @@ void oj_dump_raw(const char *str, size_t cnt, Out out) {
1456
1456
  }
1457
1457
 
1458
1458
  void oj_out_init(Out out) {
1459
- out->buf = out->stack_buffer;
1460
- out->cur = out->buf;
1461
- out->end = out->buf + sizeof(out->stack_buffer) - BUFFER_EXTRA;
1462
- out->allocated = false;
1459
+ out->buf = out->stack_buffer;
1460
+ out->cur = out->buf;
1461
+ out->end = out->buf + sizeof(out->stack_buffer) - BUFFER_EXTRA;
1462
+ out->allocated = false;
1463
+ out->key_filter_off = false;
1463
1464
  }
1464
1465
 
1465
1466
  void oj_out_free(Out out) {
@@ -1731,6 +1732,11 @@ void oj_dump_float(VALUE obj, int depth, Out out, bool as_ok) {
1731
1732
  size_t oj_dump_float_printf(char *buf, size_t blen, VALUE obj, double d, const char *format) {
1732
1733
  size_t cnt = snprintf(buf, blen, format, d);
1733
1734
 
1735
+ // snprintf() returns the length the output would have needed, which is
1736
+ // more than it wrote when the format asks for more room than buf has.
1737
+ if (blen <= cnt) {
1738
+ cnt = blen - 1;
1739
+ }
1734
1740
  // Round off issues at 16 significant digits so check for obvious ones of
1735
1741
  // 0001 and 9999.
1736
1742
  if (17 <= cnt && (0 == strcmp("0001", buf + cnt - 4) || 0 == strcmp("9999", buf + cnt - 4))) {
@@ -1742,6 +1748,21 @@ size_t oj_dump_float_printf(char *buf, size_t blen, VALUE obj, double d, const c
1742
1748
  return cnt;
1743
1749
  }
1744
1750
 
1751
+ // The only and except lists are the keys joined with a ':' and wrapped in one
1752
+ // at each end, so a key is in a list when ":key:" is in it. Matching in place
1753
+ // finds what searching the list for a copy of the key with the colons on it
1754
+ // found, without a buffer sized by the key.
1755
+ static bool key_listed(const char *list, const char *key, size_t klen) {
1756
+ const char *s;
1757
+
1758
+ for (s = list; NULL != (s = strchr(s, ':')); s++) {
1759
+ if (0 == strncmp(s + 1, key, klen) && ':' == s[klen + 1]) {
1760
+ return true;
1761
+ }
1762
+ }
1763
+ return false;
1764
+ }
1765
+
1745
1766
  bool oj_key_skip(VALUE key, const char *only, const char *except) {
1746
1767
  const char *skey;
1747
1768
 
@@ -1751,15 +1772,9 @@ bool oj_key_skip(VALUE key, const char *only, const char *except) {
1751
1772
  default: skey = NULL; break;
1752
1773
  }
1753
1774
  if (NULL != skey && '\0' != *skey) {
1754
- size_t size = strlen(skey);
1755
- char *buf = alloca(size + 3);
1756
-
1757
- buf[0] = ':';
1758
- strcpy(buf + 1, skey);
1759
- buf[size + 1] = ':';
1760
- buf[size + 2] = '\0';
1775
+ size_t klen = strlen(skey);
1761
1776
 
1762
- return ((NULL != only && NULL == strstr(only, buf)) || (NULL != except && NULL != strstr(except, buf)));
1777
+ return ((NULL != only && !key_listed(only, skey, klen)) || (NULL != except && key_listed(except, skey, klen)));
1763
1778
  }
1764
1779
  return NULL != only;
1765
1780
  }
data/ext/oj/dump.h CHANGED
@@ -70,9 +70,19 @@ inline static void assure_size(Out out, size_t len) {
70
70
  }
71
71
  }
72
72
 
73
+ // Bytes that fill_indent(), or the dump_opts equivalent used when :indent is a
74
+ // String, writes at this depth. nl_size is array_size or hash_size.
75
+ inline static size_t indent_len(Out out, int depth, size_t nl_size) {
76
+ if (out->opts->dump_opts.use) {
77
+ return depth * out->opts->dump_opts.indent_size + nl_size;
78
+ }
79
+ return depth * out->indent + 1;
80
+ }
81
+
73
82
  inline static void fill_indent(Out out, int cnt) {
74
83
  if (0 < out->indent) {
75
84
  cnt *= out->indent;
85
+ assure_size(out, cnt + 1);
76
86
  *out->cur++ = '\n';
77
87
  memset(out->cur, ' ', cnt);
78
88
  out->cur += cnt;
data/ext/oj/dump_compat.c CHANGED
@@ -162,6 +162,7 @@ static void dump_array(VALUE a, int depth, Out out, bool as_ok) {
162
162
  }
163
163
  }
164
164
  } else {
165
+ assure_size(out, size);
165
166
  fill_indent(out, d2);
166
167
  }
167
168
  oj_dump_compat_val(RARRAY_AREF(a, i), d2, out, true);
@@ -298,9 +299,10 @@ static ID message_id = 0;
298
299
  static ID backtrace_id = 0;
299
300
 
300
301
  static void exception_alt(VALUE obj, int depth, Out out) {
301
- int d3 = depth + 2;
302
- size_t size = d3 * out->indent + 2;
303
- size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
302
+ int d3 = depth + 2;
303
+ size_t size = d3 * out->indent + 2;
304
+ size_t sep_len = out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2;
305
+ volatile VALUE bt;
304
306
 
305
307
  if (0 == message_id) {
306
308
  message_id = rb_intern("message");
@@ -331,7 +333,12 @@ static void exception_alt(VALUE obj, int depth, Out out) {
331
333
  if (0 < out->opts->dump_opts.after_size) {
332
334
  APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
333
335
  }
334
- dump_array(rb_funcall(obj, backtrace_id, 0), depth, out, false);
336
+ bt = rb_funcall(obj, backtrace_id, 0);
337
+ if (T_ARRAY == rb_type(bt)) {
338
+ dump_array(bt, depth, out, false);
339
+ } else {
340
+ oj_dump_nil(Qnil, depth, out, false);
341
+ }
335
342
  fill_indent(out, depth);
336
343
  *out->cur++ = '}';
337
344
  *out->cur = '\0';
data/ext/oj/dump_object.c CHANGED
@@ -10,6 +10,24 @@ static const char hex_chars[17] = "0123456789abcdef";
10
10
 
11
11
  static void dump_obj_attrs(VALUE obj, VALUE clas, slot_t id, int depth, Out out);
12
12
 
13
+ // An odd class is dumped as {"^O":class,...} and is rebuilt with a create
14
+ // function when loaded so there is no place to put a "^i" circular id on it.
15
+ // Registering it in the circular cache would consume an id that is never
16
+ // written and any later occurrence would then be dumped as a "^r" that refers
17
+ // to nothing. Instead dump odd classes in full each time they are encountered
18
+ // just like Time, Rational, and Complex are.
19
+ static void dump_circular_obj(VALUE obj, VALUE clas, int depth, Out out) {
20
+ if (NULL != oj_get_odd(clas)) {
21
+ dump_obj_attrs(obj, clas, 0, depth, out);
22
+ } else {
23
+ long id = oj_check_circular(obj, out);
24
+
25
+ if (0 <= id) {
26
+ dump_obj_attrs(obj, clas, id, depth, out);
27
+ }
28
+ }
29
+ }
30
+
13
31
  static void dump_time(VALUE obj, Out out) {
14
32
  switch (out->opts->time_format) {
15
33
  case RubyTime:
@@ -47,11 +65,7 @@ static void dump_data(VALUE obj, int depth, Out out, bool as_ok) {
47
65
  oj_dump_cstr(str, len, 0, 0, out);
48
66
  }
49
67
  } else {
50
- long id = oj_check_circular(obj, out);
51
-
52
- if (0 <= id) {
53
- dump_obj_attrs(obj, clas, id, depth, out);
54
- }
68
+ dump_circular_obj(obj, clas, depth, out);
55
69
  }
56
70
  }
57
71
  }
@@ -74,11 +88,7 @@ static void dump_obj(VALUE obj, int depth, Out out, bool as_ok) {
74
88
  oj_dump_raw(str, len, out);
75
89
  }
76
90
  } else {
77
- long id = oj_check_circular(obj, out);
78
-
79
- if (0 <= id) {
80
- dump_obj_attrs(obj, clas, id, depth, out);
81
- }
91
+ dump_circular_obj(obj, clas, depth, out);
82
92
  }
83
93
  }
84
94
 
data/ext/oj/extconf.rb CHANGED
@@ -32,6 +32,7 @@ have_func('pthread_mutex_init')
32
32
  have_func('getrlimit', 'sys/resource.h')
33
33
  have_func('rb_enc_interned_str')
34
34
  have_func('rb_ext_ractor_safe', 'ruby.h')
35
+ have_func('rb_hash_start', 'ruby.h')
35
36
 
36
37
  dflags['OJ_DEBUG'] = true unless ENV['OJ_DEBUG'].nil?
37
38