From f94741190ef24393380824b2ee30fcd82e359401 Mon Sep 17 00:00:00 2001 From: Yusuke Endoh Date: Fri, 20 Nov 2026 16:20:43 +0900 Subject: [PATCH] Iterate the options of pkey_ctx_apply_options without calling #each The block given to #each carried the EVP_PKEY_CTX pointer, so a Hash subclass that overrides #each could retain the block and call it after the context was freed. Iterate the Hash with rb_hash_foreach instead. Co-Authored-By: Claude Opus 5.5 --- ext/openssl/ossl_pkey.c | 10 ++++------ test/openssl/test_pkey.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ext/openssl/ossl_pkey.c b/ext/openssl/ossl_pkey.c index 50cc94e29..0db0e7b98 100644 --- a/ext/openssl/ossl_pkey.c +++ b/ext/openssl/ossl_pkey.c @@ -242,10 +242,9 @@ ossl_pkey_new_from_data(int argc, VALUE *argv, VALUE self) return ossl_pkey_wrap(pkey); } -static VALUE -pkey_ctx_apply_options_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ctx_v)) +static int +pkey_ctx_apply_options_i(VALUE key, VALUE value, VALUE ctx_v) { - VALUE key = rb_ary_entry(i, 0), value = rb_ary_entry(i, 1); EVP_PKEY_CTX *ctx = (EVP_PKEY_CTX *)ctx_v; if (SYMBOL_P(key)) @@ -255,7 +254,7 @@ pkey_ctx_apply_options_i(RB_BLOCK_CALL_FUNC_ARGLIST(i, ctx_v)) if (EVP_PKEY_CTX_ctrl_str(ctx, StringValueCStr(key), StringValueCStr(value)) <= 0) ossl_raise(ePKeyError, "EVP_PKEY_CTX_ctrl_str(ctx, %+"PRIsVALUE", %+"PRIsVALUE")", key, value); - return Qnil; + return ST_CONTINUE; } static VALUE @@ -264,8 +263,7 @@ pkey_ctx_apply_options0(VALUE args_v) VALUE *args = (VALUE *)args_v; Check_Type(args[1], T_HASH); - rb_block_call(args[1], rb_intern("each"), 0, NULL, - pkey_ctx_apply_options_i, args[0]); + rb_hash_foreach(args[1], pkey_ctx_apply_options_i, args[0]); return Qnil; } diff --git a/test/openssl/test_pkey.rb b/test/openssl/test_pkey.rb index d9503c4e7..b9cd4d4f8 100644 --- a/test/openssl/test_pkey.rb +++ b/test/openssl/test_pkey.rb @@ -25,6 +25,17 @@ def test_s_generate_parameters } end + def test_s_generate_parameters_options_without_each + options_class = Class.new(Hash) { + def each + raise "must not be called" + end + } + options = options_class["ec_paramgen_curve" => "secp384r1"] + pkey = OpenSSL::PKey.generate_parameters("EC", options) + assert_equal "secp384r1", pkey.group.curve_name + end + def test_s_generate_parameters_with_block # DSA kengen is not FIPS-approved. # https://github.com/openssl/openssl/commit/49a35f0#diff-605396c063194975af8ce31399d42690ab18186b422fb5012101cc9132660fe1R611-R614