Project

General

Profile

Bug #8935

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

Added by DoNot BugMe over 2 years ago. Updated over 2 years ago.

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

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 [ruby-core:57306] Updated by DoNot BugMe over 2 years 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 [ruby-core:57307] Updated by Nobuyoshi Nakada over 2 years ago

  • Description updated (diff)

#3 [ruby-core:57308] Updated by DoNot BugMe over 2 years ago

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

#4 [ruby-core:57314] Updated by wind winy over 2 years 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 [ruby-core:57320] Updated by DoNot BugMe over 2 years 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