Hi,

At Thu, 22 Oct 2009 12:43:37 +0900,
Mike Pomraning wrote in [ruby-core:26222]:
> 4. Why this is very bad:
> 
> In practice, fds passed to for_fd() may be exposed by
> extension modules, which modules are responsible for the
> cleanup of the file descriptor.  Thus, close(2)ing upon
> finalization may rudely and dangerously close a file
> descriptor already closed and reassigned to some unrelated
> bit of code, causing baffling, "action at a distance"
> failures.

IO.for_fd is often dangerous.  Basically, such extension
libraries should expose IO instances, but not file descriptors.

This is a patch to add :autoclose option to IO.for_fd and
IO.new.


Index: io.c =================================================================== --- io.c (revision 25430) +++ io.c (working copy) @@ -129,5 +129,5 @@ static VALUE argf; static ID id_write, id_read, id_getc, id_flush, id_readpartial; static VALUE sym_mode, sym_perm, sym_extenc, sym_intenc, sym_encoding, sym_open_args; -static VALUE sym_textmode, sym_binmode; +static VALUE sym_textmode, sym_binmode, sym_autoclose; struct timeval rb_time_interval(VALUE); @@ -4296,4 +4296,7 @@ extract_binmode(VALUE opthash, int *fmod if ((*fmode & FMODE_BINMODE) && (*fmode & FMODE_TEXTMODE)) rb_raise(rb_eArgError, "both textmode and binmode specified"); + v = rb_hash_aref(opthash, sym_autoclose); + if (v == Qfalse) + *fmode |= FMODE_PREP; } } @@ -9936,3 +9939,4 @@ Init_IO(void) sym_textmode = ID2SYM(rb_intern("textmode")); sym_binmode = ID2SYM(rb_intern("binmode")); + sym_autoclose = ID2SYM(rb_intern("autoclose")); }
-- Nobu Nakada