Bug #17661


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

Added by tenderlovemaking (Aaron Patterson) over 1 year ago. Updated 9 months ago.

Target version:


As reported here: 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.


file =
file.each_byte do |byte|
  p byte

Proposed fix is here:

Updated by xtkoba (Tee KOBAYASHI) over 1 year ago

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

Updated by wyhaines (Kirk Haines) over 1 year 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 #3

Updated by nobu (Nobuyoshi Nakada) about 1 year ago

  • Status changed from Open to Closed

Applied in changeset git|13939d61b4b69bd109c5f41303c79868d639fa44.

Check if closed after each yield [Bug #17661]

Updated by nobu (Nobuyoshi Nakada) about 1 year ago

Moved the check just after rb_yield.
And I found that each_codepoint also had a similar bug.
@wyhaines (Kirk Haines) Could you add a spec for the method?

Actions #5

Updated by nagachika (Tomoyuki Chikanaga) 10 months ago

  • Backport changed from 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN, 3.0: UNKNOWN to 2.6: UNKNOWN, 2.7: REQUIRED, 3.0: REQUIRED

Updated by nagachika (Tomoyuki Chikanaga) 9 months ago

  • Backport changed from 2.6: UNKNOWN, 2.7: REQUIRED, 3.0: REQUIRED to 2.6: UNKNOWN, 2.7: REQUIRED, 3.0: DONE

ruby_3_0 e6e25b794d8db52e1df85a02f28846ad7eb82d49 merged revision(s) 13939d61b4b69bd109c5f41303c79868d639fa44.


Also available in: Atom PDF