From: cremno@... Date: 2015-06-29T22:55:44+00:00 Subject: [ruby-core:69779] [Ruby trunk - Bug #11319] [Open] "w+" is interpreted as "r+" when reopening stdio streams Issue #11319 has been reported by cremno phobia. ---------------------------------------- Bug #11319: "w+" is interpreted as "r+" when reopening stdio streams https://bugs.ruby-lang.org/issues/11319 * Author: cremno phobia * Status: Open * Priority: Normal * Assignee: * ruby -v: * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN ---------------------------------------- ~~~diff 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| ~~~ -- https://bugs.ruby-lang.org/