From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-3.5 required=3.0 tests=ALL_TRUSTED,BAYES_00, RP_MATCHES_RCVD shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: spew@80x24.org Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 9CB26203AA for ; Mon, 29 Jun 2015 23:20:59 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] io.c: reopen stdio streams correctly when given "w+" Date: Mon, 29 Jun 2015 23:20:59 +0000 Message-Id: <1435620059-15623-1-git-send-email-e@80x24.org> List-Id: * io.c (rb_io_oflags_modestr): handle O_TRUNC correctly * test/ruby/test_io.rb (test_reopen_stdio): new test Patch-by: cremno phobia [ruby-core:69779] [Bug #11319] --- io.c | 3 +++ test/ruby/test_io.rb | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/io.c b/io.c index 8844987..6c62c97 100644 --- a/io.c +++ b/io.c @@ -5033,6 +5033,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..9ded25b 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| -- EW