From aa5b65b2edf4e79f09b6e9dd488bf160b6a39260 Mon Sep 17 00:00:00 2001 From: naruse Date: Mon, 23 Jan 2012 07:56:25 +0000 Subject: [PATCH] * io.c (extract_binmode): raise an exception if binmode/textmode is specified with both vmode and opthash. [ruby-core:42199] [Bug #5918] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@34359 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ io.c | 16 ++++++++++++---- test/ruby/test_io_m17n.rb | 25 +++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index cab6f89819..bde3d8e657 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Mon Jan 23 16:42:28 2012 NARUSE, Yui + + * io.c (extract_binmode): raise an exception if binmode/textmode + is specified with both vmode and opthash. + [ruby-core:42199] [Bug #5918] + Mon Jan 23 16:35:27 2012 NARUSE, Yui * io.c (rb_io_extract_modeenc): set ASCII-8BIT if binmode is specified diff --git a/io.c b/io.c index ea550592bb..a3a45c0b12 100644 --- a/io.c +++ b/io.c @@ -4786,11 +4786,19 @@ extract_binmode(VALUE opthash, int *fmode) if (!NIL_P(opthash)) { VALUE v; v = rb_hash_aref(opthash, sym_textmode); - if (!NIL_P(v) && RTEST(v)) - *fmode |= FMODE_TEXTMODE; + if (!NIL_P(v)) { + if (*fmode & FMODE_TEXTMODE) + rb_raise(rb_eArgError, "textmode specified twice"); + if (RTEST(v)) + *fmode |= FMODE_TEXTMODE; + } v = rb_hash_aref(opthash, sym_binmode); - if (!NIL_P(v) && RTEST(v)) - *fmode |= FMODE_BINMODE; + if (!NIL_P(v)) { + if (*fmode & FMODE_BINMODE) + rb_raise(rb_eArgError, "binmode specified twice"); + if (RTEST(v)) + *fmode |= FMODE_BINMODE; + } if ((*fmode & FMODE_BINMODE) && (*fmode & FMODE_TEXTMODE)) rb_raise(rb_eArgError, "both textmode and binmode specified"); diff --git a/test/ruby/test_io_m17n.rb b/test/ruby/test_io_m17n.rb index 24a11643cd..f1a736735c 100644 --- a/test/ruby/test_io_m17n.rb +++ b/test/ruby/test_io_m17n.rb @@ -1047,6 +1047,29 @@ EOT f.set_encoding("iso-2022-jp") } } + assert_raise(ArgumentError) { + open(__FILE__, "rb", binmode: true) {|f| + f.set_encoding("iso-2022-jp") + } + } + assert_raise(ArgumentError) { + open(__FILE__, "rb", binmode: false) {|f| + f.set_encoding("iso-2022-jp") + } + } + end + + def test_textmode_twice + assert_raise(ArgumentError) { + open(__FILE__, "rt", textmode: true) {|f| + f.set_encoding("iso-2022-jp") + } + } + assert_raise(ArgumentError) { + open(__FILE__, "rt", textmode: false) {|f| + f.set_encoding("iso-2022-jp") + } + } end def test_write_conversion_fixenc @@ -1344,6 +1367,8 @@ EOT def test_both_textmode_binmode assert_raise(ArgumentError) { open("not-exist", "r", :textmode=>true, :binmode=>true) } + assert_raise(ArgumentError) { open("not-exist", "rt", :binmode=>true) } + assert_raise(ArgumentError) { open("not-exist", "rb", :textmode=>true) } end def test_textmode_decode_universal_newline_read