Skip to content

Type Zlib::GzipReader#read without a length as returning String - #3315

Merged
ksss merged 1 commit into
ruby:masterfrom
hasannadeem:gzip-reader-read-all
Nov 17, 2026
Merged

ksss merged 1 commit into
ruby:masterfrom
hasannadeem:gzip-reader-read-all

Conversation

@hasannadeem

Copy link
Copy Markdown
Contributor

Zlib::GzipReader#read was typed (?int? length, ?string? outbuf) -> String?, so gz.read.upcase was a type error. Without a length, rb_gzreader_read calls gzfile_read_all, which returns an empty string at the end of the stream, never nil. Only gzfile_read with a length returns nil there:

gz = Zlib::GzipReader.new(StringIO.new(Zlib.gzip("hello")))
gz.read     # => "hello"
gz.read     # => ""
gz.read(1)  # => nil

This splits it the same way IO#read already is:

def read: (?nil, ?string? outbuf) -> String
        | (int? length, ?string? outbuf) -> String?

test_read now checks the exact return types, including "" from read and nil from read(1) once the stream is exhausted. It passes on Ruby 3.4 and 4.0.7. The runtime type check can't tell String from String?, so the test can't fail on master. Steep shows the difference: gz.read.upcase and gz.read(nil, buf).upcase are errors on master and pass here, while gz.read(3).upcase is still an error. rbs -r zlib validate passes and rake annotate leaves the file unchanged.

This touches the same files as #3314 but merges cleanly with it, and the zlib tests pass with both applied.

Like IO#read, GzipReader#read reads to the end of the stream when the
length is nil or omitted, and returns an empty string at the end of the
stream rather than nil. Only a read with a length returns nil there.
@ksss
ksss added this pull request to the merge queue Nov 17, 2026
Merged via the queue into ruby:master with commit e575cb0 Nov 17, 2026
25 checks passed
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.

2 participants