Skip to content

Commit d4c99de

Browse files
committed
Merge pull request #247 from nobu/potential_memory_leak
Potential memory leak
2 parents 638a1f7 + fae344f commit d4c99de

File tree

5 files changed

+15
-34
lines changed

5 files changed

+15
-34
lines changed

ext/json/ext/generator/generator.c

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -526,16 +526,11 @@ static const rb_data_type_t JSON_Generator_State_type = {
526526
};
527527
#endif
528528

529-
static JSON_Generator_State *State_allocate(void)
530-
{
531-
JSON_Generator_State *state = ZALLOC(JSON_Generator_State);
532-
return state;
533-
}
534-
535529
static VALUE cState_s_allocate(VALUE klass)
536530
{
537-
JSON_Generator_State *state = State_allocate();
538-
return TypedData_Wrap_Struct(klass, &JSON_Generator_State_type, state);
531+
JSON_Generator_State *state;
532+
return TypedData_Make_Struct(klass, JSON_Generator_State,
533+
&JSON_Generator_State_type, state);
539534
}
540535

541536
/*

ext/json/ext/generator/generator.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ static VALUE mFalseClass_to_json(int argc, VALUE *argv, VALUE self);
112112
static VALUE mNilClass_to_json(int argc, VALUE *argv, VALUE self);
113113
static VALUE mObject_to_json(int argc, VALUE *argv, VALUE self);
114114
static void State_free(void *state);
115-
static JSON_Generator_State *State_allocate(void);
116115
static VALUE cState_s_allocate(VALUE klass);
117116
static VALUE cState_configure(VALUE self, VALUE opts);
118117
static VALUE cState_to_h(VALUE self);
@@ -156,11 +155,11 @@ static inline void *ruby_zalloc(size_t n)
156155
return p;
157156
}
158157
#endif
159-
#ifdef TypedData_Wrap_Struct
158+
#ifdef TypedData_Make_Struct
160159
static const rb_data_type_t JSON_Generator_State_type;
161160
#define NEW_TYPEDDATA_WRAPPER 1
162161
#else
163-
#define TypedData_Wrap_Struct(klass, ignore, json) Data_Wrap_Struct(klass, NULL, State_free, json)
162+
#define TypedData_Make_Struct(klass, type, ignore, json) Data_Make_Struct(klass, type, NULL, State_free, json)
164163
#define TypedData_Get_Struct(self, JSON_Generator_State, ignore, json) Data_Get_Struct(self, JSON_Generator_State, json)
165164
#endif
166165

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)