diff options
-rw-r--r-- | ChangeLog | 9 | ||||
-rw-r--r-- | parse.y | 20 | ||||
-rw-r--r-- | re.c | 23 | ||||
-rw-r--r-- | test/ruby/test_m17n.rb | 35 |
4 files changed, 75 insertions, 12 deletions
@@ -1,3 +1,12 @@ +Sat Dec 8 16:18:16 2007 Tanaka Akira <akr@fsij.org> + + * re.c (rb_reg_check_preprocess): new function for validating regexp + fragment. + + * parse.y (regexp): invoke reg_fragment_check. + (reg_fragment_check): defined. + (reg_fragment_check_gen): defined. + Sat Dec 8 11:06:29 2007 Tanaka Akira <akr@fsij.org> * encoding.c (rb_enc_mbclen): make it never fail. @@ -444,6 +444,8 @@ static VALUE reg_compile_gen(struct parser_params*, VALUE, int); #define reg_compile(str,options) reg_compile_gen(parser, str, options) static void reg_fragment_setenc_gen(struct parser_params*, VALUE, int); #define reg_fragment_setenc(str,options) reg_fragment_setenc_gen(parser, str, options) +static void reg_fragment_check_gen(struct parser_params*, VALUE, int); +#define reg_fragment_check(str,options) reg_fragment_check_gen(parser, str, options) #else #define remove_begin(node) (node) #endif /* !RIPPER */ @@ -3661,10 +3663,10 @@ regexp : tREGEXP_BEG xstring_contents tREGEXP_END nd_set_type(node, NODE_DREGX); } node->nd_cflag = options & RE_OPTION_MASK; - reg_fragment_setenc(node->nd_lit, options); + reg_fragment_check(node->nd_lit, options); for (list = node->nd_next; list; list = list->nd_next) { if (nd_type(list->nd_head) == NODE_STR) { - reg_fragment_setenc(list->nd_head->nd_lit, options); + reg_fragment_check(list->nd_head->nd_lit, options); } } break; @@ -8456,6 +8458,7 @@ dvar_curr_gen(struct parser_params *parser, ID id) } VALUE rb_reg_compile(VALUE str, int options); +VALUE rb_reg_check_preprocess(VALUE); static void reg_fragment_setenc_gen(struct parser_params* parser, VALUE str, int options) @@ -8475,6 +8478,19 @@ reg_fragment_setenc_gen(struct parser_params* parser, VALUE str, int options) } } +static void +reg_fragment_check_gen(struct parser_params* parser, VALUE str, int options) +{ + VALUE err; + reg_fragment_setenc_gen(parser, str, options); + err = rb_reg_check_preprocess(str); + if (err != Qnil) { + err = rb_obj_as_string(err); + compile_error(PARSER_ARG "%s", RSTRING_PTR(err)); + RB_GC_GUARD(err); + } +} + static VALUE reg_compile_gen(struct parser_params* parser, VALUE str, int options) { @@ -1629,6 +1629,29 @@ rb_reg_preprocess(const char *p, const char *end, rb_encoding *enc, return buf; } +VALUE +rb_reg_check_preprocess(VALUE str) +{ + rb_encoding *fixed_enc = 0; + onig_errmsg_buffer err; + VALUE buf; + char *p, *end; + rb_encoding *enc; + + StringValue(str); + p = RSTRING_PTR(str); + end = p + RSTRING_LEN(str); + enc = rb_enc_get(str); + + buf = rb_reg_preprocess(p, end, enc, &fixed_enc, err); + RB_GC_GUARD(str); + + if (buf == Qnil) { + return rb_reg_error_desc(str, 0, err); + } + return Qnil; +} + #if 0 static VALUE rb_reg_preprocess_obj(VALUE str, diff --git a/test/ruby/test_m17n.rb b/test/ruby/test_m17n.rb index ff009d3a01..f923173e5a 100644 --- a/test/ruby/test_m17n.rb +++ b/test/ruby/test_m17n.rb @@ -279,6 +279,22 @@ class TestM17N < Test::Unit::TestCase assert_regexp_fixed_sjis(eval(s(%q{/\xc2\xa1/}))) end + def test_regexp_embed + r = eval(e("/\xc2\xa1/")) + assert_raise(ArgumentError) { eval(s("/\xc2\xa1\#{r}/s")) } + assert_raise(ArgumentError) { eval(s("/\#{r}\xc2\xa1/s")) } + + r = /\xc2\xa1/e + #assert_raise(ArgumentError) { eval(s("/\xc2\xa1\#{r}/s")) } + #assert_raise(ArgumentError) { eval(s("/\#{r}\xc2\xa1/s")) } + + r = eval(e("/\xc2\xa1/")) + #assert_raise(ArgumentError) { /\xc2\xa1#{r}/s } + + r = /\xc2\xa1/e + #assert_raise(ArgumentError) { /\xc2\xa1#{r}/s } + end + def test_begin_end_offset str = e("\244\242\244\244\244\246\244\250\244\252a") assert(/(a)/ =~ str) @@ -397,7 +413,6 @@ class TestM17N < Test::Unit::TestCase assert_regexp_fixed_ascii8bit(/#{}/n) assert_regexp_fixed_ascii8bit(/#{}\xc2\xa1/n) assert_regexp_fixed_ascii8bit(/\xc2\xa1#{}/n) - #assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/s') } #assert_raise(SyntaxError) { s1, s2 = s('\xc2'), s('\xa1'); /#{s1}#{s2}/ } end @@ -405,9 +420,9 @@ class TestM17N < Test::Unit::TestCase assert_regexp_fixed_eucjp(/#{}/e) assert_regexp_fixed_eucjp(/#{}\xc2\xa1/e) assert_regexp_fixed_eucjp(/\xc2\xa1#{}/e) - assert_raise(RegexpError) { eval('/\xc2#{}/e') } - assert_raise(RegexpError) { eval('/#{}\xc2/e') } - #assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/e') } + assert_raise(SyntaxError) { eval('/\xc2#{}/e') } + assert_raise(SyntaxError) { eval('/#{}\xc2/e') } + assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/e') } #assert_raise(SyntaxError) { s1, s2 = e('\xc2'), e('\xa1'); /#{s1}#{s2}/ } end @@ -415,9 +430,9 @@ class TestM17N < Test::Unit::TestCase assert_regexp_fixed_sjis(/#{}/s) assert_regexp_fixed_sjis(/#{}\xc2\xa1/s) assert_regexp_fixed_sjis(/\xc2\xa1#{}/s) - assert_raise(RegexpError) { eval('/\x81#{}/s') } - assert_raise(RegexpError) { eval('/#{}\x81/s') } - #assert_raise(SyntaxError) { eval('/\x81#{}\xa1/s') } + assert_raise(SyntaxError) { eval('/\x81#{}/s') } + assert_raise(SyntaxError) { eval('/#{}\x81/s') } + assert_raise(SyntaxError) { eval('/\x81#{}\xa1/s') } #assert_raise(SyntaxError) { s1, s2 = s('\x81'), s('\xa1'); /#{s1}#{s2}/ } end @@ -425,9 +440,9 @@ class TestM17N < Test::Unit::TestCase assert_regexp_fixed_utf8(/#{}/u) assert_regexp_fixed_utf8(/#{}\xc2\xa1/u) assert_regexp_fixed_utf8(/\xc2\xa1#{}/u) - assert_raise(RegexpError) { eval('/\xc2#{}/u') } - assert_raise(RegexpError) { eval('/#{}\xc2/u') } - #assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/u') } + assert_raise(SyntaxError) { eval('/\xc2#{}/u') } + assert_raise(SyntaxError) { eval('/#{}\xc2/u') } + assert_raise(SyntaxError) { eval('/\xc2#{}\xa1/u') } #assert_raise(SyntaxError) { s1, s2 = u('\xc2'), u('\xa1'); /#{s1}#{s2}/ } end |