Skip to content

Commit

Permalink
Worked around memory leak bug in Ruby interpreter.
Browse files Browse the repository at this point in the history
Change-Id: I8e2b425f9008e6b82d41d59783bb8b04af1f886f
Fixes: protocolbuffers#474.
  • Loading branch information
haberman committed Jul 8, 2015
1 parent ab2094d commit 8c717ad
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion ruby/ext/google/protobuf_c/encode_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@

#include "protobuf.h"

// This function is equivalent to rb_str_cat(), but unlike the real
// rb_str_cat(), it doesn't leak memory in some versions of Ruby.
// For more information, see:
// https://bugs.ruby-lang.org/issues/11328
VALUE noleak_rb_str_cat(VALUE rb_str, const char *str, long len) {
size_t oldlen = RSTRING_LEN(rb_str);
rb_str_modify_expand(rb_str, len);
char *p = RSTRING_PTR(rb_str);
memcpy(p + oldlen, str, len);
rb_str_set_len(rb_str, oldlen + len);
}

// -----------------------------------------------------------------------------
// Parsing.
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -164,7 +176,7 @@ static size_t stringdata_handler(void* closure, const void* hd,
const char* str, size_t len,
const upb_bufhandle* handle) {
VALUE rb_str = (VALUE)closure;
rb_str_cat(rb_str, str, len);
noleak_rb_str_cat(rb_str, str, len);
return len;
}

Expand Down

0 comments on commit 8c717ad

Please sign in to comment.