From 451fe269e5ab1270a53ac7bdeceabe47fd431f95 Mon Sep 17 00:00:00 2001 From: nobu Date: Fri, 29 May 2015 05:55:02 +0000 Subject: openssl: wrapper object before alloc * ext/openssl: make wrapper objects before allocating structs to get rid of potential memory leaks. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50673 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ext/openssl/ossl_engine.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'ext/openssl/ossl_engine.c') diff --git a/ext/openssl/ossl_engine.c b/ext/openssl/ossl_engine.c index 20dd7b0941..0e9bc96c21 100644 --- a/ext/openssl/ossl_engine.c +++ b/ext/openssl/ossl_engine.c @@ -12,11 +12,13 @@ #if defined(OSSL_ENGINE_ENABLED) -#define WrapEngine(klass, obj, engine) do { \ +#define NewEngine(klass) \ + TypedData_Wrap_Struct((klass), &ossl_engine_type, 0) +#define SetEngine(obj, engine) do { \ if (!(engine)) { \ ossl_raise(rb_eRuntimeError, "ENGINE wasn't initialized."); \ } \ - (obj) = TypedData_Wrap_Struct((klass), &ossl_engine_type, (engine)); \ + RTYPEDDATA_DATA(obj) = (engine); \ } while(0) #define GetEngine(obj, engine) do { \ TypedData_Get_Struct((obj), ENGINE, &ossl_engine_type, (engine)); \ @@ -182,11 +184,12 @@ ossl_engine_s_engines(VALUE klass) ary = rb_ary_new(); for(e = ENGINE_get_first(); e; e = ENGINE_get_next(e)){ + obj = NewEngine(klass); /* Need a ref count of two here because of ENGINE_free being * called internally by OpenSSL when moving to the next ENGINE * and by us when releasing the ENGINE reference */ ENGINE_up_ref(e); - WrapEngine(klass, obj, e); + SetEngine(obj, e); rb_ary_push(ary, obj); } @@ -213,9 +216,10 @@ ossl_engine_s_by_id(VALUE klass, VALUE id) StringValue(id); ossl_engine_s_load(1, &id, klass); + obj = NewEngine(klass); if(!(e = ENGINE_by_id(RSTRING_PTR(id)))) ossl_raise(eEngineError, NULL); - WrapEngine(klass, obj, e); + SetEngine(obj, e); if(rb_block_given_p()) rb_yield(obj); if(!ENGINE_init(e)) ossl_raise(eEngineError, NULL); @@ -232,10 +236,11 @@ ossl_engine_s_alloc(VALUE klass) ENGINE *e; VALUE obj; + obj = NewEngine(klass); if (!(e = ENGINE_new())) { ossl_raise(eEngineError, NULL); } - WrapEngine(klass, obj, e); + SetEngine(obj, e); return obj; } -- cgit v1.2.3