Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: JRuby 1.6.7
-
Fix Version/s: JRuby 1.7.0.pre1
-
Component/s: Standard Library
-
Environment:jruby 1.6.7 (ruby-1.8.7-p357) (2012-02-22 3e82bc8) (OpenJDK 64-Bit Server VM 1.6.0_24) [linux-amd64-java]
-
Number of attachments :
Description
`Socket#bind` does not accept `nil` for the local port to bind to. Both MRI 1.8.7 and Rubinius seem to convert a `nil` port value to `0`.
Reproduction:
require 'socket'
socket = UDPSocket.new
socket.bind('localhost',nil)
Expected Results:
# => 0
Actual Results:
TypeError: no implicit conversion from nil to integer from org/jruby/ext/socket/RubyUDPSocket.java:131:in `bind' from (irb):3:in `evaluate' from org/jruby/RubyKernel.java:1083:in `eval' from /home/hal/.rvm/rubies/jruby-1.6.7/lib/ruby/1.8/irb.rb:158:in `eval_input' from /home/hal/.rvm/rubies/jruby-1.6.7/lib/ruby/1.8/irb.rb:271:in `signal_status' from /home/hal/.rvm/rubies/jruby-1.6.7/lib/ruby/1.8/irb.rb:155:in `eval_input' from org/jruby/RubyKernel.java:1410:in `loop' from org/jruby/RubyKernel.java:1183:in `catch' from /home/hal/.rvm/rubies/jruby-1.6.7/lib/ruby/1.8/irb.rb:154:in `eval_input' from /home/hal/.rvm/rubies/jruby-1.6.7/lib/ruby/1.8/irb.rb:71:in `start' from org/jruby/RubyKernel.java:1183:in `catch' from /home/hal/.rvm/rubies/jruby-1.6.7/lib/ruby/1.8/irb.rb:70:in `start' from /home/hal/.rvm/rubies/jruby-1.6.7/bin/irb:17:in `(root)'
I don't think port number nil is converted to 0. (It is true that MRI returns 0.)
Instead, what I believe is happening (https://github.com/ruby/ruby/blob/da39d32f60778b111c6cf063ae74e2627be14b77/ext/socket/raddrinfo.c#L318-320
) is that when nil is passed, the NULL pointer is passed to getaddrinfo() eventually (https://github.com/ruby/ruby/blob/da39d32f60778b111c6cf063ae74e2627be14b77/ext/socket/raddrinfo.c#L172). I am not sure what the consequences are. (Perhaps it is OS-dependent; http://linux.die.net/man/3/getaddrinfo suggests that the port number is uninitialized on Linux, and I couldn't figure out what OS X does, for example.)
On the other hand, on Java, it is not possible to get an InetSocketAddress without a port. See http://docs.oracle.com/javase/6/docs/api/java/net/InetSocketAddress.html
I committed 79a9c2e to master, which chooses an ephemeral port (by mapping nil to 0 for JVM), so the test case here works.