Skip to content

[LOW] Prevent Unpacker#skip_nil from replaying nil indefinitely - #41

Open
OskarEichler wants to merge 1 commit into
cabo:masterfrom
OskarEichler:codex/advance-after-skip-nil
Open

[LOW] Prevent Unpacker#skip_nil from replaying nil indefinitely#41
OskarEichler wants to merge 1 commit into
cabo:masterfrom
OskarEichler:codex/advance-after-skip-nil

Conversation

@OskarEichler

Copy link
Copy Markdown

Summary

CBOR::Unpacker#skip_nil consumes the nil byte from the input buffer but leaves it cached as the current head byte. Every later skip_nil call therefore returns true for the same value, and the next read also replays nil instead of returning the following object.

This resets the cached head after a successful skip so the stream advances exactly once.

Reproduction

require "cbor"

unpacker = CBOR::Unpacker.new
unpacker.feed("\xf6\x01".b)

p 3.times.map { unpacker.skip_nil }
p unpacker.read

0.5.10.3 prints [true, true, true] and then nil. A caller using while unpacker.skip_nil never reaches the following object. This branch prints [true, false, false] and then 1.

Verification

  • Exact 0.5.10.3 release and patched behavior were compared with the focused model above.
  • A non-nil head remains cached for the following read, preserving the intended peek behavior.
  • Empty input still raises EOFError.
  • Existing suite: 170 examples, 0 failures on Ruby 4.0.6.
  • rake compile, gem build cbor.gemspec, and git diff --check pass.

No tests were changed because this repository asks contributors not to add or modify tests.

Scope and limitations

The affected entry point is the low-level streaming Unpacker#skip_nil API. The infinite loop requires a caller to repeat skip_nil; it remains interruptible Ruby code, unlike a native-extension loop. Normal CBOR.decode does not use this method.

Breaking changes

skip_nil now consumes a nil exactly once, as its name and documentation imply. Code that relied on repeatedly observing the same skipped nil will behave differently; valid stream decoding is otherwise unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant