Skip to content

Keep the input when the inflate block exits on pending output - #188

Open
hsbt wants to merge 1 commit into
masterfrom
claude/elegant-colden-8735b1
Open

hsbt wants to merge 1 commit into
masterfrom
claude/elegant-colden-8735b1

Conversation

@hsbt

@hsbt hsbt commented Nov 10, 2026

Copy link
Copy Markdown
Member

In the block form of Zlib::Inflate#inflate, a call that starts with 16 KiB of output left by the previous call yields that output before it takes any input. If the block breaks or raises there, including through Thread#raise, the input of that call is lost and the stream later fails with Zlib::BufError. This happens with or without buffer:.

src = "a" * 16384 + "b" * 16384
data = Zlib::Deflate.deflate(src, 0)
head = data.byteslice(0, 2 + 5 + 16384)
inf = Zlib::Inflate.new
out = +""
inf.inflate(head) { |c| out << c }
inf.inflate(data.byteslice(head.bytesize..)) { |c| out << c; break }
inf.inflate(nil) { |c| out << c } # Zlib::BufError, expected out == src

The output buffer is now expanded before the input is taken out of the stream, so the input stays there when the block exits, as it already did for a yield in the middle of a call. #186 does not cover this yield, since it runs before the rb_protect that #186 adds, and the two do not conflict.

Generated with Claude Code

In the block form, a call that starts with 16 KiB of output left by the previous call yields it before taking any input. When the block broke or raised there, the input of that call was dropped and the stream failed later with Zlib::BufError.

  src = "a" * 16384 + "b" * 16384
  data = Zlib::Deflate.deflate(src, 0)
  head = data.byteslice(0, 2 + 5 + 16384)
  inf = Zlib::Inflate.new
  out = +""
  inf.inflate(head) { |c| out << c }
  inf.inflate(data.byteslice(head.bytesize..)) { |c| out << c; break }
  inf.inflate(nil) { |c| out << c }
  # expected: out == src
  # actual:   Zlib::BufError

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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