1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@14133 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
akr 2007-12-08 07:21:05 +00:00
parent 9667f7953e
commit b12bb50149
4 changed files with 75 additions and 12 deletions

View file

@ -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.

20
parse.y
View file

@ -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)
{

23
re.c
View file

@ -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,

View file

@ -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