Skip to content

Commit a6ba9f8

Browse files
committed
x509store: explicitly call rb_gc_mark() against Store/StoreContext
We store the reverse reference to the Ruby object in the OpenSSL struct for use from OpenSSL callback functions. To prevent the Ruby object from being relocated by GC.compact, we must "pin" it by calling rb_gc_mark().
1 parent 022b7ce commit a6ba9f8

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

ext/openssl/ossl_x509store.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,13 @@ VALUE cX509Store;
105105
VALUE cX509StoreContext;
106106
VALUE eX509StoreError;
107107

108+
static void
109+
ossl_x509store_mark(void *ptr)
110+
{
111+
X509_STORE *store = ptr;
112+
rb_gc_mark((VALUE)X509_STORE_get_ex_data(store, store_ex_verify_cb_idx));
113+
}
114+
108115
static void
109116
ossl_x509store_free(void *ptr)
110117
{
@@ -114,7 +121,7 @@ ossl_x509store_free(void *ptr)
114121
static const rb_data_type_t ossl_x509store_type = {
115122
"OpenSSL/X509/STORE",
116123
{
117-
0, ossl_x509store_free,
124+
ossl_x509store_mark, ossl_x509store_free,
118125
},
119126
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
120127
};
@@ -456,23 +463,16 @@ ossl_x509store_verify(int argc, VALUE *argv, VALUE self)
456463
return result;
457464
}
458465

459-
/*
460-
* Public Functions
461-
*/
462-
static void ossl_x509stctx_free(void*);
463-
464-
465-
static const rb_data_type_t ossl_x509stctx_type = {
466-
"OpenSSL/X509/STORE_CTX",
467-
{
468-
0, ossl_x509stctx_free,
469-
},
470-
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
471-
};
472-
473466
/*
474467
* Private functions
475468
*/
469+
static void
470+
ossl_x509stctx_mark(void *ptr)
471+
{
472+
X509_STORE_CTX *ctx = ptr;
473+
rb_gc_mark((VALUE)X509_STORE_CTX_get_ex_data(ctx, stctx_ex_verify_cb_idx));
474+
}
475+
476476
static void
477477
ossl_x509stctx_free(void *ptr)
478478
{
@@ -484,6 +484,14 @@ ossl_x509stctx_free(void *ptr)
484484
X509_STORE_CTX_free(ctx);
485485
}
486486

487+
static const rb_data_type_t ossl_x509stctx_type = {
488+
"OpenSSL/X509/STORE_CTX",
489+
{
490+
ossl_x509stctx_mark, ossl_x509stctx_free,
491+
},
492+
0, 0, RUBY_TYPED_FREE_IMMEDIATELY,
493+
};
494+
487495
static VALUE
488496
ossl_x509stctx_alloc(VALUE klass)
489497
{

0 commit comments

Comments
 (0)