[前][次][番号順一覧][スレッド一覧]

ruby-changes:2642

From: ko1@a...
Date: 8 Dec 2007 16:21:36 +0900
Subject: [ruby-changes:2642] akr - Ruby:r14133 (trunk): * re.c (rb_reg_check_preprocess): new function for validating regexp

akr	2007-12-08 16:21:05 +0900 (Sat, 08 Dec 2007)

  New Revision: 14133

  Modified files:
    trunk/ChangeLog
    trunk/parse.y
    trunk/re.c
    trunk/test/ruby/test_m17n.rb

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


  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/parse.y?r1=14133&r2=14132
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/ChangeLog?r1=14133&r2=14132
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/re.c?r1=14133&r2=14132
  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_m17n.rb?r1=14133&r2=14132

Index: re.c
===================================================================
--- re.c	(revision 14132)
+++ re.c	(revision 14133)
@@ -1629,6 +1629,29 @@
     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,
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 14132)
+++ ChangeLog	(revision 14133)
@@ -1,3 +1,12 @@
+Sat Dec  8 16:18:16 2007  Tanaka Akira  <akr@f...>
+
+	* 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@f...>
 
 	* encoding.c (rb_enc_mbclen): make it never fail.
Index: parse.y
===================================================================
--- parse.y	(revision 14132)
+++ parse.y	(revision 14133)
@@ -444,6 +444,8 @@
 #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 @@
 				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 @@
 }
 
 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 @@
     }
 }
 
+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)
 {
Index: test/ruby/test_m17n.rb
===================================================================
--- test/ruby/test_m17n.rb	(revision 14132)
+++ test/ruby/test_m17n.rb	(revision 14133)
@@ -279,6 +279,22 @@
     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 @@
     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 @@
     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 @@
     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 @@
     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
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml

[前][次][番号順一覧][スレッド一覧]