Bug #8935

Zlib::GzipWriter.new with block truncates output to 20 corrupted bytes

Added by DoNot BugMe 7 months ago. Updated 7 months ago.

[ruby-core:57305]
Status:Open
Priority:Normal
Assignee:-
Category:-
Target version:-
ruby -v:ruby 2.0.0p247 (2013-06-27 revision 41674) [i686-linux] Backport:1.9.3: UNKNOWN, 2.0.0: UNKNOWN

Description

=begin
(({Zlib::GzipWriter.new})) with block truncates output to 20 corrupted bytes.

#!/usr/bin/ruby -w

require "zlib"
require "stringio"

input = File.read "/dev/urandom", 200

corrupted and only 20 bytes long:

out1 = Zlib::GzipWriter.new(StringIO.new){ |gz| gz.write input }.close.string
puts out1.bytesize, out1.inspect, ""

good:

writer = Zlib::GzipWriter.new(StringIO.new)
writer.write input
out2 = writer.close.string
puts out2.bytesize, out2.inspect
=end

History

#1 Updated by DoNot BugMe 7 months ago

Sorry, another try without any formatting:

#!/usr/bin/ruby -w

require "zlib"
require "stringio"

input = File.read "/dev/urandom", 200

corrupted and only 20 bytes long:

out1 = Zlib::GzipWriter.new(StringIO.new){ |gz| gz.write input }.close.string
puts out1.bytesize, out1.inspect, ""

good:

writer = Zlib::GzipWriter.new(StringIO.new)
writer.write input
out2 = writer.close.string
puts out2.bytesize, out2.inspect

#2 Updated by Nobuyoshi Nakada 7 months ago

  • Description updated (diff)

#3 Updated by DoNot BugMe 7 months ago

Come to think of it, 20 bytes seems like what the gzip header + footer would take up...

#4 Updated by wind winy 7 months ago

Every ruby method can be accept a block.

Zlib::GzipWriter.new just return a object, not callback the block,

Zlib::GzipWriter.new(StringIO.new){ |gz| gz.write input }.close.string
is equal
Zlib::GzipWriter.new(StringIO.new).close.string

#5 Updated by DoNot BugMe 7 months ago

Whoa. Has that always been the case? Cause I'm pretty sure I saw this code used somewhere else too.

The problem with .open, which definitely takes a block, is that it only works with file names, not IO objects and thus not StringIO. But it's not unreasonable to want to transform a string to a gzipped string, right?

Also available in: Atom PDF