Project

General

Profile

Actions

Bug #11319

closed

"w+" is interpreted as "r+" when reopening stdio streams

Added by cremno (cremno phobia) over 8 years ago. Updated over 8 years ago.

Status:
Closed
Target version:
-
[ruby-core:69779]

Description

diff --git a/io.c b/io.c
index dfdb422..8358c74 100644
--- a/io.c
+++ b/io.c
@@ -5046,6 +5046,9 @@ rb_io_oflags_modestr(int oflags)
       case O_WRONLY:
 	return MODE_BINARY("w", "wb");
       case O_RDWR:
+	if (oflags & O_TRUNC) {
+	    return MODE_BINARY("w+", "wb+");
+	}
 	return MODE_BINARY("r+", "rb+");
     }
 }
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb
index 9b3e801..1662ac0 100644
--- a/test/ruby/test_io.rb
+++ b/test/ruby/test_io.rb
@@ -2069,6 +2069,15 @@ End
     }
   end
 
+  def test_reopen_stdio
+    mkcdtmpdir {
+      fname = 'bug11319'
+      File.write(fname, 'hello')
+      system(EnvUtil.rubybin, '-e', "STDOUT.reopen('#{fname}', 'w+')")
+      assert_equal('', File.read(fname))
+    }
+  end
+
   def test_reopen_mode
     feature7067 = '[ruby-core:47694]'
     make_tempfile {|t|

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0