diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-12-01 21:30:58 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-12-01 21:30:58 +0000 |
commit | 012c558127d329d2f846124958f7ab57bbaedc3b (patch) | |
tree | 7c58ff528c9b08835e3dcfbbb47b6dd19a7ec9fd /re.c | |
parent | 4ea50d7aea9e84be4b2ea206c939bffa903bd509 (diff) |
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.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48672 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 're.c')
-rw-r--r-- | re.c | 23 |
1 files changed, 17 insertions, 6 deletions
@@ -20,8 +20,6 @@ VALUE rb_eRegexpError; 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) 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) 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) 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 pos, int reverse, int set_backref_str) } 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) |