diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-04-06 13:21:18 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2019-04-06 13:21:18 +0000 |
commit | fe979e5bce4d5606d491ed25e603f5504a20c500 (patch) | |
tree | f2f389c56eeaa4b71914477ab5286f7ff565252a /internal.h | |
parent | f15d04358827d60a708f296973c420b2c031ac5e (diff) | |
download | ruby-fe979e5bce4d5606d491ed25e603f5504a20c500.tar.gz |
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
Diffstat (limited to 'internal.h')
-rw-r--r-- | internal.h | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/internal.h b/internal.h index f2c9c238eb..a9b4cfb4d5 100644 --- a/internal.h +++ b/internal.h @@ -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); |