Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fix memory leaks in JSON.parse

Test Script:
{{{
require 'json'

ary = (0 .. 100).map{|x| x}
str = '[' + ary.join(',') + ']'

loop do
  JSON.parse(str)
end
}}}
  • Loading branch information...
commit c2992da152d08501ebe531c42e3dcdf7bc68fdf3 1 parent d178850
@Watson1978 Watson1978 authored
View
1  ext/json/parser/extconf.rb
@@ -13,4 +13,5 @@
have_header("re.h")
have_header("ruby/st.h")
+$INCFLAGS << ' -I../../..'
create_makefile 'json/ext/parser'
View
22 ext/json/parser/parser.c
@@ -1921,6 +1921,23 @@ static VALUE cParser_source(VALUE self)
return rb_str_dup(json->Vsource);
}
+#ifdef __MACRUBY__
+#include "macruby_internal.h"
+#include "objc.h"
+static IMP json_parser_finalize_imp_super = NULL;
+static void
+json_parser_finalize_imp(void *self, SEL sel)
+{
+ GET_PARSER;
+ if (json != NULL) {
+ JSON_free(json);
+ }
+ if (json_parser_finalize_imp_super != NULL) {
+ ((void(*)(void *, SEL))json_parser_finalize_imp_super)(self, sel);
+ }
+}
+#endif
+
void Init_parser()
{
rb_require("json/common");
@@ -1966,4 +1983,9 @@ void Init_parser()
#else
i_iconv = rb_intern("iconv");
#endif
+
+#ifdef __MACRUBY__
+ json_parser_finalize_imp_super = rb_objc_install_method2((Class)cParser,
+ "finalize", (IMP)json_parser_finalize_imp);
+#endif
}
View
2  ext/json/parser/parser.h
@@ -3,7 +3,7 @@
#include "ruby.h"
-#if WITH_OBJC
+#ifdef __MACRUBY__
/* We cannot use the GC memory functions here because the underlying libedit
* function will call free() on the memory, resulting in a leak.
*/
View
22 ext/json/parser/parser.rl
@@ -778,6 +778,23 @@ static VALUE cParser_source(VALUE self)
return rb_str_dup(json->Vsource);
}
+#ifdef __MACRUBY__
+#include "macruby_internal.h"
+#include "objc.h"
+static IMP json_parser_finalize_imp_super = NULL;
+static void
+json_parser_finalize_imp(void *self, SEL sel)
+{
+ GET_PARSER;
+ if (json != NULL) {
+ JSON_free(json);
+ }
+ if (json_parser_finalize_imp_super != NULL) {
+ ((void(*)(void *, SEL))json_parser_finalize_imp_super)(self, sel);
+ }
+}
+#endif
+
void Init_parser()
{
rb_require("json/common");
@@ -823,4 +840,9 @@ void Init_parser()
#else
i_iconv = rb_intern("iconv");
#endif
+
+#ifdef __MACRUBY__
+ json_parser_finalize_imp_super = rb_objc_install_method2((Class)cParser,
+ "finalize", (IMP)json_parser_finalize_imp);
+#endif
}
Please sign in to comment.
Something went wrong with that request. Please try again.