From d3c8e661e83daa38290bd13ae04149473f96afe6 Mon Sep 17 00:00:00 2001 From: HoneyryderChuck Date: Sat, 19 Oct 2024 17:09:51 +0100 Subject: [PATCH] make bn shareable when frozen --- ext/openssl/ossl_bn.c | 6 +++++- test/openssl/test_bn.rb | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/ext/openssl/ossl_bn.c b/ext/openssl/ossl_bn.c index ea19914c5..fe234a97a 100644 --- a/ext/openssl/ossl_bn.c +++ b/ext/openssl/ossl_bn.c @@ -37,7 +37,7 @@ static const rb_data_type_t ossl_bn_type = { { 0, ossl_bn_free, }, - 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED, + 0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | RUBY_TYPED_FROZEN_SHAREABLE, }; /* @@ -259,6 +259,7 @@ ossl_bn_initialize(int argc, VALUE *argv, VALUE self) ossl_raise(rb_eArgError, "invalid argument"); } + rb_check_frozen(self); if (RB_INTEGER_TYPE_P(str)) { GetBN(self, bn); integer_to_bnptr(str, bn); @@ -689,6 +690,7 @@ BIGNUM_3c(mod_exp) ossl_bn_##func(VALUE self, VALUE bit) \ { \ BIGNUM *bn; \ + rb_check_frozen(self); \ GetBN(self, bn); \ if (BN_##func(bn, NUM2INT(bit)) <= 0) { \ ossl_raise(eBNError, NULL); \ @@ -778,6 +780,7 @@ BIGNUM_SHIFT(rshift) { \ BIGNUM *bn; \ int b; \ + rb_check_frozen(self); \ b = NUM2INT(bits); \ GetBN(self, bn); \ if (BN_##func(bn, bn, b) <= 0) \ @@ -1187,6 +1190,7 @@ ossl_bn_set_flags(VALUE self, VALUE arg) BIGNUM *bn; GetBN(self, bn); + rb_check_frozen(self); BN_set_flags(bn, NUM2INT(arg)); return Qnil; } diff --git a/test/openssl/test_bn.rb b/test/openssl/test_bn.rb index ea88ff06c..1217f250a 100644 --- a/test/openssl/test_bn.rb +++ b/test/openssl/test_bn.rb @@ -365,6 +365,8 @@ def test_ractor assert_include(128..255, Ractor.new { OpenSSL::BN.rand(8)}.take) assert_include(0...2**32, Ractor.new { OpenSSL::BN.generate_prime(32) }.take) assert_equal(0, Ractor.new { OpenSSL::BN.new(999).get_flags(OpenSSL::BN::CONSTTIME) }.take) + # test if shareable when frozen + assert Ractor.shareable?(@e1.freeze) end end end