From 0a8a641d0a1917285cbfeca0dc47f7bd56c1f093 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Wed, 16 Apr 2025 23:51:21 +0900 Subject: [PATCH] [ruby/openssl] ssl: fix SSLSocket#syswrite with String-convertible objects Correctly pass the new object assigned by StringValue() to ossl_ssl_write_internal_safe(). This is a follow-up to commit https://github.com/ruby/openssl/commit/0d8c17aa855d (Reduce OpenSSL::Buffering#do_write overhead, 2024-12-21). https://github.com/ruby/openssl/commit/3ff096196a --- ext/openssl/ossl_ssl.c | 9 ++++----- test/openssl/test_ssl.rb | 5 +++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c index db44c423f2..a5b25e14de 100644 --- a/ext/openssl/ossl_ssl.c +++ b/ext/openssl/ossl_ssl.c @@ -2080,14 +2080,13 @@ ossl_ssl_write_internal_safe(VALUE _args) static VALUE ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts) { - VALUE args[3] = {self, str, opts}; - int state; - str = StringValue(str); - + StringValue(str); int frozen = RB_OBJ_FROZEN(str); if (!frozen) { - str = rb_str_locktmp(str); + rb_str_locktmp(str); } + int state; + VALUE args[3] = {self, str, opts}; VALUE result = rb_protect(ossl_ssl_write_internal_safe, (VALUE)args, &state); if (!frozen) { rb_str_unlocktmp(str); diff --git a/test/openssl/test_ssl.rb b/test/openssl/test_ssl.rb index f1ce0b5dfc..4642063f45 100644 --- a/test/openssl/test_ssl.rb +++ b/test/openssl/test_ssl.rb @@ -270,6 +270,11 @@ class OpenSSL::TestSSL < OpenSSL::SSLTestCase ssl.syswrite(str) assert_same buf, ssl.sysread(str.size, buf) assert_equal(str, buf) + + obj = Object.new + obj.define_singleton_method(:to_str) { str } + ssl.syswrite(obj) + assert_equal(str, ssl.sysread(str.bytesize)) } } end