1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

ruby.h: fix possible memory leak

* include/ruby/ruby.h (Data_Make_Struct, TypedData_Make_Struct):
  allocate wrapper data object before allocating DATA_PTR to get
  rid of possible memory leak when the former failed.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50464 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2015-05-10 15:20:35 +00:00
parent 3875df97f1
commit 482bf1959a
3 changed files with 45 additions and 4 deletions

View file

@ -1,3 +1,9 @@
Mon May 11 00:20:31 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* include/ruby/ruby.h (Data_Make_Struct, TypedData_Make_Struct):
allocate wrapper data object before allocating DATA_PTR to get
rid of possible memory leak when the former failed.
Sun May 10 21:32:45 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
* gc.c (gc_mark_children): call dmark function for non-NULL

16
gc.c
View file

@ -1812,6 +1812,14 @@ rb_data_object_alloc(VALUE klass, void *datap, RUBY_DATA_FUNC dmark, RUBY_DATA_F
return newobj_of(klass, T_DATA, (VALUE)dmark, (VALUE)dfree, (VALUE)datap);
}
VALUE
rb_data_object_zalloc(VALUE klass, size_t size, RUBY_DATA_FUNC dmark, RUBY_DATA_FUNC dfree)
{
VALUE obj = rb_data_object_alloc(klass, 0, dmark, dfree);
DATA_PTR(obj) = xcalloc(1, size);
return obj;
}
VALUE
rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *type)
{
@ -1819,6 +1827,14 @@ rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *type)
return newobj_of(klass, T_DATA | (type->flags & ~T_MASK), (VALUE)type, (VALUE)1, (VALUE)datap);
}
VALUE
rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type)
{
VALUE obj = rb_data_typed_object_alloc(klass, 0, type);
DATA_PTR(obj) = xcalloc(1, size);
return obj;
}
size_t
rb_objspace_data_type_memsize(VALUE obj)
{

View file

@ -1011,7 +1011,9 @@ typedef void (*RUBY_DATA_FUNC)(void*);
# endif
#endif
VALUE rb_data_object_alloc(VALUE,void*,RUBY_DATA_FUNC,RUBY_DATA_FUNC);
VALUE rb_data_object_zalloc(VALUE,size_t,RUBY_DATA_FUNC,RUBY_DATA_FUNC);
VALUE rb_data_typed_object_alloc(VALUE klass, void *datap, const rb_data_type_t *);
VALUE rb_data_typed_object_zalloc(VALUE klass, size_t size, const rb_data_type_t *type);
int rb_typeddata_inherited_p(const rb_data_type_t *child, const rb_data_type_t *parent);
int rb_typeddata_is_kind_of(VALUE, const rb_data_type_t *);
void *rb_check_typeddata(VALUE, const rb_data_type_t *);
@ -1030,16 +1032,14 @@ void *rb_check_typeddata(VALUE, const rb_data_type_t *);
rb_data_object_alloc((klass),(sval),(RUBY_DATA_FUNC)(mark),(RUBY_DATA_FUNC)(free))
#define Data_Make_Struct(klass,type,mark,free,sval) (\
(sval) = ZALLOC(type),\
Data_Wrap_Struct((klass),(mark),(free),(sval))\
rb_data_struct_make((klass),(RUBY_DATA_FUNC)(mark),(RUBY_DATA_FUNC)(free),(void **)&(sval),sizeof(type)) \
)
#define TypedData_Wrap_Struct(klass,data_type,sval)\
rb_data_typed_object_alloc((klass),(sval),(data_type))
#define TypedData_Make_Struct(klass, type, data_type, sval) (\
(sval) = ZALLOC(type),\
TypedData_Wrap_Struct((klass),(data_type),(sval))\
rb_data_typed_struct_make((klass),(data_type),(void **)&(sval),sizeof(type)) \
)
#define Data_Get_Struct(obj,type,sval) \
@ -1202,12 +1202,31 @@ rb_data_object_get_warning(VALUE obj)
return rb_data_object_get(obj);
}
static inline VALUE
rb_data_struct_make(VALUE klass, RUBY_DATA_FUNC mark_func, RUBY_DATA_FUNC free_func, void **datap, size_t size)
{
VALUE obj = rb_data_object_zalloc(klass, size, mark_func, free_func);
*datap = DATA_PTR(obj);
return obj;
}
static inline VALUE
rb_data_typed_struct_make(VALUE klass, const rb_data_type_t *type, void **datap, size_t size)
{
VALUE obj = rb_data_typed_object_zalloc(klass, size, type);
*datap = DATA_PTR(obj);
return obj;
}
#define rb_data_object_alloc_0 rb_data_object_alloc
#define rb_data_object_alloc_1 rb_data_object_alloc_warning
#define rb_data_object_alloc RUBY_MACRO_SELECT(rb_data_object_alloc_, RUBY_UNTYPED_DATA_WARNING)
#define rb_data_object_get_0 rb_data_object_get
#define rb_data_object_get_1 rb_data_object_get_warning
#define rb_data_object_get RUBY_MACRO_SELECT(rb_data_object_get_, RUBY_UNTYPED_DATA_WARNING)
#define rb_data_struct_make_0 rb_data_struct_make
#define rb_data_struct_make_1 rb_data_struct_make_warning
#define rb_data_struct_make RUBY_MACRO_SELECT(rb_data_struct_make_, RUBY_UNTYPED_DATA_WARNING)
#if USE_RGENGC
#define OBJ_PROMOTED_RAW(x) ((RBASIC(x)->flags & (FL_PROMOTED0|FL_PROMOTED1)) == (FL_PROMOTED0|FL_PROMOTED1))