diff options
-rw-r--r-- | string.c | 21 |
1 files changed, 14 insertions, 7 deletions
@@ -2167,23 +2167,29 @@ rb_str_resize(VALUE str, long len) static VALUE str_buf_cat(VALUE str, const char *ptr, long len) { - long capa, total, off = -1; + long capa, total, olen, off = -1; + char *sptr; - if (ptr >= RSTRING_PTR(str) && ptr <= RSTRING_END(str)) { - off = ptr - RSTRING_PTR(str); + RSTRING_GETMEM(str, sptr, olen); + if (ptr >= sptr && ptr <= sptr + olen) { + off = ptr - sptr; } rb_str_modify(str); if (len == 0) return 0; if (STR_EMBED_P(str)) { capa = RSTRING_EMBED_LEN_MAX; + sptr = RSTRING(str)->as.ary; + olen = RSTRING_EMBED_LEN(str); } else { capa = RSTRING(str)->as.heap.aux.capa; + sptr = RSTRING(str)->as.heap.ptr; + olen = RSTRING(str)->as.heap.len; } - if (RSTRING_LEN(str) >= LONG_MAX - len) { + if (olen >= LONG_MAX - len) { rb_raise(rb_eArgError, "string sizes too big"); } - total = RSTRING_LEN(str)+len; + total = olen + len; if (capa <= total) { while (total > capa) { if (capa > LONG_MAX / 2) { @@ -2193,11 +2199,12 @@ str_buf_cat(VALUE str, const char *ptr, long len) capa = 2 * capa; } RESIZE_CAPA(str, capa); + sptr = RSTRING_PTR(str); } if (off != -1) { - ptr = RSTRING_PTR(str) + off; + ptr = sptr + off; } - memcpy(RSTRING_PTR(str) + RSTRING_LEN(str), ptr, len); + memcpy(sptr + olen, ptr, len); STR_SET_LEN(str, total); RSTRING_PTR(str)[total] = '\0'; /* sentinel */ |