From f38cc001a7ca64f7f613b5021b1714a2ee3318cb Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 6 Jan 2008 12:15:48 +0000 Subject: [PATCH] * re.c (rb_reg_initialize_str): forbid raw non ASCII character for ASCII-8BIT regexp in non ASCII-8BIT script. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14911 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ re.c | 12 ++++++++++-- test/ruby/test_m17n.rb | 2 ++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index 2576b21495..8c8fb47171 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sun Jan 6 21:14:12 2008 Tanaka Akira + + * re.c (rb_reg_initialize_str): forbid raw non ASCII character + for ASCII-8BIT regexp in non ASCII-8BIT script. + Sun Jan 6 18:19:12 2008 Tanaka Akira * include/ruby/encoding.h (rb_enc_str_buf_cat): declared. diff --git a/re.c b/re.c index 8fd80628b8..fb05e2e892 100644 --- a/re.c +++ b/re.c @@ -2037,8 +2037,16 @@ rb_reg_initialize_str(VALUE obj, VALUE str, int options, onig_errmsg_buffer err) { int ret; rb_encoding *enc = rb_enc_get(str); - if (options & ARG_ENCODING_NONE) - enc = rb_ascii8bit_encoding(); + if (options & ARG_ENCODING_NONE) { + rb_encoding *ascii8bit = rb_ascii8bit_encoding(); + if (enc != ascii8bit) { + if (rb_enc_str_coderange(str) != ENC_CODERANGE_7BIT) { + strcpy(err, "/.../n has a non escaped non ASCII character in non ASCII-8BIT script"); + return -1; + } + enc = ascii8bit; + } + } ret = rb_reg_initialize(obj, RSTRING_PTR(str), RSTRING_LEN(str), enc, options, err); RB_GC_GUARD(str); diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index 590b22f18f..b15bcfdf10 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -320,6 +320,8 @@ class TestM17N < Test::Unit::TestCase assert_regexp_fixed_ascii8bit(eval(a(%{/\xc2\xa1/n}))) assert_regexp_fixed_ascii8bit(eval(a(%q{/\xc2\xa1/}))) + assert_raise(SyntaxError) { eval("/\xa1\xa1/n".force_encoding("euc-jp")) } + [/\xc2\xa1/n, eval(a(%{/\xc2\xa1/})), eval(a(%{/\xc2\xa1/n}))].each {|r| assert_equal(nil, r =~ a("a")) assert_equal(nil, r =~ e("a"))