From 482bf1959a257813728cc7b13a9ff698590fec44 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 10 May 2015 15:20:35 +0000 Subject: [PATCH] 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 --- ChangeLog | 6 ++++++ gc.c | 16 ++++++++++++++++ include/ruby/ruby.h | 27 +++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4560a6839c..67789050e5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Mon May 11 00:20:31 2015 Nobuyoshi Nakada + + * 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 * gc.c (gc_mark_children): call dmark function for non-NULL diff --git a/gc.c b/gc.c index 3b2d5c9519..53c0ec5dd4 100644 --- a/gc.c +++ b/gc.c @@ -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) { diff --git a/include/ruby/ruby.h b/include/ruby/ruby.h index 6a370bd118..2cafadc93e 100644 --- a/include/ruby/ruby.h +++ b/include/ruby/ruby.h @@ -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))