Skip to content

Commit f2db943

Browse files
ko1nobu
authored andcommitted
support Ruby 2.x for openssl
ruby/ruby@e76b56f
1 parent 9e7cf9e commit f2db943

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

ext/openssl/ossl.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,11 @@ print_mem_leaks(VALUE self)
497497
int ret;
498498
#endif
499499

500-
BN_CTX_free(ossl_bn_ctx);
501-
ossl_bn_ctx = NULL;
500+
#ifndef HAVE_RB_EXT_RACTOR_SAFE
501+
// for Ruby 2.x
502+
void ossl_bn_ctx_free(void); // ossl_bn.c
503+
ossl_bn_ctx_free();
504+
#endif
502505

503506
#if OPENSSL_VERSION_NUMBER >= 0x10100000
504507
ret = CRYPTO_mem_leaks_fp(stderr);

ext/openssl/ossl_bn.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99
*/
1010
/* modified by Michal Rokos <[email protected]> */
1111
#include "ossl.h"
12+
13+
#if HAVE_RB_EXT_RACTOR_SAFE
1214
#include <ruby/ractor.h>
15+
#endif
1316

1417
#define NewBN(klass) \
1518
TypedData_Wrap_Struct((klass), &ossl_bn_type, 0)
@@ -152,6 +155,7 @@ ossl_bn_value_ptr(volatile VALUE *ptr)
152155
* Private
153156
*/
154157

158+
#if HAVE_RB_EXT_RACTOR_SAFE
155159
void
156160
ossl_bn_ctx_free(void *ptr)
157161
{
@@ -180,6 +184,28 @@ ossl_bn_ctx_get(void)
180184
}
181185
return ctx;
182186
}
187+
#else
188+
// for ruby 2.x
189+
static BN_CTX *gv_ossl_bn_ctx;
190+
191+
BN_CTX *
192+
ossl_bn_ctx_get(void)
193+
{
194+
if (gv_ossl_bn_ctx == NULL) {
195+
if (!(gv_ossl_bn_ctx = BN_CTX_new())) {
196+
ossl_raise(rb_eRuntimeError, "Cannot init BN_CTX");
197+
}
198+
}
199+
return gv_ossl_bn_ctx;
200+
}
201+
202+
void
203+
ossl_bn_ctx_free(void)
204+
{
205+
BN_CTX_free(gv_ossl_bn_ctx);
206+
gv_ossl_bn_ctx = NULL;
207+
}
208+
#endif
183209

184210
static VALUE
185211
ossl_bn_alloc(VALUE klass)
@@ -1116,7 +1142,11 @@ Init_ossl_bn(void)
11161142
eOSSLError = rb_define_class_under(mOSSL, "OpenSSLError", rb_eStandardError);
11171143
#endif
11181144

1145+
#ifdef HAVE_RB_EXT_RACTOR_SAFE
11191146
ossl_bn_ctx_key = rb_ractor_local_storage_ptr_newkey(&ossl_bn_ctx_key_type);
1147+
#else
1148+
ossl_bn_ctx_get();
1149+
#endif
11201150

11211151
eBNError = rb_define_class_under(mOSSL, "BNError", eOSSLError);
11221152

0 commit comments

Comments
 (0)