1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* io.c (pipe_open): Close pipes when rb_execarg_fixup() raises

an exception.
  (rb_execarg_fixup_v): New function.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@46232 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2014-05-29 14:13:20 +00:00
parent 6f39cf2d1e
commit 52e91e0f72
3 changed files with 56 additions and 23 deletions

View file

@ -1,3 +1,9 @@
Thu May 29 23:11:20 2014 Tanaka Akira <akr@fsij.org>
* io.c (pipe_open): Close pipes when rb_execarg_fixup() raises
an exception.
(rb_execarg_fixup_v): New function.
Thu May 29 22:18:57 2014 Tanaka Akira <akr@fsij.org> Thu May 29 22:18:57 2014 Tanaka Akira <akr@fsij.org>
* test/lib/minitest/unit.rb (capture_subprocess_io): Close fds. * test/lib/minitest/unit.rb (capture_subprocess_io): Close fds.

18
io.c
View file

@ -5869,6 +5869,13 @@ popen_exec(void *pp, char *errmsg, size_t errmsg_len)
} }
#endif #endif
static VALUE
rb_execarg_fixup_v(VALUE execarg_obj)
{
rb_execarg_fixup(execarg_obj);
return Qnil;
}
static VALUE static VALUE
pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convconfig) pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convconfig)
{ {
@ -5884,6 +5891,7 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convc
char errmsg[80] = { '\0' }; char errmsg[80] = { '\0' };
#endif #endif
#if defined(HAVE_FORK) || defined(HAVE_SPAWNV) #if defined(HAVE_FORK) || defined(HAVE_SPAWNV)
int state;
struct popen_arg arg; struct popen_arg arg;
int e = 0; int e = 0;
#endif #endif
@ -5964,7 +5972,15 @@ pipe_open(VALUE execarg_obj, const char *modestr, int fmode, convconfig_t *convc
rb_sys_fail_str(prog); rb_sys_fail_str(prog);
} }
if (!NIL_P(execarg_obj)) { if (!NIL_P(execarg_obj)) {
rb_execarg_fixup(execarg_obj); rb_protect(rb_execarg_fixup_v, execarg_obj, &state);
if (state) {
if (0 <= arg.write_pair[0]) close(arg.write_pair[0]);
if (0 <= arg.write_pair[1]) close(arg.write_pair[1]);
if (0 <= arg.pair[0]) close(arg.pair[0]);
if (0 <= arg.pair[1]) close(arg.pair[1]);
rb_jump_tag(state);
}
# if defined(HAVE_FORK) # if defined(HAVE_FORK)
pid = rb_fork_async_signal_safe(&status, popen_exec, &arg, arg.eargp->redirect_fds, errmsg, sizeof(errmsg)); pid = rb_fork_async_signal_safe(&status, popen_exec, &arg, arg.eargp->redirect_fds, errmsg, sizeof(errmsg));
# else # else

View file

@ -733,11 +733,14 @@ class TestProcess < Test::Unit::TestCase
} }
with_pipe {|r, w| with_pipe {|r, w|
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('me')"]) io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('me')"])
w.close begin
errmsg = io.read w.close
assert_equal("", r.read) errmsg = io.read
assert_not_equal("", errmsg) assert_equal("", r.read)
Process.wait assert_not_equal("", errmsg)
ensure
io.close
end
} }
with_pipe {|r, w| with_pipe {|r, w|
errmsg = `#{RUBY} -e "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts(123)"` errmsg = `#{RUBY} -e "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts(123)"`
@ -785,29 +788,38 @@ class TestProcess < Test::Unit::TestCase
} }
with_pipe {|r, w| with_pipe {|r, w|
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('me')", :close_others=>true]) io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('me')", :close_others=>true])
w.close begin
errmsg = io.read w.close
assert_equal("", r.read) errmsg = io.read
assert_not_equal("", errmsg) assert_equal("", r.read)
Process.wait assert_not_equal("", errmsg)
ensure
io.close
end
} }
with_pipe {|r, w| with_pipe {|r, w|
w.close_on_exec = false w.close_on_exec = false
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('mo')", :close_others=>false]) io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('mo')", :close_others=>false])
w.close begin
errmsg = io.read w.close
assert_equal("mo\n", r.read) errmsg = io.read
assert_equal("", errmsg) assert_equal("mo\n", r.read)
Process.wait assert_equal("", errmsg)
ensure
io.close
end
} }
with_pipe {|r, w| with_pipe {|r, w|
w.close_on_exec = false w.close_on_exec = false
io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('mo')", :close_others=>nil]) io = IO.popen([RUBY, "-e", "STDERR.reopen(STDOUT); IO.new(#{w.fileno}, 'w').puts('mo')", :close_others=>nil])
w.close begin
errmsg = io.read w.close
assert_equal("mo\n", r.read) errmsg = io.read
assert_equal("", errmsg) assert_equal("mo\n", r.read)
Process.wait assert_equal("", errmsg)
ensure
io.close
end
} }
} }
@ -1131,8 +1143,7 @@ class TestProcess < Test::Unit::TestCase
Process.wait spawn([RUBY, "poiu"], "-e", "exit 4") Process.wait spawn([RUBY, "poiu"], "-e", "exit 4")
assert_equal(4, $?.exitstatus) assert_equal(4, $?.exitstatus)
assert_equal("1", IO.popen([[RUBY, "qwerty"], "-e", "print 1"]).read) assert_equal("1", IO.popen([[RUBY, "qwerty"], "-e", "print 1"]) {|f| f.read })
Process.wait
write_file("s", <<-"End") write_file("s", <<-"End")
exec([#{RUBY.dump}, "lkjh"], "-e", "exit 5") exec([#{RUBY.dump}, "lkjh"], "-e", "exit 5")