diff options
| author | ksss <[email protected]> | 2017-01-02 16:54:34 +0900 |
|---|---|---|
| committer | ksss <[email protected]> | 2017-01-02 17:32:44 +0900 |
| commit | 8a9a24152a23595fba0802f555d49e0c263836af (patch) | |
| tree | 3e9f7031a42b8e2a6c2726d23e7873586c2cdf19 /src/string.c | |
| parent | 30b6648b9c53f8c3f5156910758cb37eb0d654a7 (diff) | |
| download | mruby-8a9a24152a23595fba0802f555d49e0c263836af.tar.gz mruby-8a9a24152a23595fba0802f555d49e0c263836af.zip | |
Fix memory error on str_buf_cat
Modify from nofree to embed string
Diffstat (limited to 'src/string.c')
| -rw-r--r-- | src/string.c | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/string.c b/src/string.c index b57f1cec4..ae2e5ccc8 100644 --- a/src/string.c +++ b/src/string.c @@ -695,14 +695,21 @@ mrb_str_modify(mrb_state *mrb, struct RString *s) } if (RSTR_NOFREE_P(s)) { char *p = s->as.heap.ptr; + mrb_int len = s->as.heap.len; - s->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)s->as.heap.len+1); + RSTR_UNSET_NOFREE_FLAG(s); + if (len < RSTRING_EMBED_LEN_MAX) { + RSTR_SET_EMBED_FLAG(s); + RSTR_SET_EMBED_LEN(s, len); + } + else { + s->as.heap.ptr = (char *)mrb_malloc(mrb, (size_t)len+1); + s->as.heap.aux.capa = len; + } if (p) { - memcpy(RSTR_PTR(s), p, s->as.heap.len); + memcpy(RSTR_PTR(s), p, len); } - RSTR_PTR(s)[s->as.heap.len] = '\0'; - s->as.heap.aux.capa = s->as.heap.len; - RSTR_UNSET_NOFREE_FLAG(s); + RSTR_PTR(s)[len] = '\0'; return; } } |
