From 182604c8c35a7ae7bbf097f7d8b8d2eacc817f2c Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Sun, 4 Dec 2016 21:22:35 +0900 Subject: bn: keep reference to temporary OpenSSL::BN object created by GetBNPtr() GetBNPtr() accepts both OpenSSL::BN and Ruby integers. In the latter case, it creates a temporary OpenSSL::BN internally. The OpenSSL::BN object immediately disappears from the stack and is not protected from GC. Fixes: https://github.com/ruby/openssl/issues/87 --- ext/openssl/ossl_pkey_ec.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'ext/openssl/ossl_pkey_ec.c') diff --git a/ext/openssl/ossl_pkey_ec.c b/ext/openssl/ossl_pkey_ec.c index 5191c0f4..fc3f034a 100644 --- a/ext/openssl/ossl_pkey_ec.c +++ b/ext/openssl/ossl_pkey_ec.c @@ -1635,7 +1635,7 @@ static VALUE ossl_ec_point_mul(int argc, VALUE *argv, VALUE self) * points | self | arg2[0] | arg2[1] | ... */ long i, num; - VALUE tmp_p, tmp_b; + VALUE bns_tmp, tmp_p, tmp_b; const EC_POINT **points; const BIGNUM **bignums; @@ -1645,9 +1645,13 @@ static VALUE ossl_ec_point_mul(int argc, VALUE *argv, VALUE self) ossl_raise(rb_eArgError, "bns must be 1 longer than points; see the documentation"); num = RARRAY_LEN(arg1); + bns_tmp = rb_ary_tmp_new(num); bignums = ALLOCV_N(const BIGNUM *, tmp_b, num); - for (i = 0; i < num; i++) - bignums[i] = GetBNPtr(RARRAY_AREF(arg1, i)); + for (i = 0; i < num; i++) { + VALUE item = RARRAY_AREF(arg1, i); + bignums[i] = GetBNPtr(item); + rb_ary_push(bns_tmp, item); + } points = ALLOCV_N(const EC_POINT *, tmp_p, num); points[0] = point_self; /* self */ -- cgit v1.2.3