Skip to content

Reject negative read lengths before changing the buffer - #69

Open
OskarEichler wants to merge 1 commit into
ruby:masterfrom
OskarEichler:codex/negative-buffered-io
Open

Reject negative read lengths before changing the buffer#69
OskarEichler wants to merge 1 commit into
ruby:masterfrom
OskarEichler:codex/negative-buffered-io

Conversation

@OskarEichler

Copy link
Copy Markdown

Summary

Reject a negative read length before touching the destination or advancing the read-buffer offset. The current failure can move the offset backwards and expose bytes already consumed.

Reproduction

require 'net/protocol'
require 'stringio'
io = Net::BufferedIO.new(StringIO.new('abcdef'))
p io.read(1) # "a"
begin
  io.read(-1)
rescue => e
  p e.class # before: NoMethodError; after: ArgumentError
end
p io.read_all # before: "abcdef"; after: "bcdef"

Focused checks cover three initial offsets, small and arbitrarily large negative integers, both ignore_eof values, unchanged destination contents, zero/positive reads and EOF behavior.

Verification

Compatibility and limitations

Negative lengths now raise ArgumentError rather than an incidental NoMethodError/RangeError. This is an intentional error-contract correction. Valid lengths and existing destination-append semantics are unchanged; no new coercion rules are introduced.

The patch also applies to installed release 0.3.0, whose runtime file matches the reviewed master. Gem version, dependencies and Ruby >= 2.6 requirement stay unchanged. Only macOS/Ruby 4.0.6 was run locally; other Ruby/OS combinations require upstream CI. No production traffic or external service was used.

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