From 05cbca84fb81d290bc6b9a07f6cb5d2cb81111d5 Mon Sep 17 00:00:00 2001 From: nobu Date: Tue, 14 Jul 2009 07:13:11 +0000 Subject: * io.c (rb_io_initialize): check if the descriptor can be accessed in the specified open mode. [ruby-dev:38571] git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@24102 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ io.c | 22 +++++++++++++++++----- test/ruby/test_io.rb | 3 +++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index fd1e1503d..a2f1ebf5e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Tue Jul 14 16:13:04 2009 Nobuyoshi Nakada + + * io.c (rb_io_initialize): check if the descriptor can be accessed + in the specified open mode. [ruby-dev:38571] + Tue Jul 14 09:26:14 2009 Hidetoshi NAGAI * ext/tk/lib/multi-tk.rb: Long-term-callback support isn't stable yet. diff --git a/io.c b/io.c index 45c4a7321..4e576e710 100644 --- a/io.c +++ b/io.c @@ -6342,7 +6342,11 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io) int fd, fmode, oflags = O_RDONLY; convconfig_t convconfig; VALUE opt; +#if defined(HAVE_FCNTL) && defined(F_GETFL) + int ofmode; +#else struct stat st; +#endif rb_secure(4); @@ -6351,15 +6355,23 @@ rb_io_initialize(int argc, VALUE *argv, VALUE io) rb_io_extract_modeenc(&vmode, 0, opt, &oflags, &fmode, &convconfig); fd = NUM2INT(fnum); +#if defined(HAVE_FCNTL) && defined(F_GETFL) + oflags = fcntl(fd, F_GETFL); + if (oflags == -1) rb_sys_fail(0); +#else if (fstat(fd, &st) == -1) rb_sys_fail(0); +#endif UPDATE_MAXFD(fd); - if (NIL_P(vmode)) { #if defined(HAVE_FCNTL) && defined(F_GETFL) - oflags = fcntl(fd, F_GETFL); - if (oflags == -1) rb_sys_fail(0); - fmode = rb_io_oflags_fmode(oflags); -#endif + ofmode = rb_io_oflags_fmode(oflags); + if (NIL_P(vmode)) { + fmode = ofmode; + } + else if ((~ofmode & fmode) & FMODE_READWRITE) { + VALUE error = INT2FIX(EINVAL); + rb_exc_raise(rb_class_new_instance(1, &error, rb_eSystemCallError)); } +#endif MakeOpenFile(io, fp); fp->fd = fd; fp->mode = fmode; diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index 69e3666cf..8a5b878f3 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -1379,6 +1379,9 @@ class TestIO < Test::Unit::TestCase fd = IO.sysopen(t.path, "w") assert_kind_of(Integer, fd) + %w[r r+ w+ a+].each do |mode| + assert_raise(Errno::EINVAL, '[ruby-dev:38571]') {IO.new(fd, mode)} + end f = IO.new(fd, "w") f.write("FOO\n") f.close -- cgit