ruby-changes:12403
From: nobu <ko1@a...>
Date: Tue, 14 Jul 2009 16:13:31 +0900 (JST)
Subject: [ruby-changes:12403] Ruby:r24102 (trunk): * io.c (rb_io_initialize): check if the descriptor can be accessed
nobu 2009-07-14 16:13:11 +0900 (Tue, 14 Jul 2009) New Revision: 24102 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=24102 Log: * io.c (rb_io_initialize): check if the descriptor can be accessed in the specified open mode. [ruby-dev:38571] Modified files: trunk/ChangeLog trunk/io.c trunk/test/ruby/test_io.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 24101) +++ ChangeLog (revision 24102) @@ -1,3 +1,8 @@ +Tue Jul 14 16:13:04 2009 Nobuyoshi Nakada <nobu@r...> + + * 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 <nagai@a...> * ext/tk/lib/multi-tk.rb: Long-term-callback support isn't stable yet. Index: io.c =================================================================== --- io.c (revision 24101) +++ io.c (revision 24102) @@ -6342,7 +6342,11 @@ 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_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 defined(HAVE_FCNTL) && defined(F_GETFL) + ofmode = rb_io_oflags_fmode(oflags); 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); + 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; Index: test/ruby/test_io.rb =================================================================== --- test/ruby/test_io.rb (revision 24101) +++ test/ruby/test_io.rb (revision 24102) @@ -1379,6 +1379,9 @@ 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/