diff options
author | Lourens Naudé <[email protected]> | 2019-11-26 13:07:49 +0000 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2020-12-10 18:15:26 +0900 |
commit | 9a17437558e42aa1da372b515ba8bc18067d578c (patch) | |
tree | 3feb8a32ef1a7b46f4e3e24ecbabedc2524ac905 | |
parent | e7433a3288b48f90447bc8ebb1531f57be17392c (diff) |
Right size literal regular expression buffers on compile
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/2696
-rw-r--r-- | regcomp.c | 20 |
1 files changed, 20 insertions, 0 deletions
@@ -138,6 +138,25 @@ bitset_on_num(BitSetRef bs) } #endif +// Attempt to right size allocated buffers for a regex post compile +static void +onig_reg_resize(regex_t *reg) +{ + resize: + if (reg->alloc > reg->used) { + unsigned char *new_ptr = xrealloc(reg->p, reg->used); + // Skip the right size optimization if memory allocation fails + if (new_ptr) { + reg->alloc = reg->used; + reg->p = new_ptr; + } + } + if (reg->chain) { + reg = reg->chain; + goto resize; + } +} + extern int onig_bbuf_init(BBuf* buf, OnigDistance size) { @@ -5886,6 +5905,7 @@ onig_compile(regex_t* reg, const UChar* pattern, const UChar* pattern_end, #endif end: + onig_reg_resize(reg); return r; err_unset: |