diff --git a/ChangeLog b/ChangeLog index 43642d7252..fd8b2b7218 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Fri Mar 23 13:19:20 2012 Nobuyoshi Nakada + + * bignum.c (rb_str_to_inum): must be ASCII compatible encoding as + well as String#hex and String#oct. [ruby-core:43566][Bug #6192] + + * string.c (rb_must_asciicompat): check if ASCII compatible. + Thu Mar 22 23:14:36 2012 Nobuyoshi Nakada * transcode.c (str_encode_bang, encoded_dup): if nothing was diff --git a/bignum.c b/bignum.c index fae29652b6..678a65bba7 100644 --- a/bignum.c +++ b/bignum.c @@ -775,6 +775,7 @@ rb_str_to_inum(VALUE str, int base, int badcheck) VALUE ret; StringValue(str); + rb_must_asciicompat(str); if (badcheck) { s = StringValueCStr(str); } diff --git a/include/ruby/intern.h b/include/ruby/intern.h index e07f7fd781..61ed85fcdb 100644 --- a/include/ruby/intern.h +++ b/include/ruby/intern.h @@ -707,6 +707,7 @@ VALUE rb_str_buf_cat2(VALUE, const char*); VALUE rb_str_buf_cat_ascii(VALUE, const char*); VALUE rb_obj_as_string(VALUE); VALUE rb_check_string_type(VALUE); +void rb_must_asciicompat(VALUE); VALUE rb_str_dup(VALUE); VALUE rb_str_resurrect(VALUE str); VALUE rb_str_locktmp(VALUE); diff --git a/string.c b/string.c index d06a99636e..7fd2d94e59 100644 --- a/string.c +++ b/string.c @@ -1409,6 +1409,15 @@ rb_str_associated(VALUE str) return Qfalse; } +void +rb_must_asciicompat(VALUE str) +{ + rb_encoding *enc = rb_enc_get(str); + if (!rb_enc_asciicompat(enc)) { + rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc)); + } +} + VALUE rb_string_value(volatile VALUE *ptr) { @@ -6722,11 +6731,6 @@ rb_str_scan(VALUE str, VALUE pat) static VALUE rb_str_hex(VALUE str) { - rb_encoding *enc = rb_enc_get(str); - - if (!rb_enc_asciicompat(enc)) { - rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc)); - } return rb_str_to_inum(str, 16, FALSE); } @@ -6748,11 +6752,6 @@ rb_str_hex(VALUE str) static VALUE rb_str_oct(VALUE str) { - rb_encoding *enc = rb_enc_get(str); - - if (!rb_enc_asciicompat(enc)) { - rb_raise(rb_eEncCompatError, "ASCII incompatible encoding: %s", rb_enc_name(enc)); - } return rb_str_to_inum(str, -8, FALSE); } diff --git a/test/ruby/test_integer.rb b/test/ruby/test_integer.rb index 7f8212ebf0..3f4add7f15 100644 --- a/test/ruby/test_integer.rb +++ b/test/ruby/test_integer.rb @@ -90,6 +90,13 @@ class TestInteger < Test::Unit::TestCase assert_equal(2 ** 50, Integer(2.0 ** 50)) assert_raise(TypeError) { Integer(nil) } + + bug6192 = '[ruby-core:43566]' + assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-16be"))} + assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-16le"))} + assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-32be"))} + assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("utf-32le"))} + assert_raise(Encoding::CompatibilityError, bug6192) {Integer("0".encode("iso-2022-jp"))} end def test_int_p diff --git a/test/ruby/test_string.rb b/test/ruby/test_string.rb index 622ba9ea63..f8336679b5 100644 --- a/test/ruby/test_string.rb +++ b/test/ruby/test_string.rb @@ -1465,6 +1465,13 @@ class TestString < Test::Unit::TestCase assert_equal(0x4000000000000000, "4611686018427387904".to_i(10)) assert_equal(1, "1__2".to_i(10)) assert_equal(1, "1_z".to_i(10)) + + bug6192 = '[ruby-core:43566]' + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-16be").to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-16le").to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-32be").to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("utf-32le").to_i} + assert_raise(Encoding::CompatibilityError, bug6192) {"0".encode("iso-2022-jp").to_i} end def test_to_s