From 2e7458968361b54e5d887cb78f202b3191500ed9 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Wed, 29 Apr 2026 20:20:06 +0900 Subject: [PATCH] ssl: refactor SSLSocket#{peer_,}finished_message The variable-sized alloca makes me nervous, even though it turned out to be safe: the Finished message is 36 bytes long in SSL 3.0 and is usually smaller in newer protocol versions. However, the alloca is not actually needed since we can simply write into the String's content. While at it, update the rdoc comment to clarify the difference of the two methods. --- ext/openssl/ossl_ssl.c | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c index 5a6ef7260..9c0b68957 100644 --- a/ext/openssl/ossl_ssl.c +++ b/ext/openssl/ossl_ssl.c @@ -2482,16 +2482,15 @@ ossl_ssl_get_verify_result(VALUE self) /* * call-seq: - * ssl.finished_message => "finished message" - * - * Returns the last *Finished* message sent + * ssl.finished_message -> string or nil * + * Returns the contents of the last +Finished+ message sent to the peer. */ static VALUE ossl_ssl_get_finished(VALUE self) { SSL *ssl; - char sizer[1], *buf; + char sizer[1]; size_t len; GetSSL(self, ssl); @@ -2500,23 +2499,23 @@ ossl_ssl_get_finished(VALUE self) if (len == 0) return Qnil; - buf = ALLOCA_N(char, len); - SSL_get_finished(ssl, buf, len); - return rb_str_new(buf, len); + VALUE str = rb_str_new(NULL, len); + SSL_get_finished(ssl, RSTRING_PTR(str), len); + return str; } /* * call-seq: - * ssl.peer_finished_message => "peer finished message" - * - * Returns the last *Finished* message received + * ssl.peer_finished_message -> string or nil * + * Returns the contents of the last +Finished+ message expected to be sent + * by the peer. */ static VALUE ossl_ssl_get_peer_finished(VALUE self) { SSL *ssl; - char sizer[1], *buf; + char sizer[1]; size_t len; GetSSL(self, ssl); @@ -2525,9 +2524,9 @@ ossl_ssl_get_peer_finished(VALUE self) if (len == 0) return Qnil; - buf = ALLOCA_N(char, len); - SSL_get_peer_finished(ssl, buf, len); - return rb_str_new(buf, len); + VALUE str = rb_str_new(NULL, len); + SSL_get_peer_finished(ssl, RSTRING_PTR(str), len); + return str; } /*