diff options
-rw-r--r-- | ChangeLog | 7 | ||||
-rw-r--r-- | ext/openssl/ossl_ssl.c | 8 | ||||
-rw-r--r-- | test/openssl/test_pair.rb | 11 |
3 files changed, 25 insertions, 1 deletions
@@ -1,3 +1,10 @@ +Sun Aug 7 06:48:21 2016 Eric Wong <e@80x24.org> + + * ext/openssl/ossl_ssl.c (ossl_ssl_write_internal): + avoid undefined behavior + * test/openssl/test_pair.rb (test_write_zero): new test + [ruby-core:76751] [Bug #12660] + Sat Aug 6 09:35:30 2016 Nobuyoshi Nakada <nobu@ruby-lang.org> * id_table.h (rb_id_table_iterator_result): add dummy sentinel diff --git a/ext/openssl/ossl_ssl.c b/ext/openssl/ossl_ssl.c index 5876946183..a180ff251d 100644 --- a/ext/openssl/ossl_ssl.c +++ b/ext/openssl/ossl_ssl.c @@ -1744,7 +1744,13 @@ ossl_ssl_write_internal(VALUE self, VALUE str, VALUE opts) if (ssl_started(ssl)) { for (;;){ - nwrite = SSL_write(ssl, RSTRING_PTR(str), RSTRING_LENINT(str)); + int num = RSTRING_LENINT(str); + + /* SSL_write(3ssl) manpage states num == 0 is undefined */ + if (num == 0) + goto end; + + nwrite = SSL_write(ssl, RSTRING_PTR(str), num); switch(ssl_get_error(ssl, nwrite)){ case SSL_ERROR_NONE: goto end; diff --git a/test/openssl/test_pair.rb b/test/openssl/test_pair.rb index 5750f3b7df..d611d0f965 100644 --- a/test/openssl/test_pair.rb +++ b/test/openssl/test_pair.rb @@ -311,6 +311,17 @@ module OpenSSL::TestPairM } end + def test_write_zero + ssl_pair {|s1, s2| + assert_equal 0, s2.write_nonblock('', exception: false) + assert_kind_of Symbol, s1.read_nonblock(1, exception: false) + assert_equal 0, s2.syswrite('') + assert_kind_of Symbol, s1.read_nonblock(1, exception: false) + assert_equal 0, s2.write('') + assert_kind_of Symbol, s1.read_nonblock(1, exception: false) + } + end + def tcp_pair host = "127.0.0.1" serv = TCPServer.new(host, 0) |