summaryrefslogtreecommitdiff
path: root/internal.h
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-04-06 13:21:18 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2019-04-06 13:21:18 +0000
commitfe979e5bce4d5606d491ed25e603f5504a20c500 (patch)
treef2f389c56eeaa4b71914477ab5286f7ff565252a /internal.h
parentf15d04358827d60a708f296973c420b2c031ac5e (diff)
downloadruby-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.h8
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);