The Crystal Programming Language Forum

Arithmetic overflow (OverflowError) when Gzip::Writer

hello everyone~
my writegz.cr is this:

require "gzip"


maxnum = ARGV[0].to_i
rep = ARGV[1].to_i
line = ARGV[2].to_s * rep

File.open("file.txt.gz", "w") do |f|
	Gzip::Writer.open(f) do |gfile|
		while maxnum > 0
			gfile.puts(line)
			maxnum -=1
		end
	end
end
puts "write done"

After crystal build writegz.cr --release,
then run ./writegz 2741990 1000 A >/dev/null get error:

Unhandled exception: Arithmetic overflow (OverflowError)
  from ???
  from /usr/share/crystal/src/gzip/writer.cr:0:0 in 'write'
  from /usr/share/crystal/src/string.cr:4117:5 in '__crystal_main'
  from /usr/share/crystal/src/crystal/main.cr:47:14 in 'main'
  from __libc_start_main
  from ???
  from ???

then run ./writegz 2741990 10 A >/dev/null get no error.
then run ./writegz 1741990 1000 A >/dev/null get no error

when build crystal build writegz.cr --error-trace --debug, then run ./writegz 2741990 1000 A >/dev/null get detail error:

Unhandled exception: Arithmetic overflow (OverflowError)
  from /usr/share/crystal/src/gzip/writer.cr:0:12 in 'write'
  from /usr/share/crystal/src/io.cr:481:7 in 'write_utf8'
  from /usr/share/crystal/src/string.cr:4285:5 in 'to_s'
  from /usr/share/crystal/src/io.cr:184:5 in '<<'
  from /usr/share/crystal/src/io.cr:227:5 in 'puts'
  from writegz.cr:11:4 in '__crystal_main'
  from /usr/share/crystal/src/crystal/main.cr:97:5 in 'main_user_code'
  from /usr/share/crystal/src/crystal/main.cr:86:7 in 'main'
  from /usr/share/crystal/src/crystal/main.cr:106:3 in 'main'
  from __libc_start_main
  from ???
  from ???

$crystal --version
Crystal 0.31.1 [0e2e1d067] (2019-09-30)

LLVM: 8.0.0
Default target: x86_64-unknown-linux-gnu

$cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)




when Gzip::Reader with big gzip file also get the same error:

Unhandled exception: Arithmetic overflow (OverflowError)
  from ???
  from /usr/share/crystal/src/gzip/reader.cr:0:0 in 'unbuffered_read'
  from /usr/share/crystal/src/io/buffered.cr:215:5 in '__crystal_main'
  from /usr/share/crystal/src/crystal/main.cr:47:14 in 'main'
  from __libc_start_main
  from ???
  from ???

the code is:

require "gzip"
ifile = "large.gz" # large.gz is more than 64740990 lines
Gzip::Reader.open(ifile) do |gfile|
    gfile.each_line do |gline|
		puts gline
	end
end





when I use Process.run with the same big gzip file,get no error! the code is:

ifile = "large.gz" # large.gz is more than 64740990 lines
Process.run("gzip -dc #{ifile}", shell: true) do |proc|
	while line = proc.output.gets
		puts line
	end
end

Thanks~
Regard
Si

This bug has been fixed in #8418. Please update to the latest Crystal release 0.32.1.

1 Like

Thanks~ I usually search the error "crysal Arithmetic overflow " in google, but I should firstly try to search the error in the crstal github issue. Sorry the question!