ciborium 0.1.0 → 0.1.1

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: 134062e0f9eac0198937cb3b75bf5fa10e5d2d164c2115e665d447b44fbf1ecb
4
- data.tar.gz: c1a7df490adfd99e368e960543a1334fb7faefda53634669d91f10ae29137d8a
3
+ metadata.gz: ab8765be2dadb6db30760c9c518f2b9069df308de37ee9ad0eba140b5a8afc32
4
+ data.tar.gz: 4872bf482689890b26b80be1cb56ffce61e9df17da9975becd034a7e1b6185a3
5
5
  SHA512:
6
- metadata.gz: 408467f3bab40ca8c346807f11df507f5e0c7a038f2891f2bba7a276bff23b40ed380ea04a424c6dd102a62ba4bfbae38520ce4c85328ce1dc018501ac584a3a
7
- data.tar.gz: 98a49f65d0d2888e3fa6aa4f5944ab1b6602deeb5d7e79c6f2d298c61161eb9ed9fe0abb50cf292ab5f0bc2f14abce659a5628ea0157cde1d9be6148d2cea281
6
+ metadata.gz: a4050d850916403cb833f20c401e3e26778be1cfa31d946f6e0ba2ce10b6d5758b861c53613ef2e58dca52ee50f5881004c98ea1c46cbd859d1b863f794eac39
7
+ data.tar.gz: 037fd9e7ef419cedadcb2e1e956fd36cb50a0db7e8eb7bc35b6899c097652c9252398af0b5b172dbf2b342062220c8b44977aeeee6349a2377dad163fb127814
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.1 (2026-11-11)
4
+
5
+ - Decode every NaN to `Float::NAN`. Repeated NaN map keys are now duplicate keys, instead of costing time quadratic in their number.
6
+ - Fix decoding when GC compaction runs mid-decode, such as under `GC.auto_compact` or inside an extension, which could raise a spurious duplicate key error or crash.
7
+ - `Decoder#read_array_header` and `#read_map_header` follow `profile: :ctap2`, rejecting heads not in their shortest form.
8
+ - Raise `EncodeError` when nesting would overflow the machine stack, as it could inside a Fiber at a high `max_depth`.
9
+
3
10
  ## 0.1.0 (2026-11-11)
4
11
 
5
12
  - Initial release: a CBOR encoder and decoder backed by a Rust extension, where tags carry meaning only through allowlisted extensions.
@@ -6,15 +6,18 @@ pub(crate) const FLAG_CTAP2: u32 = 1 << 2;
6
6
  // them.
7
7
  const PREALLOCATE_LIMIT: usize = 256;
8
8
 
9
+ // Containers and pending keys are referred to by their index in `pin`, since compaction may move
10
+ // the objects and only updates references the GC can see.
9
11
  enum Frame {
10
12
  Array {
11
- array: magnus::RArray,
13
+ slot: usize,
12
14
  remaining: Option<usize>,
13
15
  },
14
16
  Map {
15
- hash: magnus::RHash,
17
+ slot: usize,
16
18
  remaining: Option<usize>,
17
- key: Option<magnus::Value>,
19
+ // Whether a key awaits its value, held in `pin` just above the map.
20
+ has_key: bool,
18
21
  // Byte ranges of the current and the previous key, for the ctap2 profile's ordering.
19
22
  key_start: usize,
20
23
  previous_key: Option<(usize, usize)>,
@@ -34,12 +37,16 @@ pub(crate) struct Decoder<'a> {
34
37
  // Tag number to a callable taking (tag, content), for allowlisted extensions only.
35
38
  handlers: Option<magnus::RHash>,
36
39
  stack: Vec<Frame>,
37
- // Containers held in `stack` live on the Rust heap, which the GC does not scan. This array
38
- // keeps them reachable, and is itself reachable through the Decoder on the native stack. It
39
- // is not registered globally, so a NoMemoryError unwinding past Rust leaves nothing behind.
40
+ // Open containers and pending keys, in the order of `stack`. Frames live on the Rust heap,
41
+ // which the GC does not scan. This array is reachable through the Decoder on the native
42
+ // stack, and is not registered globally, so a NoMemoryError unwinding past Rust leaves
43
+ // nothing behind.
40
44
  pin: magnus::RArray,
41
45
  tagged_class: magnus::RClass,
42
46
  simple_class: magnus::RClass,
47
+ // Float::NAN, which every NaN decodes to. Separate NaN Floats are never eql?, so as map keys
48
+ // each would collide with every earlier one.
49
+ nan: magnus::Value,
43
50
  }
44
51
 
45
52
  impl<'a> Decoder<'a> {
@@ -65,6 +72,7 @@ impl<'a> Decoder<'a> {
65
72
  pin: ruby.ary_new(),
66
73
  tagged_class: ciborium.const_get("Tagged")?,
67
74
  simple_class: ciborium.const_get("Simple")?,
75
+ nan: ruby.class_float().const_get("NAN")?,
68
76
  })
69
77
  }
70
78
 
@@ -83,7 +91,7 @@ impl<'a> Decoder<'a> {
83
91
  loop {
84
92
  let start = self.cursor.pos();
85
93
  if let Some(Frame::Map {
86
- key: None,
94
+ has_key: false,
87
95
  key_start,
88
96
  ..
89
97
  }) = self.stack.last_mut()
@@ -97,6 +105,7 @@ impl<'a> Decoder<'a> {
97
105
  let value = match header {
98
106
  ciborium_ll::Header::Positive(n) => self.ruby.integer_from_u64(n).as_value(),
99
107
  ciborium_ll::Header::Negative(n) => self.negative(n)?,
108
+ ciborium_ll::Header::Float(f) if f.is_nan() => self.nan,
100
109
  ciborium_ll::Header::Float(f) => self.ruby.float_from_f64(f).as_value(),
101
110
  ciborium_ll::Header::Simple(n) => self.simple(n)?,
102
111
  ciborium_ll::Header::Bytes(len) => self.string(len, false)?,
@@ -109,8 +118,8 @@ impl<'a> Decoder<'a> {
109
118
  let capacity = len
110
119
  .map_or(0, |n| n.min(self.cursor.remaining()).min(PREALLOCATE_LIMIT));
111
120
  let array = self.ruby.ary_new_capa(capacity);
112
- self.open(Frame::Array {
113
- array,
121
+ self.open_container(array.as_value(), |slot| Frame::Array {
122
+ slot,
114
123
  remaining: len,
115
124
  })?;
116
125
  continue;
@@ -121,10 +130,10 @@ impl<'a> Decoder<'a> {
121
130
  self.ruby.hash_new().as_value()
122
131
  } else {
123
132
  let hash = self.ruby.hash_new();
124
- self.open(Frame::Map {
125
- hash,
133
+ self.open_container(hash.as_value(), |slot| Frame::Map {
134
+ slot,
126
135
  remaining: len,
127
- key: None,
136
+ has_key: false,
128
137
  key_start: 0,
129
138
  previous_key: None,
130
139
  })?;
@@ -146,15 +155,21 @@ impl<'a> Decoder<'a> {
146
155
  if self.stack.len() >= self.max_depth {
147
156
  return Err(crate::error::Error::Depth(self.max_depth));
148
157
  }
149
- match &frame {
150
- Frame::Array { array, .. } => self.pin.push(*array)?,
151
- Frame::Map { hash, .. } => self.pin.push(*hash)?,
152
- Frame::Tag { .. } => {}
153
- }
154
158
  self.stack.push(frame);
155
159
  Ok(())
156
160
  }
157
161
 
162
+ fn open_container(
163
+ &mut self,
164
+ container: magnus::Value,
165
+ frame: impl FnOnce(usize) -> Frame,
166
+ ) -> Result<(), crate::error::Error> {
167
+ let slot = self.pin.len();
168
+ self.open(frame(slot))?;
169
+ self.pin.push(container)?;
170
+ Ok(())
171
+ }
172
+
158
173
  fn close(&mut self) -> Result<(), crate::error::Error> {
159
174
  if let Some(Frame::Array { .. } | Frame::Map { .. }) = self.stack.pop() {
160
175
  self.pin.pop::<magnus::Value>()?;
@@ -172,7 +187,8 @@ impl<'a> Decoder<'a> {
172
187
  loop {
173
188
  let finished = match self.stack.last_mut() {
174
189
  None => return Ok(Some(value)),
175
- Some(Frame::Array { array, remaining }) => {
190
+ Some(Frame::Array { slot, remaining }) => {
191
+ let array: magnus::RArray = self.pin.entry(*slot as isize)?;
176
192
  array.push(value)?;
177
193
  match remaining {
178
194
  Some(n) => {
@@ -186,13 +202,13 @@ impl<'a> Decoder<'a> {
186
202
  }
187
203
  }
188
204
  Some(Frame::Map {
189
- hash,
205
+ slot,
190
206
  remaining,
191
- key,
207
+ has_key,
192
208
  key_start,
193
209
  previous_key,
194
210
  }) => {
195
- let Some(k) = key.take() else {
211
+ if !*has_key {
196
212
  if self.flags & FLAG_CTAP2 != 0 {
197
213
  let current = (*key_start, self.cursor.pos());
198
214
  let input = self.cursor.input();
@@ -210,17 +226,18 @@ impl<'a> Decoder<'a> {
210
226
  }
211
227
  *previous_key = Some(current);
212
228
  }
213
- *key = Some(value);
229
+ *has_key = true;
214
230
  self.pin.push(value)?;
215
231
  return Ok(None);
216
- };
232
+ }
233
+ *has_key = false;
234
+ let key: magnus::Value = self.pin.pop()?;
235
+ let hash: magnus::RHash = self.pin.entry(*slot as isize)?;
217
236
  let size = hash.len();
218
- hash.aset(k, value)?;
237
+ hash.aset(key, value)?;
219
238
  if hash.len() == size && self.flags & FLAG_LAST_WINS == 0 {
220
239
  return Err(crate::error::Error::malformed("duplicate map key"));
221
240
  }
222
- let hash = *hash;
223
- self.pin.pop::<magnus::Value>()?;
224
241
  match remaining {
225
242
  Some(n) => {
226
243
  *n -= 1;
@@ -245,18 +262,19 @@ impl<'a> Decoder<'a> {
245
262
  }
246
263
 
247
264
  fn close_indefinite(&mut self, start: usize) -> Result<magnus::Value, crate::error::Error> {
248
- use magnus::value::ReprValue as _;
249
265
  let value = match self.stack.last() {
250
- Some(Frame::Array {
251
- array,
252
- remaining: None,
253
- }) => array.as_value(),
254
- Some(Frame::Map {
255
- hash,
256
- remaining: None,
257
- key: None,
258
- ..
259
- }) => hash.as_value(),
266
+ Some(
267
+ Frame::Array {
268
+ slot,
269
+ remaining: None,
270
+ }
271
+ | Frame::Map {
272
+ slot,
273
+ remaining: None,
274
+ has_key: false,
275
+ ..
276
+ },
277
+ ) => self.pin.entry(*slot as isize)?,
260
278
  _ => {
261
279
  return Err(crate::error::Error::malformed(format!(
262
280
  "unexpected break at offset {start}"
@@ -320,6 +338,6 @@ impl<'a> Decoder<'a> {
320
338
  }
321
339
 
322
340
  fn expects_key(&self) -> bool {
323
- matches!(self.stack.last(), Some(Frame::Map { key: None, .. }))
341
+ matches!(self.stack.last(), Some(Frame::Map { has_key: false, .. }))
324
342
  }
325
343
  }
@@ -60,6 +60,14 @@ impl<'a> Encoder<'a> {
60
60
  self.max_depth
61
61
  )));
62
62
  }
63
+ // A Fiber's machine stack can run out well before max_depth, and Ruby recovers from
64
+ // overflowing it by longjmp across these frames.
65
+ // SAFETY: the GVL is held.
66
+ if unsafe { rb_sys::ruby_stack_check() } != 0 {
67
+ return Err(crate::error::Error::Encode(
68
+ "nesting exceeds the available stack".to_owned(),
69
+ ));
70
+ }
63
71
  if value.is_nil() {
64
72
  return self.push(ciborium_ll::Header::Simple(22));
65
73
  }
@@ -57,10 +57,12 @@ fn scan(
57
57
 
58
58
  /// Reads the head at `offset` as `[major type, argument, end offset]`, or nil when the input
59
59
  /// ends before it does. The argument is nil for indefinite lengths and for major type 7.
60
+ /// `flags` are those of `decode`, of which only the ctap2 profile applies.
60
61
  fn read_head(
61
62
  ruby: &magnus::Ruby,
62
63
  input: magnus::RString,
63
64
  offset: usize,
65
+ flags: u32,
64
66
  ) -> Result<Option<(u8, Option<u64>, usize)>, magnus::Error> {
65
67
  // SAFETY: reading a head runs no Ruby code, so nothing can modify or free the string meanwhile.
66
68
  let mut cursor = crate::cursor::Cursor::new(unsafe { input.as_slice() }, offset);
@@ -69,6 +71,10 @@ fn read_head(
69
71
  Err(crate::error::Error::Incomplete) => return Ok(None),
70
72
  Err(error) => return Err(error.into_ruby(ruby)),
71
73
  };
74
+ if flags & crate::decode::FLAG_CTAP2 != 0 {
75
+ crate::canonical::Ctap2::check_head(cursor.input()[offset], &header, offset)
76
+ .map_err(|error| error.into_ruby(ruby))?;
77
+ }
72
78
  let (major, argument) = match header {
73
79
  ciborium_ll::Header::Positive(n) => (0, Some(n)),
74
80
  ciborium_ll::Header::Negative(n) => (1, Some(n)),
@@ -144,7 +150,7 @@ fn init(ruby: &magnus::Ruby) -> Result<(), magnus::Error> {
144
150
  let scan_state = native.define_class("ScanState", ruby.class_object())?;
145
151
  scan_state.define_singleton_method("new", magnus::function!(crate::scan::ScanState::new, 0))?;
146
152
  scan_state.define_method("clear", magnus::method!(crate::scan::ScanState::clear, 0))?;
147
- native.define_module_function("read_head", magnus::function!(crate::read_head, 2))?;
153
+ native.define_module_function("read_head", magnus::function!(crate::read_head, 3))?;
148
154
  native.define_module_function("encode_head", magnus::function!(crate::encode_head, 2))?;
149
155
  native.const_set("FLAG_SYMBOLIZE_KEYS", crate::decode::FLAG_SYMBOLIZE_KEYS)?;
150
156
  native.const_set("FLAG_LAST_WINS", crate::decode::FLAG_LAST_WINS)?;
@@ -69,7 +69,8 @@ module Ciborium
69
69
  read_container_head(4, "array")
70
70
  end
71
71
 
72
- # Reads the head of a definite-length map and returns its number of pairs.
72
+ # Reads the head of a definite-length map and returns its number of pairs. Keys read one by
73
+ # one afterwards are not checked for duplicates or, under the ctap2 profile, for their order.
73
74
  #
74
75
  #: () -> Integer
75
76
  def read_map_header
@@ -116,7 +117,7 @@ module Ciborium
116
117
 
117
118
  #: (Integer major, String name) -> Integer
118
119
  private def read_container_head(major, name)
119
- head = Native.read_head(@buffer, @pos)
120
+ head = Native.read_head(@buffer, @pos, @options.flags)
120
121
  raise IncompleteError, "unexpected end of input" unless head
121
122
 
122
123
  actual, length, pos = head
@@ -2,5 +2,5 @@
2
2
  # rbs_inline: enabled
3
3
 
4
4
  module Ciborium
5
- VERSION = "0.1.0" #: String
5
+ VERSION = "0.1.1" #: String
6
6
  end
@@ -48,7 +48,8 @@ module Ciborium
48
48
  # : () -> Integer
49
49
  def read_array_header: () -> Integer
50
50
 
51
- # Reads the head of a definite-length map and returns its number of pairs.
51
+ # Reads the head of a definite-length map and returns its number of pairs. Keys read one by
52
+ # one afterwards are not checked for duplicates or, under the ctap2 profile, for their order.
52
53
  #
53
54
  # : () -> Integer
54
55
  def read_map_header: () -> Integer
@@ -16,7 +16,7 @@ module Ciborium
16
16
  def clear: () -> void
17
17
  end
18
18
 
19
- def self.read_head: (String input, Integer offset) -> [Integer, Integer?, Integer]?
19
+ def self.read_head: (String input, Integer offset, Integer flags) -> [Integer, Integer?, Integer]?
20
20
 
21
21
  def self.encode_head: (Integer major, Integer argument) -> String
22
22
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ciborium
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sorah Fukumori