Skip to content

Allocate OpenSSL objects in #initialize{,_copy} - #1046

Merged
rhenium merged 9 commits into
ruby:masterfrom
rhenium:ky/alloc-in-initialize
Jul 5, 2026
Merged

rhenium merged 9 commits into
ruby:masterfrom
rhenium:ky/alloc-in-initialize

Conversation

@rhenium

@rhenium rhenium commented Apr 29, 2026

Copy link
Copy Markdown
Member

Allocate the underlying OpenSSL object in #initialize{,_copy} instead of .allocate. Allocating an empty OpenSSL object in .allocate is wasteful because #initialize{,_copy} cannot always reuse an existing object and have to allocate a new one.

Some ruby/openssl classes already follow this approach, such as OpenSSL::PKey::PKey. Let's standardize on it.

Also expand New*() and Set*() macros, which are not very helpful for readability in my opinion.

@rhenium
rhenium force-pushed the ky/alloc-in-initialize branch from 890a3f9 to 1c7f29a Compare April 29, 2026 16:46
@rhenium
rhenium force-pushed the ky/alloc-in-initialize branch 2 times, most recently from 6dd5059 to 363ca19 Compare May 29, 2026 09:49
rhenium added 9 commits July 6, 2026 02:25
Allocate the wrapper object before attempting to load the provider.

Also, expand macros NewProvider() and SetProvider(), which are only
used once and are unlikely to be reused in the future.
Simplify the code and raise exceptions with a consistent error message.

This unifies the exception type raised when #initialize is called twice
to TypeError. This should not affect valid usage of these classes.
Allocate the underlying OpenSSL object in #initialize{,_copy} instead
of .allocate. Allocating an empty OpenSSL object in .allocate is
wasteful because #initialize{,_copy} cannot always reuse an existing
object and have to allocate a new one.

Some ruby/openssl classes already follow this approach, such as
OpenSSL::PKey::PKey. Let's standardize on it.

Also expand New*() and Set*() macros, which are only used once or twice
and are not very helpful for readability in my opinion.
@rhenium
rhenium force-pushed the ky/alloc-in-initialize branch from 363ca19 to f902610 Compare July 5, 2026 17:43
@rhenium
rhenium merged commit 680370b into ruby:master Jul 5, 2026
48 checks passed
rhenium added a commit to rhenium/ruby-openssl that referenced this pull request Oct 29, 2026
Allocate EVP_MD_CTX in OpenSSL::HMAC#initialize and #initialize_copy
instead of .allocate. <ruby#1046>
made similar changes to several other classes, but overlooked HMAC.

This fixes crash in OpenSSL::HMAC.allocate.reset.

Fixes ruby#1134
matzbot pushed a commit to ruby/ruby that referenced this pull request Oct 29, 2026
Allocate EVP_MD_CTX in OpenSSL::HMAC#initialize and #initialize_copy
instead of .allocate. <ruby/openssl#1046>
made similar changes to several other classes, but overlooked HMAC.

This fixes crash in OpenSSL::HMAC.allocate.reset.

Fixes ruby/openssl#1134

ruby/openssl@696d74f5cd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant