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

ruby-changes:36591

From: nobu <ko1@a...>
Date: Tue, 2 Dec 2014 06:31:19 +0900 (JST)
Subject: [ruby-changes:36591] nobu:r48672 (trunk): re.c: rb_reg_region_copy

nobu	2014-12-02 06:30:58 +0900 (Tue, 02 Dec 2014)

  New Revision: 48672

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=48672

  Log:
    re.c: rb_reg_region_copy
    
    * re.c (rb_reg_region_copy): new function to try with GC if copy
      failed and return the error.

  Modified files:
    trunk/ChangeLog
    trunk/ext/strscan/strscan.c
    trunk/include/ruby/re.h
    trunk/re.c
Index: include/ruby/re.h
===================================================================
--- include/ruby/re.h	(revision 48671)
+++ include/ruby/re.h	(revision 48672)
@@ -58,6 +58,7 @@ long rb_reg_adjust_startpos(VALUE, VALUE https://github.com/ruby/ruby/blob/trunk/include/ruby/re.h#L58
 void rb_match_busy(VALUE);
 VALUE rb_reg_quote(VALUE);
 regex_t *rb_reg_prepare_re(VALUE re, VALUE str);
+int rb_reg_region_copy(struct re_registers *, const struct re_registers *);
 
 RUBY_SYMBOL_EXPORT_END
 
Index: re.c
===================================================================
--- re.c	(revision 48671)
+++ re.c	(revision 48672)
@@ -20,8 +20,6 @@ VALUE rb_eRegexpError; https://github.com/ruby/ruby/blob/trunk/re.c#L20
 typedef char onig_errmsg_buffer[ONIG_MAX_ERROR_MESSAGE_LEN];
 #define errcpy(err, msg) strlcpy((err), (msg), ONIG_MAX_ERROR_MESSAGE_LEN)
 
-#define CHECK_REGION_COPIED(regs) \
-    do {if (!(regs)->allocated) rb_memerror();} while (0)
 #define BEG(no) (regs->beg[(no)])
 #define END(no) (regs->end[(no)])
 
@@ -877,6 +875,17 @@ match_alloc(VALUE klass) https://github.com/ruby/ruby/blob/trunk/re.c#L875
     return (VALUE)match;
 }
 
+int
+rb_reg_region_copy(struct re_registers *to, const struct re_registers *from)
+{
+    onig_region_copy(to, (OnigRegion *)from);
+    if (to->allocated) return 0;
+    rb_gc();
+    onig_region_copy(to, (OnigRegion *)from);
+    if (to->allocated) return 0;
+    return ONIGERR_MEMORY;
+}
+
 typedef struct {
     long byte_pos;
     long char_pos;
@@ -984,8 +993,8 @@ match_init_copy(VALUE obj, VALUE orig) https://github.com/ruby/ruby/blob/trunk/re.c#L993
     RMATCH(obj)->regexp = RMATCH(orig)->regexp;
 
     rm = RMATCH(obj)->rmatch;
-    onig_region_copy(&rm->regs, RMATCH_REGS(orig));
-    CHECK_REGION_COPIED(&rm->regs);
+    if (rb_reg_region_copy(&rm->regs, RMATCH_REGS(orig)))
+	rb_memerror();
 
     if (!RMATCH(orig)->rmatch->char_offset_updated) {
         rm->char_offset_updated = 0;
@@ -998,6 +1007,7 @@ match_init_copy(VALUE obj, VALUE orig) https://github.com/ruby/ruby/blob/trunk/re.c#L1007
         MEMCPY(rm->char_offset, RMATCH(orig)->rmatch->char_offset,
                struct rmatch_offset, rm->regs.num_regs);
         rm->char_offset_updated = 1;
+	RB_GC_GUARD(orig);
     }
 
     return obj;
@@ -1472,10 +1482,11 @@ rb_reg_search0(VALUE re, VALUE str, long https://github.com/ruby/ruby/blob/trunk/re.c#L1482
     }
 
     if (NIL_P(match)) {
+	int err;
 	match = match_alloc(rb_cMatch);
-	onig_region_copy(RMATCH_REGS(match), regs);
+	err = rb_reg_region_copy(RMATCH_REGS(match), regs);
 	onig_region_free(regs, 0);
-	CHECK_REGION_COPIED(RMATCH_REGS(match));
+	if (err) rb_memerror();
     }
     else {
 	if (rb_safe_level() >= 3)
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 48671)
+++ ChangeLog	(revision 48672)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Tue Dec  2 06:30:55 2014  Nobuyoshi Nakada  <nobu@r...>
+
+	* re.c (rb_reg_region_copy): new function to try with GC if copy
+	  failed and return the error.
+
 Tue Dec  2 04:43:08 2014  Nobuyoshi Nakada  <nobu@r...>
 
 	* re.c (CHECK_REGION_COPIED): onig_region_copy() can fail when
Index: ext/strscan/strscan.c
===================================================================
--- ext/strscan/strscan.c	(revision 48671)
+++ ext/strscan/strscan.c	(revision 48672)
@@ -251,8 +251,9 @@ strscan_init_copy(VALUE vself, VALUE vor https://github.com/ruby/ruby/blob/trunk/ext/strscan/strscan.c#L251
 	self->str = orig->str;
 	self->prev = orig->prev;
 	self->curr = orig->curr;
-	onig_region_copy(&self->regs, &orig->regs);
-	if (!self->regs.allocated) rb_memerror();
+	if (rb_reg_region_copy(&self->regs, &orig->regs))
+	    rb_memerror();
+	RB_GC_GUARD(vorig);
     }
 
     return vself;

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

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