json 3.1.0 → 3.2.0

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: f2c5d1d2dfae6ea305b69988b2f277305a0b52f02a7a047c2a5d0602a54546f8
4
- data.tar.gz: 3840fd11930f94e94b1ddbe6fede6831662192b93c0e1e5bf8296c858e93cf4e
3
+ metadata.gz: dfa0b8944bdc1b1430778f602a291f704d6a127069ba231a511d9493f251aa91
4
+ data.tar.gz: c78507e03c6327dabda8a08a3c821217fb2e2509344ef3d48fb2dfb1ad734ebc
5
5
  SHA512:
6
- metadata.gz: 26e58633051da64fee887463df8df3b11ec934557726e9affbd96c04115f393ca05e164e9b1bfbcaa73ba7431ce0951d48b1f6214ad05b9ef13ab98e160b9f65
7
- data.tar.gz: 6b83a6ff45819906f284a4851cad7b33c214dcc2794d74aa5a8a27ef80b8ed0c34ca853957a444ef1188a92480a2eaa92ca188dcedef4cbf37c1ea271ea0d551
6
+ metadata.gz: 2a64146f23831a94b36b2bbe194d275f8e169237cac77ec47614342dbc53ee5e467bd41fb87a1a71031ca6aecc83f023f96acad719ecb6c91c8877644332a458
7
+ data.tar.gz: 947a6b55a76493981309be2e3b3f79bb60afe7d4c8db76ee1855023faf66e96dc099238f273ad9fad01c19b2e53d2d23f1d20b55880ab5f268b026c869aa738d
data/CHANGES.md CHANGED
@@ -2,6 +2,18 @@
2
2
 
3
3
  ### Unreleased
4
4
 
5
+ ### 2026-11-24 (3.2.0)
6
+
7
+ * The `script_safe: true` option of `JSON.generate` no longer escapes forward slashes `/`, and instead escapes script tag
8
+ openings (`<script`), script tag closing (`</script`) and HTML comments (`<!--`) as recommended by the HTML spec.
9
+ * The `script_safe: true` option of `JSON.generate` no longer escapes `U+2028` and `U+2029` given that Javascript is
10
+ now a proper subset of JSON since many years (see: https://github.com/tc39/proposal-json-superset).
11
+ * Fix `JSON::ParserError#json_path` raising `NoMethodError` when the document was parsed with `symbolize_names: true`.
12
+ * Fix `JSON.generate` not detecting duplicate keys such as `{ "1" => 1, 1 => 2 }`.
13
+ * JRuby: raise `TypeError` when a String subclass used as a Hash key has a `to_s` that doesn't return a String, like the C extension.
14
+ * JRuby and TruffleRuby: fix a stack overflow in `JSON::Coder` when `as_json` returns a Symbol for a string with invalid encoding.
15
+ * JRuby: fix `JSON::Coder` (and any frozen `JSON::State`) not being thread-safe. Each `#dump` call now uses its own nesting depth instead of mutating the shared state, which under concurrent use could make every later call raise `JSON::NestingError`.
16
+
5
17
  ### 2026-10-13 (3.1.0)
6
18
 
7
19
  * Added `rfc8785` generator option to canonicalize the output. Replaces the `json-canonicalization` with numerous correctness fixes.
@@ -138,6 +150,15 @@ Numerous rarely used aliases have been removed.
138
150
  * Fix `allow_blank` parsing option to no longer allow invalid types (e.g. `load([], allow_blank: true)` now raise a type error).
139
151
  * Add `allow_invalid_escape` parsing option to ignore backslashes that aren't followed by one of the valid escape characters.
140
152
 
153
+ ### 2026-11-13 (2.18.3)
154
+
155
+ * Backport format string injection fix from 2.19.2. `CVE-2026-33210` / GHSA-3m6g-2423-7cp3.
156
+
157
+ ### 2026-11-02 (2.18.2)
158
+
159
+ * Backport handling of unescaped control characters preceeded by a backslash from 2.19.3.
160
+ GHSA-cjpr-jw2g-fw99.
161
+
141
162
  ### 2026-02-03 (2.18.1)
142
163
 
143
164
  * Fix a potential crash in very specific circumstance if GC triggers during a call to `to_json`
@@ -33,7 +33,7 @@ typedef struct FBufferStruct {
33
33
  static void fbuffer_free(FBuffer *fb);
34
34
  static void fbuffer_clear(FBuffer *fb);
35
35
  static void fbuffer_append(FBuffer *fb, const char *newstr, size_t len);
36
- static void fbuffer_append_long(FBuffer *fb, long number);
36
+ static void fbuffer_append_len(FBuffer *fb, rb_len_t number);
37
37
  static inline void fbuffer_append_char(FBuffer *fb, char newchr);
38
38
  static VALUE fbuffer_finalize(FBuffer *fb);
39
39
 
@@ -218,7 +218,7 @@ static inline void fbuffer_advance_to(FBuffer *fb, char *end)
218
218
  /*
219
219
  * Appends the decimal string representation of \a number into the buffer.
220
220
  */
221
- static void fbuffer_append_long(FBuffer *fb, long number)
221
+ static void fbuffer_append_len(FBuffer *fb, rb_len_t number)
222
222
  {
223
223
  /*
224
224
  * The jeaiii_ultoa() function produces digits left-to-right,
@@ -231,9 +231,9 @@ static void fbuffer_append_long(FBuffer *fb, long number)
231
231
  * generated is therefore 20 (including a potential sign character).
232
232
  */
233
233
 
234
- static const int MAX_CHARS_FOR_LONG = 20;
234
+ static const int MAX_CHARS_FOR_LEN = 20;
235
235
 
236
- fbuffer_inc_capa(fb, MAX_CHARS_FOR_LONG);
236
+ fbuffer_inc_capa(fb, MAX_CHARS_FOR_LEN);
237
237
 
238
238
  if (number < 0) {
239
239
  fbuffer_append_reserved_char(fb, '-');
@@ -59,7 +59,12 @@ struct generate_json_data {
59
59
  long depth;
60
60
  };
61
61
 
62
+ #if defined(HAVE_SIMD) && defined(HAVE_SIMD_SSE2)
62
63
  static SIMD_Implementation simd_impl;
64
+ # define init_simd_impl() (simd_impl = find_simd_implementation())
65
+ #else
66
+ # define init_simd_impl() (void)0
67
+ #endif
63
68
 
64
69
  static VALUE cState_from_state_s(VALUE self, VALUE opts);
65
70
  static VALUE cState_partial_generate(VALUE self, VALUE obj, generator_func, VALUE io);
@@ -99,8 +104,11 @@ NORETURN(static void) raise_generator_error(VALUE invalid_object, const char *fm
99
104
 
100
105
  // 0 - single byte char that don't need to be escaped.
101
106
  // (x | 8) - char that needs to be escaped.
102
- static const unsigned char CHAR_LENGTH_MASK = 7;
103
- static const unsigned char ESCAPE_MASK = 8;
107
+ #define CHAR_LENGTH_MASK 7
108
+ #define ESCAPE_MASK 8
109
+ #define HTML_ESCAPE_MASK 16
110
+ #define ESCAPE_LEN_1 9 // ESCAPE_MASK | 1
111
+ #define HTML_ESCAPE_LEN_1 25 // HTML_ESCAPE_MASK | ESCAPE_LEN_1
104
112
 
105
113
  typedef struct _search_state {
106
114
  const char *ptr;
@@ -150,6 +158,84 @@ static const unsigned char escape_table_basic[256] = {
150
158
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
151
159
  };
152
160
 
161
+ static const unsigned char script_safe_escape_table[256] = {
162
+ // ASCII Control Characters
163
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
164
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
165
+ // ASCII Characters
166
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
167
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,25, 0, 0, 0, // '<'
168
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
169
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
170
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
171
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
172
+ // Continuation byte
173
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
174
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
175
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
176
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
177
+ // First byte of a 2-byte code point
178
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
179
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
180
+ // First byte of a 3-byte code point
181
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
182
+ //First byte of a 4+ byte code point
183
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
184
+ };
185
+
186
+ static const unsigned char ascii_only_escape_table[256] = {
187
+ // ASCII Control Characters
188
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
189
+ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
190
+ // ASCII Characters
191
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
192
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
193
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
194
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
195
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
196
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
197
+ // Continuation byte
198
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
199
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
200
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
202
+ // First byte of a 2-byte code point
203
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
204
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
205
+ // First byte of a 3-byte code point
206
+ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
207
+ //First byte of a 4+ byte code point
208
+ 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
209
+ };
210
+
211
+ static inline bool unsafe_html_tag_p(const char *ptr, const char *end)
212
+ {
213
+ size_t remaining = end - ptr;
214
+
215
+ if (remaining < 4) return false;
216
+ if (memcmp(ptr, "<!--", 4) == 0) return true;
217
+
218
+ union { uint64_t num; char bytes[8]; } chunk;
219
+ if (remaining >= 8) {
220
+ memcpy(chunk.bytes, ptr, 8);
221
+ }
222
+ else if (remaining == 7) {
223
+ chunk.num = 0;
224
+ memcpy(chunk.bytes, ptr, 7);
225
+ }
226
+ else {
227
+ return false;
228
+ }
229
+
230
+ chunk.num |= 0x2020202020202020;
231
+
232
+ if (memcmp(chunk.bytes, "</script", 8) == 0) return true;
233
+
234
+ chunk.bytes[7] = 0;
235
+
236
+ return memcmp(chunk.bytes, "<script\0", 8) == 0;
237
+ }
238
+
153
239
  static inline unsigned char search_escape_basic(search_state *search)
154
240
  {
155
241
  while (search->ptr < search->end) {
@@ -199,8 +285,7 @@ ALWAYS_INLINE(static) void escape_UTF8_char_basic(search_state *search)
199
285
  *
200
286
  * - If out_ascii_only: non-ASCII characters (>0x7F)
201
287
  *
202
- * - If script_safe: forwardslash (/), line separator (U+2028), and
203
- * paragraph separator (U+2029)
288
+ * - If script_safe: script tags (`<script`, `</script`) and HTML comments (`<!--`).
204
289
  *
205
290
  * Everything else (should be UTF-8) is just passed through and
206
291
  * appended to the result.
@@ -244,7 +329,9 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
244
329
  {
245
330
  const unsigned char ch = (unsigned char)*search->ptr;
246
331
  switch (ch_len) {
247
- case 1: {
332
+ case ESCAPE_LEN_1: {
333
+ search_flush(search);
334
+
248
335
  switch (ch) {
249
336
  case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
250
337
  case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
@@ -265,16 +352,18 @@ static inline void escape_UTF8_char(search_state *search, unsigned char ch_len)
265
352
  }
266
353
  break;
267
354
  }
268
- case 3: {
269
- if (search->ptr[2] & 1) {
270
- fbuffer_append(search->buffer, "\\u2029", 6);
355
+ case HTML_ESCAPE_LEN_1: {
356
+ if (unsafe_html_tag_p(search->ptr, search->end)) {
357
+ search_flush(search);
358
+ fbuffer_append(search->buffer, "\\u003c", 6);
271
359
  } else {
272
- fbuffer_append(search->buffer, "\\u2028", 6);
360
+ search->ptr++;
361
+ return; // no escape needed, resume search without updating cursor.
273
362
  }
274
363
  break;
275
364
  }
276
365
  }
277
- search->cursor = (search->ptr += ch_len);
366
+ search->cursor = (search->ptr += (ch_len & CHAR_LENGTH_MASK));
278
367
  }
279
368
 
280
369
  #ifdef HAVE_SIMD
@@ -507,31 +596,6 @@ ALWAYS_INLINE(static) TARGET_SSE2 unsigned char search_escape_basic_sse2(search_
507
596
 
508
597
  #endif /* HAVE_SIMD */
509
598
 
510
- static const unsigned char script_safe_escape_table[256] = {
511
- // ASCII Control Characters
512
- 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
513
- 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
514
- // ASCII Characters
515
- 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, // '"' and '/'
516
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
517
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
518
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
519
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
520
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
521
- // Continuation byte
522
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
523
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
524
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
525
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
526
- // First byte of a 2-byte code point
527
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
528
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
529
- // First byte of a 3-byte code point
530
- 3, 3,11, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // 0xE2 is the start of \u2028 and \u2029
531
- //First byte of a 4+ byte code point
532
- 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
533
- };
534
-
535
599
  static inline unsigned char search_script_safe_escape(search_state *search)
536
600
  {
537
601
  while (search->ptr < search->end) {
@@ -540,16 +604,9 @@ static inline unsigned char search_script_safe_escape(search_state *search)
540
604
 
541
605
  if (RB_UNLIKELY(ch_len)) {
542
606
  if (ch_len & ESCAPE_MASK) {
543
- if (RB_UNLIKELY(ch_len == 11)) {
544
- const unsigned char *uptr = (const unsigned char *)search->ptr;
545
- if (!(uptr[1] == 0x80 && (uptr[2] >> 1) == 0x54)) {
546
- search->ptr += 3;
547
- continue;
548
- }
549
- }
550
- search_flush(search);
551
- return ch_len & CHAR_LENGTH_MASK;
552
- } else {
607
+ return ch_len;
608
+ }
609
+ else {
553
610
  search->ptr += ch_len;
554
611
  }
555
612
  } else {
@@ -568,31 +625,6 @@ static void convert_UTF8_to_script_safe_JSON(search_state *search)
568
625
  }
569
626
  }
570
627
 
571
- static const unsigned char ascii_only_escape_table[256] = {
572
- // ASCII Control Characters
573
- 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
574
- 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
575
- // ASCII Characters
576
- 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // '"'
577
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
578
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
579
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, // '\\'
580
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
581
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
582
- // Continuation byte
583
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
584
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
585
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
586
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
587
- // First byte of a 2-byte code point
588
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
589
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
590
- // First byte of a 3-byte code point
591
- 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
592
- //First byte of a 4+ byte code point
593
- 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 9, 9,
594
- };
595
-
596
628
  static inline unsigned char search_ascii_only_escape(search_state *search, const unsigned char escape_table[256])
597
629
  {
598
630
  while (search->ptr < search->end) {
@@ -600,8 +632,7 @@ static inline unsigned char search_ascii_only_escape(search_state *search, const
600
632
  unsigned char ch_len = escape_table[ch];
601
633
 
602
634
  if (RB_UNLIKELY(ch_len)) {
603
- search_flush(search);
604
- return ch_len & CHAR_LENGTH_MASK;
635
+ return ch_len;
605
636
  } else {
606
637
  search->ptr++;
607
638
  }
@@ -614,7 +645,9 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
614
645
  {
615
646
  const unsigned char ch = (unsigned char)*search->ptr;
616
647
  switch (ch_len) {
617
- case 1: {
648
+ case ESCAPE_LEN_1: {
649
+ search_flush(search);
650
+
618
651
  switch (ch) {
619
652
  case '"': fbuffer_append(search->buffer, "\\\"", 2); break;
620
653
  case '\\': fbuffer_append(search->buffer, "\\\\", 2); break;
@@ -635,7 +668,19 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
635
668
  }
636
669
  break;
637
670
  }
671
+ case HTML_ESCAPE_LEN_1: {
672
+ if (unsafe_html_tag_p(search->ptr, search->end)) {
673
+ search_flush(search);
674
+ fbuffer_append(search->buffer, "\\u003c", 6);
675
+ } else {
676
+ search->ptr++;
677
+ return; // no escape needed, resume search without updating cursor.
678
+ }
679
+ break;
680
+ }
638
681
  default: {
682
+ search_flush(search);
683
+
639
684
  const char *hexdig = "0123456789abcdef";
640
685
  char scratch[12] = { '\\', 'u', 0, 0, 0, 0, '\\', 'u' };
641
686
 
@@ -685,7 +730,7 @@ static inline void full_escape_UTF8_char(search_state *search, unsigned char ch_
685
730
  break;
686
731
  }
687
732
  }
688
- search->cursor = (search->ptr += ch_len);
733
+ search->cursor = (search->ptr += (ch_len & CHAR_LENGTH_MASK));
689
734
  }
690
735
 
691
736
  static void convert_UTF8_to_ASCII_only_JSON(search_state *search, const unsigned char escape_table[256])
@@ -1020,6 +1065,9 @@ json_object_i(VALUE key, VALUE val, VALUE _arg)
1020
1065
  raise_generator_error(key, "%"PRIsVALUE" not allowed as object key in JSON", CLASS_OF(key));
1021
1066
  }
1022
1067
  }
1068
+
1069
+ json_inspect_hash_with_mixed_keys(arg);
1070
+
1023
1071
  key_to_s = rb_convert_type(key, T_STRING, "String", "to_s");
1024
1072
  break;
1025
1073
  }
@@ -1158,7 +1206,7 @@ static void generate_json_fixnum(FBuffer *buffer, struct generate_json_data *dat
1158
1206
  generate_json_rfc8785_number(buffer, obj);
1159
1207
  return;
1160
1208
  }
1161
- fbuffer_append_long(buffer, FIX2LONG(obj));
1209
+ fbuffer_append_len(buffer, FIX2LEN(obj));
1162
1210
  }
1163
1211
 
1164
1212
  static void generate_json_bignum(FBuffer *buffer, struct generate_json_data *data, VALUE obj)
@@ -1677,7 +1725,7 @@ static VALUE cState_max_nesting_set(VALUE self, VALUE depth)
1677
1725
  /*
1678
1726
  * call-seq: script_safe
1679
1727
  *
1680
- * If this boolean is true, the forward slashes will be escaped in
1728
+ * If this boolean is true, script tags and HTML comments will be escaped in
1681
1729
  * the json output.
1682
1730
  */
1683
1731
  static VALUE cState_script_safe(VALUE self)
@@ -1689,7 +1737,7 @@ static VALUE cState_script_safe(VALUE self)
1689
1737
  /*
1690
1738
  * call-seq: script_safe=(enable)
1691
1739
  *
1692
- * This sets whether or not the forward slashes will be escaped in
1740
+ * This sets whether or not script tags and HTML comments will be escaped in
1693
1741
  * the json output.
1694
1742
  */
1695
1743
  static VALUE cState_script_safe_set(VALUE self, VALUE enable)
@@ -2168,5 +2216,5 @@ void Init_generator(void)
2168
2216
 
2169
2217
  rb_require("json/ext/generator/state");
2170
2218
 
2171
- simd_impl = find_simd_implementation();
2219
+ init_simd_impl();
2172
2220
  }
data/ext/json/ext/json.h CHANGED
@@ -7,7 +7,14 @@
7
7
 
8
8
  #ifndef HAVE_RB_LEN_T
9
9
  typedef long rb_len_t;
10
+ #endif
11
+ #ifndef FIX2LEN
12
+ # define FIX2LEN FIX2LONG
13
+ #endif
14
+ #ifndef LEN2NUM
10
15
  # define LEN2NUM LONG2NUM
16
+ #endif
17
+ #ifndef PRIdLEN
11
18
  # define PRIdLEN "ld"
12
19
  #endif
13
20
 
@@ -1120,7 +1120,7 @@ NOINLINE(static) VALUE json_decode_large_integer(const char *start, rb_len_t len
1120
1120
  return json_decode_large_number(start, len, json_decode_inum);
1121
1121
  }
1122
1122
 
1123
- static inline VALUE json_decode_integer(uint64_t mantissa, int mantissa_digits, bool negative, const char *start, const char *end)
1123
+ static inline VALUE json_decode_integer(uint64_t mantissa, ssize_t mantissa_digits, bool negative, const char *start, const char *end)
1124
1124
  {
1125
1125
  if (RB_LIKELY(mantissa_digits <= MAX_FAST_INTEGER_SIZE)) {
1126
1126
  if (RB_LIKELY(!negative)) {
@@ -1164,7 +1164,7 @@ NOINLINE(static) VALUE json_decode_large_float(const char *start, rb_len_t len)
1164
1164
  /* Ruby JSON optimized float decoder using vendored Ryu algorithm
1165
1165
  * Accepts pre-extracted mantissa and exponent from first-pass validation
1166
1166
  */
1167
- static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantissa, int mantissa_digits, int64_t exponent, bool negative,
1167
+ static inline VALUE json_decode_float(JSON_ParserConfig *config, uint64_t mantissa, ssize_t mantissa_digits, int64_t exponent, bool negative,
1168
1168
  const char *start, const char *end)
1169
1169
  {
1170
1170
  if (RB_UNLIKELY(config->decimal_class)) {
@@ -1421,7 +1421,7 @@ static inline uint64_t decode_4digits_unrolled(uint32_t val) {
1421
1421
  }
1422
1422
  #endif
1423
1423
 
1424
- static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulator)
1424
+ static inline ssize_t json_parse_digits(JSON_ParserState *state, uint64_t *accumulator)
1425
1425
  {
1426
1426
  const char *start = state->cursor;
1427
1427
 
@@ -1454,7 +1454,7 @@ static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulat
1454
1454
  state->cursor++;
1455
1455
  }
1456
1456
 
1457
- return (int)(state->cursor - start);
1457
+ return (ssize_t)(state->cursor - start);
1458
1458
  }
1459
1459
  #endif
1460
1460
 
@@ -1463,7 +1463,7 @@ static inline int json_parse_digits(JSON_ParserState *state, uint64_t *accumulat
1463
1463
  *accumulator = *accumulator * 10 + (next_char - '0');
1464
1464
  state->cursor++;
1465
1465
  }
1466
- return (int)(state->cursor - start);
1466
+ return (ssize_t)(state->cursor - start);
1467
1467
  }
1468
1468
 
1469
1469
  static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig *config, bool negative, const char *start, bool resumable)
@@ -1473,11 +1473,11 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
1473
1473
 
1474
1474
  // Variables for Ryu optimization - extract digits during parsing
1475
1475
  int64_t exponent = 0;
1476
- int decimal_point_pos = -1;
1476
+ ssize_t decimal_point_pos = -1;
1477
1477
  uint64_t mantissa = 0;
1478
1478
 
1479
1479
  // Parse integer part and extract mantissa digits
1480
- int mantissa_digits = json_parse_digits(state, &mantissa);
1480
+ ssize_t mantissa_digits = json_parse_digits(state, &mantissa);
1481
1481
 
1482
1482
  if (RB_UNLIKELY((first_digit == '0' && mantissa_digits > 1) || (negative && mantissa_digits == 0))) {
1483
1483
  return Qundef;
@@ -1489,7 +1489,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
1489
1489
  decimal_point_pos = mantissa_digits; // Remember position of decimal point
1490
1490
  state->cursor++;
1491
1491
 
1492
- int fractional_digits = json_parse_digits(state, &mantissa);
1492
+ ssize_t fractional_digits = json_parse_digits(state, &mantissa);
1493
1493
  mantissa_digits += fractional_digits;
1494
1494
 
1495
1495
  if (RB_UNLIKELY(!fractional_digits)) {
@@ -1510,7 +1510,7 @@ static inline VALUE json_parse_number(JSON_ParserState *state, JSON_ParserConfig
1510
1510
  }
1511
1511
 
1512
1512
  uint64_t abs_exponent = 0;
1513
- int exponent_digits = json_parse_digits(state, &abs_exponent);
1513
+ ssize_t exponent_digits = json_parse_digits(state, &abs_exponent);
1514
1514
 
1515
1515
  if (RB_UNLIKELY(!exponent_digits)) {
1516
1516
  return Qundef;
@@ -2261,6 +2261,7 @@ typedef struct JSON_ResumableParserStruct {
2261
2261
  size_t incomplete_bytes;
2262
2262
  bool complete;
2263
2263
  bool in_use;
2264
+ bool initialized;
2264
2265
  } JSON_ResumableParser;
2265
2266
 
2266
2267
  static void JSON_ResumableParser_mark(void *ptr)
@@ -2397,9 +2398,15 @@ static VALUE cResumableParser_initialize(int argc, VALUE *argv, VALUE self)
2397
2398
  {
2398
2399
  rb_check_frozen(self);
2399
2400
 
2401
+ JSON_ResumableParser *parser = cResumableParser_get(self);
2402
+
2403
+ if (parser->initialized) {
2404
+ rb_raise(rb_eTypeError, "already initialized parser");
2405
+ }
2406
+ parser->initialized = true;
2407
+
2400
2408
  VALUE opts = Qfalse;
2401
2409
  rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS, argc, argv, "0:", &opts);
2402
- JSON_ResumableParser *parser = cResumableParser_get(self);
2403
2410
 
2404
2411
  opts = argc > 0 ? argv[0] : Qnil;
2405
2412
  parser_config_init(&parser->config, opts, self, true);
@@ -1,16 +1,15 @@
1
1
  case RbConfig::CONFIG['host_cpu']
2
2
  when /^(arm|aarch64)/
3
3
  # Try to compile a small program using NEON instructions
4
- header, type, init, extra = 'arm_neon.h', 'uint8x16_t', 'vdupq_n_u8(32)', nil
4
+ header, type, init = 'arm_neon.h', 'uint8x16_t', 'vdupq_n_u8(32)'
5
5
  when /^(x86_64|x64)/
6
- header, type, init, extra = 'x86intrin.h', '__m128i', '_mm_set1_epi8(32)', 'if (__builtin_cpu_supports("sse2")) { printf("OK"); }'
6
+ header, type, init = 'x86intrin.h', '__m128i', '_mm_set1_epi8(32)'
7
7
  end
8
8
  if header
9
9
  if have_header(header) && try_compile(<<~SRC, '-Werror=implicit-function-declaration')
10
10
  #{cpp_include(header)}
11
11
  int main(int argc, char **argv) {
12
12
  #{type} test = #{init};
13
- #{extra}
14
13
  if (argc > 100000) printf("%p", &test);
15
14
  return 0;
16
15
  }
@@ -20,5 +19,3 @@ if header
20
19
  puts "Disable SIMD"
21
20
  end
22
21
  end
23
-
24
- have_header('cpuid.h')
@@ -143,7 +143,6 @@ ALWAYS_INLINE(static) int string_scan_simd_neon(const char **ptr, const char *en
143
143
  #define HAVE_SIMD 1
144
144
  #define HAVE_SIMD_SSE2 1
145
145
 
146
- #ifdef HAVE_CPUID_H
147
146
  #define FIND_SIMD_IMPLEMENTATION_DEFINED 1
148
147
 
149
148
  #if defined(__clang__) || defined(__GNUC__)
@@ -182,17 +181,10 @@ ALWAYS_INLINE(static) TARGET_SSE2 int string_scan_simd_sse2(const char **ptr, co
182
181
  return 0;
183
182
  }
184
183
 
185
- #include <cpuid.h>
186
- #endif /* HAVE_CPUID_H */
187
-
188
184
  static inline SIMD_Implementation find_simd_implementation(void)
189
185
  {
190
- // TODO Revisit. I think the SSE version now only uses SSE2 instructions.
191
- if (__builtin_cpu_supports("sse2")) {
192
- return SIMD_SSE2;
193
- }
194
-
195
- return SIMD_NONE;
186
+ // SSE2 is part of the x86-64 baseline.
187
+ return SIMD_SSE2;
196
188
  }
197
189
 
198
190
  #endif /* HAVE_X86INTRIN_H */
data/lib/json/common.rb CHANGED
@@ -171,13 +171,14 @@ module JSON
171
171
  when Integer
172
172
  "[#{segment}]"
173
173
  when String, Symbol
174
+ segment = segment.to_s
174
175
  if !segment.valid_encoding?
175
176
  segment = segment.b.gsub(/["\\]/n, { '"' => '\\"', '\\' => '\\\\' }).force_encoding(Encoding::UTF_8)
176
177
  %{["#{segment}"]}
177
178
  elsif segment.match?(/\A[a-zA-Z\$\_][a-zA-Z\$\_0-9]*\z/)
178
179
  ".#{segment}"
179
180
  else
180
- segment = segment.to_s.gsub(/["\\]/, { '"' => '\\"', '\\' => '\\\\' })
181
+ segment = segment.gsub(/["\\]/, { '"' => '\\"', '\\' => '\\\\' })
181
182
  %{["#{segment}"]}
182
183
  end
183
184
  else
@@ -37,15 +37,12 @@ module JSON
37
37
  "\x1f" => '\u001f',
38
38
  '"' => '\"',
39
39
  '\\' => '\\\\',
40
+ '<' => '\\u003c',
40
41
  }.freeze # :nodoc:
41
42
 
42
- SCRIPT_SAFE_MAP = MAP.merge(
43
- '/' => '\\/',
44
- "\u2028" => '\u2028',
45
- "\u2029" => '\u2029',
46
- ).freeze
43
+ ESCAPE_PATTERN = /["\\\x0-\x1f]/
47
44
 
48
- SCRIPT_SAFE_ESCAPE_PATTERN = /[\/"\\\x0-\x1f\u2028-\u2029]/
45
+ SCRIPT_SAFE_ESCAPE_PATTERN = Regexp.union(ESCAPE_PATTERN, /<(?=\/?script)/i, /<(?=!--)/)
49
46
 
50
47
  def self.native_type?(value) # :nodoc:
51
48
  (false == value || true == value || nil == value || String === value || Symbol === value || Array === value || Hash === value || Integer === value || Float === value || Fragment === value)
@@ -63,25 +60,16 @@ module JSON
63
60
  # Convert a UTF8 encoded Ruby string _string_ to a JSON string, encoded with
64
61
  # UTF16 big endian characters as \u????, and return it.
65
62
  def self.utf8_to_json(string, script_safe = false) # :nodoc:
66
- if script_safe
67
- if SCRIPT_SAFE_ESCAPE_PATTERN.match?(string)
68
- string.gsub(SCRIPT_SAFE_ESCAPE_PATTERN, SCRIPT_SAFE_MAP)
69
- else
70
- string
71
- end
63
+ pattern = script_safe ? SCRIPT_SAFE_ESCAPE_PATTERN : ESCAPE_PATTERN
64
+ if pattern.match?(string)
65
+ string.gsub(pattern, MAP)
72
66
  else
73
- if /["\\\x0-\x1f]/.match?(string)
74
- string.gsub(/["\\\x0-\x1f]/, MAP)
75
- else
76
- string
77
- end
67
+ string
78
68
  end
79
69
  end
80
70
 
81
71
  def self.utf8_to_json_ascii(original_string, script_safe = false) # :nodoc:
82
- string = original_string.b
83
- map = script_safe ? SCRIPT_SAFE_MAP : MAP
84
- string.gsub!(/[\/"\\\x0-\x1f]/n) { map[$&] || $& }
72
+ string = utf8_to_json(original_string, script_safe).b
85
73
  string.gsub!(/(
86
74
  (?:
87
75
  [\xc2-\xdf][\x80-\xbf] |
@@ -146,7 +134,7 @@ module JSON
146
134
  # * *space_before*: a string that is put before a : pair delimiter (default: ''),
147
135
  # * *object_nl*: a string that is put at the end of a JSON object (default: ''),
148
136
  # * *array_nl*: a string that is put at the end of a JSON array (default: ''),
149
- # * *script_safe*: true if U+2028, U+2029 and forward slash (/) should be escaped
137
+ # * *script_safe*: true if forward slash (/) should be escaped
150
138
  # as to make the JSON object safe to interpolate in a script tag (default: false).
151
139
  # * *check_circular*: is deprecated now, use the :max_nesting option instead,
152
140
  # * *max_nesting*: sets the maximum level of data structure nesting in
@@ -207,7 +195,7 @@ module JSON
207
195
  # the generated JSON, max_nesting = 0 if no maximum is checked.
208
196
  attr_accessor :max_nesting
209
197
 
210
- # If this attribute is set to true, forward slashes will be escaped in
198
+ # If this attribute is set to true, script tags and HTML comments will be escaped in
211
199
  # all json strings.
212
200
  attr_accessor :script_safe
213
201
 
@@ -451,7 +439,11 @@ module JSON
451
439
  end
452
440
  obj.each_pair do |k,v|
453
441
  if first
454
- key_type = k.class
442
+ if ::String == k.class || ::Symbol == k.class
443
+ key_type = k.class
444
+ else
445
+ JSON.send(:on_mixed_keys_hash, obj)
446
+ end
455
447
  else
456
448
  if key_type && !@allow_duplicate_key && key_type != k.class
457
449
  key_type = nil # stop checking
@@ -592,7 +584,11 @@ module JSON
592
584
 
593
585
  hash.each { |key, value|
594
586
  if first
595
- key_type = key.class
587
+ if ::String == key.class || ::Symbol == key.class
588
+ key_type = key.class
589
+ else
590
+ JSON.send(:on_mixed_keys_hash, hash)
591
+ end
596
592
  else
597
593
  if key_type && !state.allow_duplicate_key? && key_type != key.class
598
594
  key_type = nil # stop checking
@@ -783,9 +779,18 @@ module JSON
783
779
 
784
780
  if state.strict? && state.as_json
785
781
  unless Generator.valid_encoding?(string)
786
- string = state.as_json.call(string, false)
787
- unless string.is_a?(::String)
788
- return string.to_json(state, *args)
782
+ coerced = state.as_json.call(string, false)
783
+ unless coerced.equal?(string)
784
+ # as_json is only called once, so a new string that is still
785
+ # invalid raises instead of calling it again.
786
+ coerced = coerced.name if coerced.is_a?(::Symbol)
787
+ unless coerced.is_a?(::String)
788
+ return coerced.to_json(state, *args)
789
+ end
790
+ unless Generator.valid_encoding?(coerced)
791
+ raise GeneratorError.new("source sequence is illegal/malformed utf-8", self)
792
+ end
793
+ string = coerced
789
794
  end
790
795
  end
791
796
  end
data/lib/json/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JSON
4
- VERSION = '3.1.0'
4
+ VERSION = '3.2.0'
5
5
  end
data/lib/json.rb CHANGED
@@ -118,7 +118,7 @@ require 'json/common'
118
118
  #
119
119
  # ==== Parsing Options
120
120
  #
121
- # ====== Input Options
121
+ # ===== Input Options
122
122
  #
123
123
  # Option +max_nesting+ (\Integer) specifies the maximum nesting depth allowed;
124
124
  # defaults to +100+;
@@ -214,7 +214,7 @@ require 'json/common'
214
214
  # When enabled:
215
215
  # JSON.parse('"Hell\o"', allow_invalid_escape: true) # => "Hello"
216
216
  #
217
- # ====== Output Options
217
+ # ===== Output Options
218
218
  #
219
219
  # Option +freeze+ (boolean) specifies whether the returned objects will be frozen;
220
220
  # defaults to +false+.
@@ -323,7 +323,7 @@ require 'json/common'
323
323
  #
324
324
  # ==== Generating Options
325
325
  #
326
- # ====== Input Options
326
+ # ===== Input Options
327
327
  #
328
328
  # Option +allow_nan+ (boolean) specifies whether
329
329
  # +NaN+, +Infinity+, and +-Infinity+ may be generated;
@@ -378,7 +378,7 @@ require 'json/common'
378
378
  # which may leave the process in an unrecoverable state.
379
379
  # It is highly discouraged.
380
380
  #
381
- # ====== Escaping Options
381
+ # ===== Escaping Options
382
382
  #
383
383
  # Options +script_safe+ (boolean) specifies wether <tt>'\u2028'</tt>, <tt>'\u2029'</tt>
384
384
  # and <tt>'/'</tt> should be escaped as to make the JSON object safe to interpolate in script
@@ -387,7 +387,7 @@ require 'json/common'
387
387
  # Options +ascii_only+ (boolean) specifies wether all characters outside the ASCII range
388
388
  # should be escaped.
389
389
  #
390
- # ====== Output Options
390
+ # ===== Output Options
391
391
  #
392
392
  # The default formatting options generate the most compact
393
393
  # \JSON data, all on one line and with no whitespace.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Florian Frank