internal.h: fix potential memory leak

* internal.h (rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString):
  create tmpbuf to keep the pointer before xmalloc which can raise
  a NoMemoryError exception.  extracted from
  https://github.com/bear-metal/ruby/tree/transient-imemo-tmpbuf

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67459 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2019-04-06 13:21:18 +00:00
Родитель f15d043588
Коммит fe979e5bce
1 изменённых файлов: 7 добавлений и 1 удалений

Просмотреть файл

@ -1132,15 +1132,21 @@ static inline VALUE
rb_imemo_tmpbuf_auto_free_pointer_new_from_an_RString(VALUE str)
{
const void *src;
VALUE imemo;
rb_imemo_tmpbuf_t *tmpbuf;
void *dst;
size_t len;
SafeStringValue(str);
/* create tmpbuf to keep the pointer before xmalloc */
imemo = rb_imemo_tmpbuf_auto_free_pointer(NULL);
tmpbuf = (rb_imemo_tmpbuf_t *)imemo;
len = RSTRING_LEN(str);
src = RSTRING_PTR(str);
dst = ruby_xmalloc(len);
memcpy(dst, src, len);
return rb_imemo_tmpbuf_auto_free_pointer(dst);
tmpbuf->ptr = dst;
return imemo;
}
void rb_strterm_mark(VALUE obj);