Skip to content

Commit 6020ec8

Browse files
committed
parser.rl: allocate structs with wrapper
* ext/json/ext/parser/parser.rl (cJSON_parser_s_allocate): allocate structs with making new wrapper objects and get rid of potential memory leak. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50660 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
1 parent 638a1f7 commit 6020ec8

File tree

3 files changed

+10
-23
lines changed

3 files changed

+10
-23
lines changed

ext/json/ext/parser/parser.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,14 +2091,6 @@ static VALUE cParser_parse(VALUE self)
20912091
}
20922092
}
20932093

2094-
2095-
static JSON_Parser *JSON_allocate(void)
2096-
{
2097-
JSON_Parser *json = ZALLOC(JSON_Parser);
2098-
json->fbuffer = fbuffer_alloc(0);
2099-
return json;
2100-
}
2101-
21022094
static void JSON_mark(void *ptr)
21032095
{
21042096
JSON_Parser *json = ptr;
@@ -2135,8 +2127,10 @@ static const rb_data_type_t JSON_Parser_type = {
21352127

21362128
static VALUE cJSON_parser_s_allocate(VALUE klass)
21372129
{
2138-
JSON_Parser *json = JSON_allocate();
2139-
return TypedData_Wrap_Struct(klass, &JSON_Parser_type, json);
2130+
JSON_Parser *json;
2131+
VALUE obj = TypedData_Make_Struct(klass, JSON_Parser, &JSON_Parser_type, json);
2132+
json->fbuffer = fbuffer_alloc(0);
2133+
return obj;
21402134
}
21412135

21422136
/*

ext/json/ext/parser/parser.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ static char *JSON_parse_string(JSON_Parser *json, char *p, char *pe, VALUE *resu
6868
static VALUE convert_encoding(VALUE source);
6969
static VALUE cParser_initialize(int argc, VALUE *argv, VALUE self);
7070
static VALUE cParser_parse(VALUE self);
71-
static JSON_Parser *JSON_allocate(void);
7271
static void JSON_mark(void *json);
7372
static void JSON_free(void *json);
7473
static VALUE cJSON_parser_s_allocate(VALUE klass);
@@ -82,11 +81,11 @@ static inline void *ruby_zalloc(size_t n)
8281
return p;
8382
}
8483
#endif
85-
#ifdef TypedData_Wrap_Struct
84+
#ifdef TypedData_Make_Struct
8685
static const rb_data_type_t JSON_Parser_type;
8786
#define NEW_TYPEDDATA_WRAPPER 1
8887
#else
89-
#define TypedData_Wrap_Struct(klass, ignore, json) Data_Wrap_Struct(klass, JSON_mark, JSON_free, json)
88+
#define TypedData_Make_Struct(klass, type, ignore, json) Data_Make_Struct(klass, type, NULL, JSON_free, json)
9089
#define TypedData_Get_Struct(self, JSON_Parser, ignore, json) Data_Get_Struct(self, JSON_Parser, json)
9190
#endif
9291

ext/json/ext/parser/parser.rl

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -814,14 +814,6 @@ static VALUE cParser_parse(VALUE self)
814814
}
815815
}
816816

817-
818-
static JSON_Parser *JSON_allocate(void)
819-
{
820-
JSON_Parser *json = ZALLOC(JSON_Parser);
821-
json->fbuffer = fbuffer_alloc(0);
822-
return json;
823-
}
824-
825817
static void JSON_mark(void *ptr)
826818
{
827819
JSON_Parser *json = ptr;
@@ -858,8 +850,10 @@ static const rb_data_type_t JSON_Parser_type = {
858850

859851
static VALUE cJSON_parser_s_allocate(VALUE klass)
860852
{
861-
JSON_Parser *json = JSON_allocate();
862-
return TypedData_Wrap_Struct(klass, &JSON_Parser_type, json);
853+
JSON_Parser *json;
854+
VALUE obj = TypedData_Make_Struct(klass, JSON_Parser, &JSON_Parser_type, json);
855+
json->fbuffer = fbuffer_alloc(0);
856+
return obj;
863857
}
864858

865859
/*

0 commit comments

Comments
 (0)