summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog5
-rw-r--r--bootstraptest/test_io.rb17
-rw-r--r--io.c13
3 files changed, 26 insertions, 9 deletions
diff --git a/ChangeLog b/ChangeLog
index c6e57f3791e..a0331e5c22e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+Thu Feb 14 21:00:14 2008 Tanaka Akira <akr@fsij.org>
+
+ * io.c (io_reopen): check STDIN, STDOUT and STDERR mode according to
+ stdio streams.
+
Thu Feb 14 16:07:40 2008 Yukihiro Matsumoto <matz@ruby-lang.org>
* test/ruby/test_math.rb: actual-expected argument ordering for
diff --git a/bootstraptest/test_io.rb b/bootstraptest/test_io.rb
index a5d54afc5b5..4974a1eeb92 100644
--- a/bootstraptest/test_io.rb
+++ b/bootstraptest/test_io.rb
@@ -52,3 +52,20 @@ assert_equal 'ok', %q{
File.unlink(tmpname)
:ok
}
+
+assert_equal 'ok', %q{
+ require 'tmpdir'
+ begin
+ tmpname = "#{Dir.tmpdir}/ruby-btest-#{$$}-#{rand(0x100000000).to_s(36)}"
+ rw = File.open(tmpname, File::RDWR|File::CREAT|File::EXCL)
+ rescue Errno::EEXIST
+ retry
+ end
+ save = STDIN.dup
+ STDIN.reopen(rw)
+ STDIN.print "a"
+ STDIN.reopen(save)
+ rw.close
+ File.unlink(tmpname)
+ :ok
+}
diff --git a/io.c b/io.c
index 44d9b3728e8..79995c7dd18 100644
--- a/io.c
+++ b/io.c
@@ -4112,8 +4112,9 @@ io_reopen(VALUE io, VALUE nfile)
if (fptr == orig) return io;
#if !defined __CYGWIN__
if (IS_PREP_STDIO(fptr)) {
- if (((fptr->mode & FMODE_READWRITE) & (orig->mode & FMODE_READWRITE)) !=
- (fptr->mode & FMODE_READWRITE)) {
+ if ((fptr->stdio_file == stdin && !(orig->mode & FMODE_READABLE)) ||
+ (fptr->stdio_file == stdout && !(orig->mode & FMODE_WRITABLE)) ||
+ (fptr->stdio_file == stderr && !(orig->mode & FMODE_WRITABLE))) {
rb_raise(rb_eArgError,
"%s can't change access mode from \"%s\" to \"%s\"",
PREP_STDIO_NAME(fptr), rb_io_flags_mode(fptr->mode),
@@ -4132,13 +4133,7 @@ io_reopen(VALUE io, VALUE nfile)
}
/* copy rb_io_t structure */
- if (fptr->mode & FMODE_PREP) {
- int mask = FMODE_PREP|FMODE_READWRITE;
- fptr->mode = (orig->mode & ~mask)|(fptr->mode & mask);
- }
- else {
- fptr->mode = orig->mode;
- }
+ fptr->mode = orig->mode | (fptr->mode & FMODE_PREP);
fptr->pid = orig->pid;
fptr->lineno = orig->lineno;
if (fptr->path) free(fptr->path);