ruby-changes:49899
From: nobu <ko1@a...>
Date: Wed, 24 Jan 2018 00:31:28 +0900 (JST)
Subject: [ruby-changes:49899] nobu:r62017 (trunk): init.c: encode socket error message
nobu 2018-01-24 00:31:22 +0900 (Wed, 24 Jan 2018) New Revision: 62017 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=62017 Log: init.c: encode socket error message * ext/socket/init.c (rsock_raise_socket_error): on Windows, encode error messages from wide characters to the default encodings. [ruby-core:84972] [Bug #14384] Modified files: trunk/ext/socket/init.c trunk/test/socket/test_addrinfo.rb Index: ext/socket/init.c =================================================================== --- ext/socket/init.c (revision 62016) +++ ext/socket/init.c (revision 62017) @@ -10,6 +10,10 @@ https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L10 #include "rubysocket.h" +#ifdef _WIN32 +VALUE rb_w32_conv_from_wchar(const WCHAR *wstr, rb_encoding *enc); +#endif + VALUE rb_cBasicSocket; VALUE rb_cIPSocket; VALUE rb_cTCPSocket; @@ -39,7 +43,15 @@ rsock_raise_socket_error(const char *rea https://github.com/ruby/ruby/blob/trunk/ext/socket/init.c#L43 if (error == EAI_SYSTEM && (e = errno) != 0) rb_syserr_fail(e, reason); #endif +#ifdef _WIN32 + rb_encoding *enc = rb_default_internal_encoding(); + VALUE msg = rb_sprintf("%s: ", reason); + if (!enc) enc = rb_default_internal_encoding(); + rb_str_concat(msg, rb_w32_conv_from_wchar(gai_strerrorW(error), enc)); + rb_exc_raise(rb_exc_new_str(rb_eSocket, msg)); +#else rb_raise(rb_eSocket, "%s: %s", reason, gai_strerror(error)); +#endif } #ifdef _WIN32 Index: test/socket/test_addrinfo.rb =================================================================== --- test/socket/test_addrinfo.rb (revision 62016) +++ test/socket/test_addrinfo.rb (revision 62017) @@ -102,6 +102,14 @@ class TestSocketAddrinfo < Test::Unit::T https://github.com/ruby/ruby/blob/trunk/test/socket/test_addrinfo.rb#L102 assert(!ipv4_ai.unix?) end + def test_error_message + e = assert_raise_with_message(SocketError, /getaddrinfo:/) do + Addrinfo.ip("...") + end + m = e.message + assert_not_equal([false, Encoding::ASCII_8BIT], [m.ascii_only?, m.encoding], proc {m.inspect}) + end + def test_ipv4_address_predicates list = [ [:ipv4_private?, "10.0.0.0", "10.255.255.255", -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/