ruby-changes:38383
From: nobu <ko1@a...>
Date: Mon, 11 May 2015 00:20:59 +0900 (JST)
Subject: [ruby-changes:38383] nobu:r50464 (trunk): ruby.h: fix possible memory leak
nobu 2015-05-11 00:20:35 +0900 (Mon, 11 May 2015) New Revision: 50464 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=50464 Log: 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. Modified files: trunk/ChangeLog trunk/gc.c trunk/include/ruby/ruby.h Index: include/ruby/ruby.h =================================================================== --- include/ruby/ruby.h (revision 50463) +++ include/ruby/ruby.h (revision 50464) @@ -1011,7 +1011,9 @@ typedef void (*RUBY_DATA_FUNC)(void*); https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1011 # 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 https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1032 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) https://github.com/ruby/ruby/blob/trunk/include/ruby/ruby.h#L1202 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)) Index: ChangeLog =================================================================== --- ChangeLog (revision 50463) +++ ChangeLog (revision 50464) @@ -1,3 +1,9 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon May 11 00:20:31 2015 Nobuyoshi Nakada <nobu@r...> + + * 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@r...> * gc.c (gc_mark_children): call dmark function for non-NULL Index: gc.c =================================================================== --- gc.c (revision 50463) +++ gc.c (revision 50464) @@ -1813,12 +1813,28 @@ rb_data_object_alloc(VALUE klass, void * https://github.com/ruby/ruby/blob/trunk/gc.c#L1813 } 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) { if (klass) Check_Type(klass, T_CLASS); 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) { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/