json 2.11.3 → 2.21.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGES.md +194 -8
- data/LEGAL +12 -0
- data/README.md +44 -4
- data/ext/json/ext/fbuffer/fbuffer.h +99 -76
- data/ext/json/ext/generator/extconf.rb +9 -0
- data/ext/json/ext/generator/generator.c +788 -563
- data/ext/json/ext/json.h +183 -0
- data/ext/json/ext/parser/extconf.rb +34 -2
- data/ext/json/ext/parser/parser.c +2254 -695
- data/ext/json/ext/simd/conf.rb +24 -0
- data/ext/json/ext/simd/simd.h +208 -0
- data/ext/json/ext/vendor/fast_float_parser.h +814 -0
- data/ext/json/ext/vendor/fpconv.c +20 -19
- data/json.gemspec +2 -3
- data/lib/json/add/core.rb +1 -0
- data/lib/json/add/string.rb +35 -0
- data/lib/json/common.rb +135 -55
- data/lib/json/ext/generator/state.rb +12 -14
- data/lib/json/ext.rb +28 -2
- data/lib/json/generic_object.rb +0 -8
- data/lib/json/truffle_ruby/generator.rb +172 -72
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +122 -5
- metadata +8 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ea6afdf75f49e474e408c24daae4856891310979fbb10aa34ead66ad2fbf3321
|
|
4
|
+
data.tar.gz: 19eb129df23cde0ac607fa6ddf909d6ea5eaef0035b2180eea4c2c8a7a5c5ff0
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c2212cdac41569ebd41e83e6d1ea3324e944f6a8db0070a2805f2213da7341a05cc8eab4dc6d77852f30d31a8951f36d435fdb0aff647776eb8f42cee25113ef
|
|
7
|
+
data.tar.gz: b16fdb8a1f5384aa6e9064c7884c0f7ec96632bc2c1c6324cc2e902459e9ac94559ec691cf45dea30fc374fab74ce4e3a82f8bb9d0ec22e0ddd5357764076942
|
data/CHANGES.md
CHANGED
|
@@ -1,5 +1,190 @@
|
|
|
1
1
|
# Changes
|
|
2
2
|
|
|
3
|
+
### Unreleased
|
|
4
|
+
|
|
5
|
+
### 2026-07-31 (2.21.2)
|
|
6
|
+
|
|
7
|
+
* Fix a use-after-free bug in `JSON::ResumableParser`. [GHSA-9hj4-r449-hfvc].
|
|
8
|
+
|
|
9
|
+
### 2026-07-13 (2.21.1)
|
|
10
|
+
|
|
11
|
+
* Fix a compilation issue on Window and Microsoft Visual C++.
|
|
12
|
+
|
|
13
|
+
### 2026-07-12 (2.21.0)
|
|
14
|
+
|
|
15
|
+
* `JSON.generate` now accept a `sort_keys` option, which takes either a boolean or a block.
|
|
16
|
+
* Added `#empty?` and `#partial_value?` methods on `JSON::ResumableParser`.
|
|
17
|
+
* Numerous correctness and performance fixes for `JSON::ResumableParser`.
|
|
18
|
+
* Avoid triggering Ruby's `float out of range` warning when parsing out of range numbers.
|
|
19
|
+
* Declare C types with Ruby 4.1 `RUBY_TYPED_THREAD_SAFE_FREE`.
|
|
20
|
+
|
|
21
|
+
### 2026-06-23 (2.20.0)
|
|
22
|
+
|
|
23
|
+
* Both C and Java parsers are no longer recursive, so parsing very deep documents with `max_nesting: false` will no longer
|
|
24
|
+
result in `SystemStackError stack level too deep` errors.
|
|
25
|
+
* The `:max_nesting` option still defaults to `100`.
|
|
26
|
+
* Optimized floating point number parsing further by replacing the ryu algorithm by a port of Eisel-Lemire Fast Float.
|
|
27
|
+
* Added `JSON::ResumableParser` to parse streams of JSON documents. Not yet available on JRuby.
|
|
28
|
+
* Deprecate default support of JavaScript comments in the parser and add `allow_comments: true` parsing option.
|
|
29
|
+
* Integrate with Ruby 4.1 `ruby_sized_xfree`.
|
|
30
|
+
|
|
31
|
+
### 2026-06-11 (2.19.9)
|
|
32
|
+
|
|
33
|
+
* Fix buffer overflow that could lead to a crash when writing JSON directly into an IO
|
|
34
|
+
with `JSON.generate(object, io)`. [CVE-2026-54696].
|
|
35
|
+
|
|
36
|
+
### 2026-06-03 (2.19.8)
|
|
37
|
+
|
|
38
|
+
* Fix 1-byte buffer overread on EOS errors.
|
|
39
|
+
* Handle invalid types passed as `max_nesting` option.
|
|
40
|
+
|
|
41
|
+
### 2026-05-28 (2.19.7)
|
|
42
|
+
|
|
43
|
+
* Fix some more edge cases with out of range floats.
|
|
44
|
+
* Ensure the string provided to `JSON.parse` can't be mutated during parsing.
|
|
45
|
+
* Add missing write barriers in `State#dup`.
|
|
46
|
+
* Further validate generator `depth` config.
|
|
47
|
+
|
|
48
|
+
### 2026-05-28 (2.19.6)
|
|
49
|
+
|
|
50
|
+
* Cleanly handle overly large `depth` generator argument.
|
|
51
|
+
* Add missing write barrier in `ParserConfig`.
|
|
52
|
+
|
|
53
|
+
### 2026-05-04 (2.19.5)
|
|
54
|
+
|
|
55
|
+
* Cap the parser to emit a maximum of 5 deprecation warnings per document. Emitting more is not helpful.
|
|
56
|
+
|
|
57
|
+
### 2026-04-19 (2.19.4)
|
|
58
|
+
|
|
59
|
+
* Fix parsing of out of range floats (very large exponents that lead to either `0.0` or `Inf`).
|
|
60
|
+
|
|
61
|
+
### 2026-03-25 (2.19.3)
|
|
62
|
+
|
|
63
|
+
* Fix handling of unescaped control characters preceeded by a backslash.
|
|
64
|
+
|
|
65
|
+
### 2026-03-18 (2.19.2)
|
|
66
|
+
|
|
67
|
+
* Fix a format string injection vulnerability in `JSON.parse(doc, allow_duplicate_key: false)`. `CVE-2026-33210`.
|
|
68
|
+
|
|
69
|
+
### 2026-03-08 (2.19.1)
|
|
70
|
+
|
|
71
|
+
* Fix a compiler dependent GC bug introduced in `2.18.0`.
|
|
72
|
+
|
|
73
|
+
### 2026-03-06 (2.19.0)
|
|
74
|
+
|
|
75
|
+
* Fix `allow_blank` parsing option to no longer allow invalid types (e.g. `load([], allow_blank: true)` now raise a type error).
|
|
76
|
+
* Add `allow_invalid_escape` parsing option to ignore backslashes that aren't followed by one of the valid escape characters.
|
|
77
|
+
|
|
78
|
+
### 2026-02-03 (2.18.1)
|
|
79
|
+
|
|
80
|
+
* Fix a potential crash in very specific circumstance if GC triggers during a call to `to_json`
|
|
81
|
+
without first invoking a user defined `#to_json` method.
|
|
82
|
+
|
|
83
|
+
### 2025-12-11 (2.18.0)
|
|
84
|
+
|
|
85
|
+
* Add `:allow_control_characters` parser options, to allow JSON strings containing unescaped ASCII control characters (e.g. newlines).
|
|
86
|
+
|
|
87
|
+
### 2026-03-18 (2.17.1.2) - Security Backport
|
|
88
|
+
|
|
89
|
+
* Fix a format string injection vulnerability in `JSON.parse(doc, allow_duplicate_key: false)`. `CVE-2026-33210`.
|
|
90
|
+
|
|
91
|
+
### 2025-12-04 (2.17.1)
|
|
92
|
+
|
|
93
|
+
* Fix a regression in parsing of unicode surogate pairs (`\uXX\uXX`) that could cause an invalid string to be returned.
|
|
94
|
+
|
|
95
|
+
### 2025-12-03 (2.17.0)
|
|
96
|
+
|
|
97
|
+
* Improve `JSON.load` and `JSON.unsafe_load` to allow passing options as second argument.
|
|
98
|
+
* Fix the parser to no longer ignore invalid escapes in strings.
|
|
99
|
+
Only `\"`, `\\`, `\b`, `\f`, `\n`, `\r`, `\t` and `\u` are valid JSON escapes.
|
|
100
|
+
* Fixed `JSON::Coder` to use the depth it was initialized with.
|
|
101
|
+
* On TruffleRuby, fix the generator to not call `to_json` on the return value of `as_json` for `Float::NAN`.
|
|
102
|
+
* Fixed handling of `state.depth`: when `to_json` changes `state.depth` but does not restore it, it is reset
|
|
103
|
+
automatically to its initial value.
|
|
104
|
+
In particular, when a `NestingError` is raised, `depth` is no longer equal to `max_nesting` after the call to
|
|
105
|
+
generate, and is reset to its initial value. Similarly when `to_json` raises an exception.
|
|
106
|
+
|
|
107
|
+
### 2025-11-07 (2.16.0)
|
|
108
|
+
|
|
109
|
+
* Deprecate `JSON::State#[]` and `JSON::State#[]=`. Consider using `JSON::Coder` instead.
|
|
110
|
+
* `JSON::Coder` now also yields to the block when encountering strings with invalid encoding.
|
|
111
|
+
* Fix GeneratorError messages to be UTF-8 encoded.
|
|
112
|
+
* Fix memory leak when `Exception` is raised, or `throw` is used during JSON generation.
|
|
113
|
+
* Optimized floating point number parsing by integrating the ryu algorithm (thanks to Josef Šimánek).
|
|
114
|
+
* Optimized numbers parsing using SWAR (thanks to Scott Myron).
|
|
115
|
+
* Optimized parsing of pretty printed documents using SWAR (thanks to Scott Myron).
|
|
116
|
+
|
|
117
|
+
### 2026-03-18 (2.15.2.1) - Security Backport
|
|
118
|
+
|
|
119
|
+
* Fix a format string injection vulnerability in `JSON.parse(doc, allow_duplicate_key: false)`. `CVE-2026-33210`.
|
|
120
|
+
|
|
121
|
+
### 2025-10-25 (2.15.2)
|
|
122
|
+
|
|
123
|
+
* Fix `JSON::Coder` to have one dedicated depth counter per invocation.
|
|
124
|
+
After encountering a circular reference in `JSON::Coder#dump`, any further `#dump` call would raise `JSON::NestingError`.
|
|
125
|
+
|
|
126
|
+
### 2025-10-07 (2.15.1)
|
|
127
|
+
|
|
128
|
+
* Fix incorrect escaping in the JRuby extension when encoding shared strings.
|
|
129
|
+
|
|
130
|
+
### 2025-09-22 (2.15.0)
|
|
131
|
+
|
|
132
|
+
* `JSON::Coder` callback now receive a second argument to convey whether the object is a hash key.
|
|
133
|
+
* Tuned the floating point number generator to not use scientific notation as aggressively.
|
|
134
|
+
|
|
135
|
+
### 2025-09-18 (2.14.1)
|
|
136
|
+
|
|
137
|
+
* Fix `IndexOutOfBoundsException` in the JRuby extension when encoding shared strings.
|
|
138
|
+
|
|
139
|
+
### 2025-09-18 (2.14.0)
|
|
140
|
+
|
|
141
|
+
* Add new `allow_duplicate_key` generator options. By default a warning is now emitted when a duplicated key is encountered.
|
|
142
|
+
In `json 3.0` an error will be raised.
|
|
143
|
+
```ruby
|
|
144
|
+
>> Warning[:deprecated] = true
|
|
145
|
+
>> puts JSON.generate({ foo: 1, "foo" => 2 })
|
|
146
|
+
(irb):2: warning: detected duplicate key "foo" in {foo: 1, "foo" => 2}.
|
|
147
|
+
This will raise an error in json 3.0 unless enabled via `allow_duplicate_key: true`
|
|
148
|
+
{"foo":1,"foo":2}
|
|
149
|
+
>> JSON.generate({ foo: 1, "foo" => 2 }, allow_duplicate_key: false)
|
|
150
|
+
detected duplicate key "foo" in {foo: 1, "foo" => 2} (JSON::GeneratorError)
|
|
151
|
+
```
|
|
152
|
+
* Fix `JSON.generate` `strict: true` mode to also restrict hash keys.
|
|
153
|
+
* Fix `JSON::Coder` to also invoke block for hash keys that aren't strings nor symbols.
|
|
154
|
+
* Fix `JSON.unsafe_load` usage with proc
|
|
155
|
+
* Fix the parser to more consistently reject invalid UTF-16 surogate pairs.
|
|
156
|
+
* Stop defining `String.json_create`, `String#to_json_raw`, `String#to_json_raw_object` when `json/add` isn't loaded.
|
|
157
|
+
|
|
158
|
+
### 2025-07-28 (2.13.2)
|
|
159
|
+
|
|
160
|
+
* Improve duplicate key warning and errors to include the key name and point to the right caller.
|
|
161
|
+
|
|
162
|
+
### 2025-07-24 (2.13.1)
|
|
163
|
+
|
|
164
|
+
* Fix support for older compilers without `__builtin_cpu_supports`.
|
|
165
|
+
|
|
166
|
+
### 2025-07-17 (2.13.0)
|
|
167
|
+
|
|
168
|
+
* Add new `allow_duplicate_key` parsing options. By default a warning is now emitted when a duplicated key is encountered.
|
|
169
|
+
In `json 3.0` an error will be raised.
|
|
170
|
+
* Optimize parsing further using SIMD to scan strings.
|
|
171
|
+
|
|
172
|
+
### 2025-05-23 (2.12.2)
|
|
173
|
+
|
|
174
|
+
* Fix compiler optimization level.
|
|
175
|
+
|
|
176
|
+
### 2025-05-23 (2.12.1)
|
|
177
|
+
|
|
178
|
+
* Fix a potential crash in large negative floating point number generation.
|
|
179
|
+
* Fix for JSON.pretty_generate to use passed state object's generate instead of state class as the required parameters aren't available.
|
|
180
|
+
|
|
181
|
+
### 2025-05-12 (2.12.0)
|
|
182
|
+
|
|
183
|
+
* Improve floating point generation to not use scientific notation as much.
|
|
184
|
+
* Include line and column in parser errors. Both in the message and as exception attributes.
|
|
185
|
+
* Handle non-string hash keys with broken `to_s` implementations.
|
|
186
|
+
* `JSON.generate` now uses SSE2 (x86) or NEON (arm64) instructions when available to escape strings.
|
|
187
|
+
|
|
3
188
|
### 2025-04-25 (2.11.3)
|
|
4
189
|
|
|
5
190
|
* Fix a regression in `JSON.pretty_generate` that could cause indentation to be off once some `#to_json` has been called.
|
|
@@ -12,7 +197,7 @@
|
|
|
12
197
|
### 2025-04-24 (2.11.1)
|
|
13
198
|
|
|
14
199
|
* Add back `JSON.restore`, `JSON.unparse`, `JSON.fast_unparse` and `JSON.pretty_unparse`.
|
|
15
|
-
These were deprecated 16 years ago, but never
|
|
200
|
+
These were deprecated 16 years ago, but never emitted warnings, only undocumented, so are
|
|
16
201
|
still used by a few gems.
|
|
17
202
|
|
|
18
203
|
### 2025-04-24 (2.11.0)
|
|
@@ -39,7 +224,7 @@
|
|
|
39
224
|
### 2025-03-12 (2.10.2)
|
|
40
225
|
|
|
41
226
|
* Fix a potential crash in the C extension parser.
|
|
42
|
-
* Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0`
|
|
227
|
+
* Raise a ParserError on all incomplete unicode escape sequence. This was the behavior until `2.10.0` inadvertently changed it.
|
|
43
228
|
* Ensure document snippets that are included in parser errors don't include truncated multibyte characters.
|
|
44
229
|
* Ensure parser error snippets are valid UTF-8.
|
|
45
230
|
* Fix `JSON::GeneratorError#detailed_message` on Ruby < 3.2
|
|
@@ -70,7 +255,7 @@
|
|
|
70
255
|
|
|
71
256
|
### 2024-11-14 (2.8.2)
|
|
72
257
|
|
|
73
|
-
* `JSON.load_file`
|
|
258
|
+
* `JSON.load_file` explicitly read the file as UTF-8.
|
|
74
259
|
|
|
75
260
|
### 2024-11-06 (2.8.1)
|
|
76
261
|
|
|
@@ -78,7 +263,7 @@
|
|
|
78
263
|
|
|
79
264
|
### 2024-11-06 (2.8.0)
|
|
80
265
|
|
|
81
|
-
* Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being
|
|
266
|
+
* Emit a deprecation warning when `JSON.load` create custom types without the `create_additions` option being explicitly enabled.
|
|
82
267
|
* Prefer to use `JSON.unsafe_load(string)` or `JSON.load(string, create_additions: true)`.
|
|
83
268
|
* Emit a deprecation warning when serializing valid UTF-8 strings encoded in `ASCII_8BIT` aka `BINARY`.
|
|
84
269
|
* Bump required Ruby version to 2.7.
|
|
@@ -86,7 +271,7 @@
|
|
|
86
271
|
pre-existing support for comments, make it suitable to parse `jsonc` documents.
|
|
87
272
|
* Many performance improvements to `JSON.parse` and `JSON.load`, up to `1.7x` faster on real world documents.
|
|
88
273
|
* Some minor performance improvements to `JSON.dump` and `JSON.generate`.
|
|
89
|
-
* `JSON.pretty_generate` no longer
|
|
274
|
+
* `JSON.pretty_generate` no longer includes newlines inside empty object and arrays.
|
|
90
275
|
|
|
91
276
|
### 2024-11-04 (2.7.6)
|
|
92
277
|
|
|
@@ -103,13 +288,13 @@
|
|
|
103
288
|
* Workaround a bug in 3.4.8 and older https://github.com/rubygems/rubygems/pull/6490.
|
|
104
289
|
This bug would cause some gems with native extension to fail during compilation.
|
|
105
290
|
* Workaround different versions of `json` and `json_pure` being loaded (not officially supported).
|
|
106
|
-
* Make `json_pure` Ractor compatible.
|
|
291
|
+
* Make `json_pure` Ractor compatible.
|
|
107
292
|
|
|
108
293
|
### 2024-10-24 (2.7.3)
|
|
109
294
|
|
|
110
295
|
* Numerous performance optimizations in `JSON.generate` and `JSON.dump` (up to 2 times faster).
|
|
111
|
-
* Limit the size of ParserError exception messages, only include up to 32 bytes of the
|
|
112
|
-
* Fix json-pure's `Object#to_json` to accept non
|
|
296
|
+
* Limit the size of ParserError exception messages, only include up to 32 bytes of the unparsable source.
|
|
297
|
+
* Fix json-pure's `Object#to_json` to accept non-state arguments.
|
|
113
298
|
* Fix multiline comment support in `json-pure`.
|
|
114
299
|
* Fix `JSON.parse` to no longer mutate the argument encoding when passed an ASCII-8BIT string.
|
|
115
300
|
* Fix `String#to_json` to raise on invalid encoding in `json-pure`.
|
|
@@ -254,6 +439,7 @@
|
|
|
254
439
|
## 2015-09-11 (2.0.0)
|
|
255
440
|
* Now complies to newest JSON RFC 7159.
|
|
256
441
|
* Implements compatibility to ruby 2.4 integer unification.
|
|
442
|
+
* Removed support for `quirks_mode` option.
|
|
257
443
|
* Drops support for old rubies whose life has ended, that is rubies < 2.0.
|
|
258
444
|
Also see https://www.ruby-lang.org/en/news/2014/07/01/eol-for-1-8-7-and-1-9-2/
|
|
259
445
|
* There were still some mentions of dual GPL licensing in the source, but JSON
|
data/LEGAL
CHANGED
|
@@ -6,3 +6,15 @@
|
|
|
6
6
|
All the files in this distribution are covered under either the Ruby's
|
|
7
7
|
license (see the file COPYING) or public-domain except some files
|
|
8
8
|
mentioned below.
|
|
9
|
+
|
|
10
|
+
ext/json/ext/vendor/fpconv.h::
|
|
11
|
+
This file is adapted from https://github.com/night-shift/fpconv
|
|
12
|
+
It is licensed under Boost Software License 1.0.
|
|
13
|
+
|
|
14
|
+
ext/json/ext/vendor/jeaiii-ltoa.h::
|
|
15
|
+
This file is adapted from https://github.com/jeaiii/itoa
|
|
16
|
+
It is licensed under the MIT License
|
|
17
|
+
|
|
18
|
+
ext/json/ext/vendor/fast_float_parser.h::
|
|
19
|
+
This file is adapted from the Fast Float C++ library by The fast_float authors https://github.com/fastfloat/fast_float
|
|
20
|
+
It is licensed under the MIT License
|
data/README.md
CHANGED
|
@@ -85,7 +85,7 @@ Both of these behavior can be disabled using the `strict: true` option:
|
|
|
85
85
|
|
|
86
86
|
```ruby
|
|
87
87
|
JSON.generate(Object.new, strict: true) # => Object not allowed in JSON (JSON::GeneratorError)
|
|
88
|
-
JSON.generate(Position.new(1, 2)) # => Position not allowed in JSON (JSON::GeneratorError)
|
|
88
|
+
JSON.generate(Position.new(1, 2), strict: true) # => Position not allowed in JSON (JSON::GeneratorError)
|
|
89
89
|
```
|
|
90
90
|
|
|
91
91
|
## JSON::Coder
|
|
@@ -97,7 +97,7 @@ Instead it is recommended to use the newer `JSON::Coder` API:
|
|
|
97
97
|
|
|
98
98
|
```ruby
|
|
99
99
|
module MyApp
|
|
100
|
-
API_JSON_CODER = JSON::Coder.new do |object|
|
|
100
|
+
API_JSON_CODER = JSON::Coder.new do |object, is_object_key|
|
|
101
101
|
case object
|
|
102
102
|
when Time
|
|
103
103
|
object.iso8601(3)
|
|
@@ -113,6 +113,24 @@ puts MyApp::API_JSON_CODER.dump(Time.now.utc) # => "2025-01-21T08:41:44.286Z"
|
|
|
113
113
|
The provided block is called for all objects that don't have a native JSON equivalent, and
|
|
114
114
|
must return a Ruby object that has a native JSON equivalent.
|
|
115
115
|
|
|
116
|
+
It is also called for objects that do have a JSON equivalent, but are used as Hash keys, for instance `{ 1 => 2}`,
|
|
117
|
+
as well as for strings that aren't valid UTF-8:
|
|
118
|
+
|
|
119
|
+
```ruby
|
|
120
|
+
coder = JSON::Coder.new do |object, is_object_key|
|
|
121
|
+
case object
|
|
122
|
+
when String
|
|
123
|
+
if !object.valid_encoding? || object.encoding != Encoding::UTF_8
|
|
124
|
+
Base64.encode64(object)
|
|
125
|
+
else
|
|
126
|
+
object
|
|
127
|
+
end
|
|
128
|
+
else
|
|
129
|
+
object
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
```
|
|
133
|
+
|
|
116
134
|
## Combining JSON fragments
|
|
117
135
|
|
|
118
136
|
To combine JSON fragments into a bigger JSON document, you can use `JSON::Fragment`:
|
|
@@ -231,8 +249,32 @@ There are also the methods `Kernel#j` for generate, and `Kernel#jj` for
|
|
|
231
249
|
`pretty_generate` output to the console, that work analogous to Core Ruby's `p` and
|
|
232
250
|
the `pp` library's `pp` methods.
|
|
233
251
|
|
|
252
|
+
## Security
|
|
253
|
+
|
|
254
|
+
When parsing or serializing untrusted input, parser and generator options should never be user controlled.
|
|
255
|
+
|
|
256
|
+
```ruby
|
|
257
|
+
# Dangerous, DO NOT DO THIS.
|
|
258
|
+
JSON.generate(params[:data], params[:options])
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Security vulnerability reports relying on attacker controlled parsing or generator options will be handled as regular bug fixes.
|
|
262
|
+
|
|
234
263
|
## Development
|
|
235
264
|
|
|
265
|
+
### Prerequisites
|
|
266
|
+
|
|
267
|
+
1. Clone the repository
|
|
268
|
+
2. Install dependencies with `bundle install`
|
|
269
|
+
|
|
270
|
+
### Testing
|
|
271
|
+
|
|
272
|
+
The full test suite can be run with:
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
bundle exec rake test
|
|
276
|
+
```
|
|
277
|
+
|
|
236
278
|
### Release
|
|
237
279
|
|
|
238
280
|
Update the `lib/json/version.rb` file.
|
|
@@ -264,5 +306,3 @@ The latest version of this library can be downloaded at
|
|
|
264
306
|
Online Documentation should be located at
|
|
265
307
|
|
|
266
308
|
* https://www.rubydoc.info/gems/json
|
|
267
|
-
|
|
268
|
-
[Ragel]: http://www.colm.net/open-source/ragel/
|
|
@@ -1,41 +1,9 @@
|
|
|
1
1
|
#ifndef _FBUFFER_H_
|
|
2
2
|
#define _FBUFFER_H_
|
|
3
3
|
|
|
4
|
-
#include "
|
|
5
|
-
#include "ruby/encoding.h"
|
|
4
|
+
#include "../json.h"
|
|
6
5
|
#include "../vendor/jeaiii-ltoa.h"
|
|
7
6
|
|
|
8
|
-
/* shims */
|
|
9
|
-
/* This is the fallback definition from Ruby 3.4 */
|
|
10
|
-
|
|
11
|
-
#ifndef RBIMPL_STDBOOL_H
|
|
12
|
-
#if defined(__cplusplus)
|
|
13
|
-
# if defined(HAVE_STDBOOL_H) && (__cplusplus >= 201103L)
|
|
14
|
-
# include <cstdbool>
|
|
15
|
-
# endif
|
|
16
|
-
#elif defined(HAVE_STDBOOL_H)
|
|
17
|
-
# include <stdbool.h>
|
|
18
|
-
#elif !defined(HAVE__BOOL)
|
|
19
|
-
typedef unsigned char _Bool;
|
|
20
|
-
# define bool _Bool
|
|
21
|
-
# define true ((_Bool)+1)
|
|
22
|
-
# define false ((_Bool)+0)
|
|
23
|
-
# define __bool_true_false_are_defined
|
|
24
|
-
#endif
|
|
25
|
-
#endif
|
|
26
|
-
|
|
27
|
-
#ifndef RB_UNLIKELY
|
|
28
|
-
#define RB_UNLIKELY(expr) expr
|
|
29
|
-
#endif
|
|
30
|
-
|
|
31
|
-
#ifndef RB_LIKELY
|
|
32
|
-
#define RB_LIKELY(expr) expr
|
|
33
|
-
#endif
|
|
34
|
-
|
|
35
|
-
#ifndef MAYBE_UNUSED
|
|
36
|
-
# define MAYBE_UNUSED(x) x
|
|
37
|
-
#endif
|
|
38
|
-
|
|
39
7
|
enum fbuffer_type {
|
|
40
8
|
FBUFFER_HEAP_ALLOCATED = 0,
|
|
41
9
|
FBUFFER_STACK_ALLOCATED = 1,
|
|
@@ -43,9 +11,12 @@ enum fbuffer_type {
|
|
|
43
11
|
|
|
44
12
|
typedef struct FBufferStruct {
|
|
45
13
|
enum fbuffer_type type;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
14
|
+
size_t initial_length;
|
|
15
|
+
size_t len;
|
|
16
|
+
size_t capa;
|
|
17
|
+
#if JSON_DEBUG
|
|
18
|
+
size_t requested;
|
|
19
|
+
#endif
|
|
49
20
|
char *ptr;
|
|
50
21
|
VALUE io;
|
|
51
22
|
} FBuffer;
|
|
@@ -61,25 +32,43 @@ typedef struct FBufferStruct {
|
|
|
61
32
|
|
|
62
33
|
static void fbuffer_free(FBuffer *fb);
|
|
63
34
|
static void fbuffer_clear(FBuffer *fb);
|
|
64
|
-
static void fbuffer_append(FBuffer *fb, const char *newstr,
|
|
35
|
+
static void fbuffer_append(FBuffer *fb, const char *newstr, size_t len);
|
|
65
36
|
static void fbuffer_append_long(FBuffer *fb, long number);
|
|
66
37
|
static inline void fbuffer_append_char(FBuffer *fb, char newchr);
|
|
67
38
|
static VALUE fbuffer_finalize(FBuffer *fb);
|
|
68
39
|
|
|
69
|
-
static void
|
|
40
|
+
static void fbuffer_init(FBuffer *fb, size_t initial_length, VALUE io, char *stack_buffer, size_t stack_buffer_size)
|
|
70
41
|
{
|
|
71
|
-
|
|
72
|
-
|
|
42
|
+
if (RTEST(io)) {
|
|
43
|
+
JSON_ASSERT(fb->type == FBUFFER_HEAP_ALLOCATED);
|
|
44
|
+
fb->io = io;
|
|
45
|
+
fb->initial_length = (initial_length > 0) ? initial_length : FBUFFER_IO_BUFFER_SIZE;
|
|
46
|
+
} else {
|
|
73
47
|
fb->type = FBUFFER_STACK_ALLOCATED;
|
|
74
48
|
fb->ptr = stack_buffer;
|
|
75
49
|
fb->capa = stack_buffer_size;
|
|
50
|
+
fb->initial_length = (initial_length > 0) ? initial_length : FBUFFER_INITIAL_LENGTH_DEFAULT;
|
|
76
51
|
}
|
|
52
|
+
#if JSON_DEBUG
|
|
53
|
+
fb->requested = 0;
|
|
54
|
+
#endif
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
static inline void fbuffer_consumed(FBuffer *fb, size_t consumed)
|
|
58
|
+
{
|
|
59
|
+
#if JSON_DEBUG
|
|
60
|
+
if (consumed > fb->requested) {
|
|
61
|
+
rb_bug("fbuffer: Out of bound write");
|
|
62
|
+
}
|
|
63
|
+
fb->requested = 0;
|
|
64
|
+
#endif
|
|
65
|
+
fb->len += consumed;
|
|
77
66
|
}
|
|
78
67
|
|
|
79
68
|
static void fbuffer_free(FBuffer *fb)
|
|
80
69
|
{
|
|
81
70
|
if (fb->ptr && fb->type == FBUFFER_HEAP_ALLOCATED) {
|
|
82
|
-
|
|
71
|
+
JSON_SIZED_FREE_N(fb->ptr, fb->capa);
|
|
83
72
|
}
|
|
84
73
|
}
|
|
85
74
|
|
|
@@ -94,85 +83,122 @@ static void fbuffer_flush(FBuffer *fb)
|
|
|
94
83
|
fbuffer_clear(fb);
|
|
95
84
|
}
|
|
96
85
|
|
|
97
|
-
static void fbuffer_realloc(FBuffer *fb,
|
|
86
|
+
static void fbuffer_realloc(FBuffer *fb, size_t new_capa)
|
|
98
87
|
{
|
|
99
|
-
if (
|
|
88
|
+
if (new_capa > fb->capa) {
|
|
100
89
|
if (fb->type == FBUFFER_STACK_ALLOCATED) {
|
|
101
90
|
const char *old_buffer = fb->ptr;
|
|
102
|
-
fb->ptr = ALLOC_N(char,
|
|
91
|
+
fb->ptr = ALLOC_N(char, new_capa);
|
|
103
92
|
fb->type = FBUFFER_HEAP_ALLOCATED;
|
|
104
93
|
MEMCPY(fb->ptr, old_buffer, char, fb->len);
|
|
105
94
|
} else {
|
|
106
|
-
|
|
95
|
+
JSON_SIZED_REALLOC_N(fb->ptr, char, new_capa, fb->capa);
|
|
107
96
|
}
|
|
108
|
-
fb->capa =
|
|
97
|
+
fb->capa = new_capa;
|
|
109
98
|
}
|
|
110
99
|
}
|
|
111
100
|
|
|
112
|
-
static void fbuffer_do_inc_capa(FBuffer *fb,
|
|
101
|
+
static void fbuffer_do_inc_capa(FBuffer *fb, size_t requested)
|
|
113
102
|
{
|
|
114
103
|
if (RB_UNLIKELY(fb->io)) {
|
|
115
|
-
if (fb->capa
|
|
116
|
-
fbuffer_realloc(fb, FBUFFER_IO_BUFFER_SIZE);
|
|
117
|
-
} else {
|
|
104
|
+
if (fb->capa != 0) {
|
|
118
105
|
fbuffer_flush(fb);
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return;
|
|
106
|
+
if (RB_LIKELY(requested < fb->capa)) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
123
109
|
}
|
|
124
110
|
}
|
|
125
111
|
|
|
126
|
-
|
|
112
|
+
size_t new_capa = fb->capa ? fb->capa : fb->initial_length;
|
|
113
|
+
size_t needed_capa = requested + fb->len;
|
|
127
114
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
fb->capa = fb->initial_length;
|
|
115
|
+
while (new_capa < needed_capa) {
|
|
116
|
+
new_capa *= 2;
|
|
131
117
|
}
|
|
132
118
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
fbuffer_realloc(fb, required);
|
|
119
|
+
fbuffer_realloc(fb, new_capa);
|
|
136
120
|
}
|
|
137
121
|
|
|
138
|
-
static inline void fbuffer_inc_capa(FBuffer *fb,
|
|
122
|
+
static inline void fbuffer_inc_capa(FBuffer *fb, size_t requested)
|
|
139
123
|
{
|
|
124
|
+
#if JSON_DEBUG
|
|
125
|
+
fb->requested = requested;
|
|
126
|
+
#endif
|
|
127
|
+
|
|
140
128
|
if (RB_UNLIKELY(requested > fb->capa - fb->len)) {
|
|
141
129
|
fbuffer_do_inc_capa(fb, requested);
|
|
142
130
|
}
|
|
143
131
|
}
|
|
144
132
|
|
|
145
|
-
static
|
|
133
|
+
static inline size_t fbuffer_size_mul_or_raise(size_t a, size_t b)
|
|
134
|
+
{
|
|
135
|
+
size_t result = a * b;
|
|
136
|
+
if (RB_UNLIKELY(a != 0 && (result / a) != b)) {
|
|
137
|
+
rb_raise(rb_eArgError, "Buffer overflow, the resulting document is too large to be generated");
|
|
138
|
+
}
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
static inline void fbuffer_append_reserved(FBuffer *fb, const char *newstr, size_t len)
|
|
143
|
+
{
|
|
144
|
+
MEMCPY(fb->ptr + fb->len, newstr, char, len);
|
|
145
|
+
fbuffer_consumed(fb, len);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
static inline void fbuffer_append(FBuffer *fb, const char *newstr, size_t len)
|
|
146
149
|
{
|
|
147
150
|
if (len > 0) {
|
|
148
151
|
fbuffer_inc_capa(fb, len);
|
|
149
|
-
|
|
150
|
-
fb->len += len;
|
|
152
|
+
fbuffer_append_reserved(fb, newstr, len);
|
|
151
153
|
}
|
|
152
154
|
}
|
|
153
155
|
|
|
154
156
|
/* Appends a character into a buffer. The buffer needs to have sufficient capacity, via fbuffer_inc_capa(...). */
|
|
155
157
|
static inline void fbuffer_append_reserved_char(FBuffer *fb, char chr)
|
|
156
158
|
{
|
|
159
|
+
#if JSON_DEBUG
|
|
160
|
+
if (fb->requested < 1) {
|
|
161
|
+
rb_bug("fbuffer: unreserved write");
|
|
162
|
+
}
|
|
163
|
+
fb->requested--;
|
|
164
|
+
#endif
|
|
165
|
+
|
|
157
166
|
fb->ptr[fb->len] = chr;
|
|
158
|
-
fb->len
|
|
167
|
+
fb->len++;
|
|
159
168
|
}
|
|
160
169
|
|
|
161
170
|
static void fbuffer_append_str(FBuffer *fb, VALUE str)
|
|
162
171
|
{
|
|
163
|
-
const char *
|
|
164
|
-
|
|
172
|
+
const char *ptr;
|
|
173
|
+
size_t len;
|
|
174
|
+
RSTRING_GETMEM(str, ptr, len);
|
|
165
175
|
|
|
176
|
+
fbuffer_append(fb, ptr, len);
|
|
166
177
|
RB_GC_GUARD(str);
|
|
178
|
+
}
|
|
167
179
|
|
|
168
|
-
|
|
180
|
+
static void fbuffer_append_str_repeat(FBuffer *fb, VALUE str, size_t repeat)
|
|
181
|
+
{
|
|
182
|
+
const char *ptr;
|
|
183
|
+
size_t len;
|
|
184
|
+
RSTRING_GETMEM(str, ptr, len);
|
|
185
|
+
|
|
186
|
+
fbuffer_inc_capa(fb, fbuffer_size_mul_or_raise(repeat, len));
|
|
187
|
+
while (repeat) {
|
|
188
|
+
#if JSON_DEBUG
|
|
189
|
+
fb->requested = len;
|
|
190
|
+
#endif
|
|
191
|
+
fbuffer_append_reserved(fb, ptr, len);
|
|
192
|
+
repeat--;
|
|
193
|
+
}
|
|
194
|
+
RB_GC_GUARD(str);
|
|
169
195
|
}
|
|
170
196
|
|
|
171
197
|
static inline void fbuffer_append_char(FBuffer *fb, char newchr)
|
|
172
198
|
{
|
|
173
199
|
fbuffer_inc_capa(fb, 1);
|
|
174
200
|
*(fb->ptr + fb->len) = newchr;
|
|
175
|
-
fb
|
|
201
|
+
fbuffer_consumed(fb, 1);
|
|
176
202
|
}
|
|
177
203
|
|
|
178
204
|
static inline char *fbuffer_cursor(FBuffer *fb)
|
|
@@ -182,7 +208,7 @@ static inline char *fbuffer_cursor(FBuffer *fb)
|
|
|
182
208
|
|
|
183
209
|
static inline void fbuffer_advance_to(FBuffer *fb, char *end)
|
|
184
210
|
{
|
|
185
|
-
fb
|
|
211
|
+
fbuffer_consumed(fb, (end - fb->ptr) - fb->len);
|
|
186
212
|
}
|
|
187
213
|
|
|
188
214
|
/*
|
|
@@ -223,14 +249,11 @@ static VALUE fbuffer_finalize(FBuffer *fb)
|
|
|
223
249
|
{
|
|
224
250
|
if (fb->io) {
|
|
225
251
|
fbuffer_flush(fb);
|
|
226
|
-
fbuffer_free(fb);
|
|
227
252
|
rb_io_flush(fb->io);
|
|
228
253
|
return fb->io;
|
|
229
254
|
} else {
|
|
230
|
-
|
|
231
|
-
fbuffer_free(fb);
|
|
232
|
-
return result;
|
|
255
|
+
return rb_utf8_str_new(FBUFFER_PTR(fb), FBUFFER_LEN(fb));
|
|
233
256
|
}
|
|
234
257
|
}
|
|
235
258
|
|
|
236
|
-
#endif
|
|
259
|
+
#endif // _FBUFFER_H_
|
|
@@ -5,6 +5,15 @@ if RUBY_ENGINE == 'truffleruby'
|
|
|
5
5
|
File.write('Makefile', dummy_makefile("").join)
|
|
6
6
|
else
|
|
7
7
|
append_cflags("-std=c99")
|
|
8
|
+
have_const("RUBY_TYPED_EMBEDDABLE", "ruby.h") # RUBY_VERSION >= 3.3
|
|
9
|
+
have_func("ruby_xfree_sized", "ruby.h") # RUBY_VERSION >= 4.1
|
|
10
|
+
|
|
8
11
|
$defs << "-DJSON_GENERATOR"
|
|
12
|
+
$defs << "-DJSON_DEBUG" if ENV.fetch("JSON_DEBUG", "0") != "0"
|
|
13
|
+
|
|
14
|
+
if enable_config('generator-use-simd', default=!ENV["JSON_DISABLE_SIMD"])
|
|
15
|
+
load __dir__ + "/../simd/conf.rb"
|
|
16
|
+
end
|
|
17
|
+
|
|
9
18
|
create_makefile 'json/ext/generator'
|
|
10
19
|
end
|