Details
-
Type:
Bug
-
Status:
Closed
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: JRuby 1.5.3, JRuby 1.5.5
-
Fix Version/s: JRuby 1.6RC1
-
Component/s: None
-
Labels:None
-
Number of attachments :
Description
Before calling Socket#bind, it returns a packed sockaddr, but afterward it's a string.
The following IRB session illustrates the issue:
jruby-1.5.5 > server = ::Socket.new( ::Socket::AF_INET, ::Socket::SOCK_STREAM, 0 )
=> #<Socket:0x6fa8bd74>
jruby-1.5.5 > server.getsockname
=> "\002\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
jruby-1.5.5 > server.bind( ::Socket.sockaddr_in( 0, "127.0.0.1") )
=> 0
jruby-1.5.5 > server.getsockname
=> "/127.0.0.1:44603"
jruby-1.5.5 > ::Socket.unpack_sockaddr_in( server.getsockname )
ArgumentError: can't resolve socket address of wrong type
from (irb):14
Note that the above works as expected in MRI and YARV.
Possibly related to this bug: http://jira.codehaus.org/browse/JRUBY-3563
Hmm. The patch "getsockname.patch" at
JRUBY-3563looks correct but it wasn't imported?Here's a patch.
diff --git a/src/org/jruby/ext/socket/RubyBasicSocket.java b/src/org/jruby/ext/socket/RubyBasicSocket.java index 8f6d5fe..f61b9cb 100644 --- a/src/org/jruby/ext/socket/RubyBasicSocket.java +++ b/src/org/jruby/ext/socket/RubyBasicSocket.java @@ -634,11 +634,12 @@ public class RubyBasicSocket extends RubyIO { } protected IRubyObject getSocknameCommon(ThreadContext context, String caller) { - SocketAddress sock = getLocalSocket(caller); + InetSocketAddress sock = getLocalSocket(caller); if(null == sock) { return RubySocket.pack_sockaddr_in(context, null, 0, "0.0.0.0"); + } else { + return RubySocket.pack_sockaddr_in(context, null, sock.getPort(), sock.getAddress().getHostAddress()); } - return context.getRuntime().newString(sock.toString()); } @DeprecatedBefore writing a test/spec for this, I found there're some tests in test/externals/ruby1.8/socket. And when I run it, I get NPE.
I'll look into it.