Skip to content

Coerce byte lengths before validating returned size - #79

Open
OskarEichler wants to merge 1 commit into
ruby:masterfrom
OskarEichler:codex/coerce-byte-length
Open

OskarEichler wants to merge 1 commit into
ruby:masterfrom
OskarEichler:codex/coerce-byte-length

Conversation

@OskarEichler

Copy link
Copy Markdown

Summary

Coerce byte lengths once before passing them to the selected backend, matching the integer conversion accepted by Random.bytes and avoiding false partial-read errors.

Reproduction and verification

SecureRandom.bytes(3.5).bytesize # 3
size = Object.new
def size.to_int; 3; end
SecureRandom.bytes(size).bytesize # 3

The baseline raises NotImplementedError even though the native backend returned the requested coerced number of bytes. Integers retain a fast path; random_bytes/hex and other formatter methods still call gen_random directly.

  • Existing rbenv exec rake test: 3 tests / 68 assertions, zero failures/errors on the baseline, this isolated branch and the cumulative release-based consumer, Ruby 4.0.6/macOS. The existing disabled PID-recycling stress test was not enabled.
  • Combined candidate: 1,539 length/formatter checks, 3 fallback checks and 13 error/coercion/Ractor checks pass. Package build/extraction and existing rdoc -C checks pass (100% of its documentation inventory).
  • Paired benchmark, 10,000 16-byte calls per round: bytes median 0.010524s baseline / 0.010591s candidate; random_bytes 0.010651s / 0.010635s. Both allocate 10,000 objects. These small timings do not support a speedup claim.
  • No tests/specs added or modified under the consumer repository's explicit policy. External scratch reproductions were used.

Breaking changes and limitations

Previously rejected fractional/to_int lengths now work; non-convertible inputs remain TypeError, with a clearer size diagnostic. Invalid Float conversion now agrees with Random.bytes. No RNG backend, random-byte content, or entropy algorithm changes.

No production execution, statistical randomness claim, or cross-platform CI claim. Older Ruby/native fallback environments need upstream CI. The shared Random::Formatter implementation belongs to Ruby itself and is not changed here.

This branch has not been deployed

No deployments
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