Project

General

Profile

Actions

Bug #17661

open

IO#each will segfault when if file is closed inside an `each_byte` block

Added by tenderlovemaking (Aaron Patterson) 4 months ago. Updated 4 months ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:102634]

Description

As reported here: https://twitter.com/asterite/status/1363487990203506689 when iterating through a file's contents with #each_byte, if the filehandle is closed inside of the block yielded to by #each byte, this condition is not detected, and a segmentation fault is thrown.

Repro:

file = http://File.open(__FILE__)
file.each_byte do |byte|
  p byte
  file.close
end

Proposed fix is here: https://github.com/ruby/ruby/pull/4217

Updated by xtkoba (Tee KOBAYASHI) 4 months ago

Probably 's|http://||' to the reproducer?

Updated by wyhaines (Kirk Haines) 4 months ago

Aaron filed this bug on my behalf, as I was having issues with my account. Those issues appear to be issues no more, however.

In the interest of having details appear in the issue tracker and not just on GitHub, I'll reiterate the description of the fix:

I have fixed the problem by adding a check inside the inner loop that iterates over the filehandle read buffer, and I have added a spec that will both expose the bug in an unfixed ruby, and pass in a fixed ruby.

The bug exists on every build of Ruby that I have available on my systems, and in looking at the history of io.c, it likely exists all the way back to 1.9.1.

Actions

Also available in: Atom PDF