Skip to content

Commit 22e601a

Browse files
authored
Remove usage of IO internals. (ruby#627)
1 parent cb8f4ee commit 22e601a

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

ext/openssl/extconf.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
end
2828
$defs.push("-D""OPENSSL_SUPPRESS_DEPRECATED")
2929

30+
have_func("rb_io_descriptor")
3031
have_func("rb_io_maybe_wait(0, Qnil, Qnil, Qnil)", "ruby/io.h") # Ruby 3.1
3132

3233
Logging::message "=== Checking for system dependent stuff... ===\n"

ext/openssl/ossl_ssl.c

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,6 +1653,17 @@ ossl_ssl_initialize(int argc, VALUE *argv, VALUE self)
16531653
return self;
16541654
}
16551655

1656+
#ifndef HAVE_RB_IO_DESCRIPTOR
1657+
static int
1658+
io_descriptor_fallback(VALUE io)
1659+
{
1660+
rb_io_t *fptr;
1661+
GetOpenFile(io, fptr);
1662+
return fptr->fd;
1663+
}
1664+
#define rb_io_descriptor io_descriptor_fallback
1665+
#endif
1666+
16561667
static VALUE
16571668
ossl_ssl_setup(VALUE self)
16581669
{
@@ -1668,8 +1679,8 @@ ossl_ssl_setup(VALUE self)
16681679
GetOpenFile(io, fptr);
16691680
rb_io_check_readable(fptr);
16701681
rb_io_check_writable(fptr);
1671-
if (!SSL_set_fd(ssl, TO_SOCKET(fptr->fd)))
1672-
ossl_raise(eSSLError, "SSL_set_fd");
1682+
if (!SSL_set_fd(ssl, TO_SOCKET(rb_io_descriptor(io))))
1683+
ossl_raise(eSSLError, "SSL_set_fd");
16731684

16741685
return Qtrue;
16751686
}
@@ -1709,21 +1720,25 @@ no_exception_p(VALUE opts)
17091720
#endif
17101721

17111722
static void
1712-
io_wait_writable(rb_io_t *fptr)
1723+
io_wait_writable(VALUE io)
17131724
{
17141725
#ifdef HAVE_RB_IO_MAYBE_WAIT
1715-
rb_io_maybe_wait_writable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT);
1726+
rb_io_maybe_wait_writable(errno, io, RUBY_IO_TIMEOUT_DEFAULT);
17161727
#else
1728+
rb_io_t *fptr;
1729+
GetOpenFile(io, fptr);
17171730
rb_io_wait_writable(fptr->fd);
17181731
#endif
17191732
}
17201733

17211734
static void
1722-
io_wait_readable(rb_io_t *fptr)
1735+
io_wait_readable(VALUE io)
17231736
{
17241737
#ifdef HAVE_RB_IO_MAYBE_WAIT
1725-
rb_io_maybe_wait_readable(errno, fptr->self, RUBY_IO_TIMEOUT_DEFAULT);
1738+
rb_io_maybe_wait_readable(errno, io, RUBY_IO_TIMEOUT_DEFAULT);
17261739
#else
1740+
rb_io_t *fptr;
1741+
GetOpenFile(io, fptr);
17271742
rb_io_wait_readable(fptr->fd);
17281743
#endif
17291744
}
@@ -1732,7 +1747,6 @@ static VALUE
17321747
ossl_start_ssl(VALUE self, int (*func)(SSL *), const char *funcname, VALUE opts)
17331748
{
17341749
SSL *ssl;
1735-
rb_io_t *fptr;
17361750
int ret, ret2;
17371751
VALUE cb_state;
17381752
int nonblock = opts != Qfalse;
@@ -1744,7 +1758,7 @@ ossl_start_ssl(VALUE self, int (*func)(SSL *), const char *funcname, VALUE opts)
17441758

17451759
GetSSL(self, ssl);
17461760

1747-
GetOpenFile(rb_attr_get(self, id_i_io), fptr);
1761+
VALUE io = rb_attr_get(self, id_i_io);
17481762
for(;;){
17491763
ret = func(ssl);
17501764

@@ -1762,12 +1776,12 @@ ossl_start_ssl(VALUE self, int (*func)(SSL *), const char *funcname, VALUE opts)
17621776
case SSL_ERROR_WANT_WRITE:
17631777
if (no_exception_p(opts)) { return sym_wait_writable; }
17641778
write_would_block(nonblock);
1765-
io_wait_writable(fptr);
1779+
io_wait_writable(io);
17661780
continue;
17671781
case SSL_ERROR_WANT_READ:
17681782
if (no_exception_p(opts)) { return sym_wait_readable; }
17691783
read_would_block(nonblock);
1770-
io_wait_readable(fptr);
1784+
io_wait_readable(io);
17711785
continue;
17721786
case SSL_ERROR_SYSCALL:
17731787
#ifdef __APPLE__
@@ -1906,8 +1920,7 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
19061920
SSL *ssl;
19071921
int ilen;
19081922
VALUE len, str;
1909-
rb_io_t *fptr;
1910-
VALUE io, opts = Qnil;
1923+
VALUE opts = Qnil;
19111924

19121925
if (nonblock) {
19131926
rb_scan_args(argc, argv, "11:", &len, &str, &opts);
@@ -1932,8 +1945,7 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
19321945
if (ilen == 0)
19331946
return str;
19341947

1935-
io = rb_attr_get(self, id_i_io);
1936-
GetOpenFile(io, fptr);
1948+
VALUE io = rb_attr_get(self, id_i_io);
19371949

19381950
rb_str_locktmp(str);
19391951
for (;;) {
@@ -1953,15 +1965,15 @@ ossl_ssl_read_internal(int argc, VALUE *argv, VALUE self, int nonblock)
19531965
if (no_exception_p(opts)) { return sym_wait_writable; }
19541966
write_would_block(nonblock);
19551967
}
1956-
io_wait_writable(fptr);
1968+
io_wait_writable(io);
19571969
continue;
19581970
case SSL_ERROR_WANT_READ:
19591971
if (nonblock) {
19601972
rb_str_unlocktmp(str);
19611973
if (no_exception_p(opts)) { return sym_wait_readable; }
19621974
read_would_block(nonblock);
19631975
}
1964-
io_wait_readable(fptr);
1976+
io_wait_readable(io);
19651977
continue;
19661978
case SSL_ERROR_SYSCALL:
19671979
if (!ERR_peek_error()) {
@@ -2027,14 +2039,14 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts)
20272039
SSL *ssl;
20282040
rb_io_t *fptr;
20292041
int num, nonblock = opts != Qfalse;
2030-
VALUE tmp, io;
2042+
VALUE tmp;
20312043

20322044
GetSSL(self, ssl);
20332045
if (!ssl_started(ssl))
20342046
rb_raise(eSSLError, "SSL session is not started yet");
20352047

20362048
tmp = rb_str_new_frozen(StringValue(str));
2037-
io = rb_attr_get(self, id_i_io);
2049+
VALUE io = rb_attr_get(self, id_i_io);
20382050
GetOpenFile(io, fptr);
20392051

20402052
/* SSL_write(3ssl) manpage states num == 0 is undefined */
@@ -2050,12 +2062,12 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts)
20502062
case SSL_ERROR_WANT_WRITE:
20512063
if (no_exception_p(opts)) { return sym_wait_writable; }
20522064
write_would_block(nonblock);
2053-
io_wait_writable(fptr);
2065+
io_wait_writable(io);
20542066
continue;
20552067
case SSL_ERROR_WANT_READ:
20562068
if (no_exception_p(opts)) { return sym_wait_readable; }
20572069
read_would_block(nonblock);
2058-
io_wait_readable(fptr);
2070+
io_wait_readable(io);
20592071
continue;
20602072
case SSL_ERROR_SYSCALL:
20612073
#ifdef __APPLE__

0 commit comments

Comments
 (0)