summaryrefslogtreecommitdiff
path: root/io.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-07-14 07:13:11 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-07-14 07:13:11 +0000
commit9681409e3691037c11c19052295616454f74c936 (patch)
treeebb308e156ad0b3b74c4f65a9ff54d7c0b1da15c /io.c
parented586352292f331f2166205c0d7b99b69eab4ba4 (diff)
* io.c (rb_io_initialize): check if the descriptor can be accessed
in the specified open mode. [ruby-dev:38571] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@24102 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'io.c')
-rw-r--r--io.c22
1 files changed, 17 insertions, 5 deletions
diff --git a/io.c b/io.c
index 45c4a73215..4e576e710e 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;