From 66c4dca1d8d48ed0d8534351bbd305bbdf271e00 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Wed, 14 Nov 2012 01:42:18 -0500 Subject: [PATCH 1/4] Fix Socket#socketpair on X19. Socket#socketpair `type` argument can accept a symbol that references to a `Socket::SOCK_*` constant. require 'socket' # Before Socket.socketpair(Socket::PF_UNIX, Socket::SOCK_DGRAM, 0) # => [#, #] Socket.socketpair(Socket::PF_UNIX, :DGRAM, 0) # => TypeError: Tried to use non-reference value 0x15a86 as type Bignum (10) # After Socket.socketpair(Socket::PF_UNIX, Socket::SOCK_DGRAM, 0) # => [#, #] Socket.socketpair(Socket::PF_UNIX, :DGRAM, 0) # => [#, #] Fixes #2011. --- lib/19/socket.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/19/socket.rb b/lib/19/socket.rb index 4736592bcd..6fcc127cac 100644 --- a/lib/19/socket.rb +++ b/lib/19/socket.rb @@ -684,6 +684,14 @@ def self.socketpair(domain, type, protocol, klass=self) end end + if type.kind_of? Symbol + begin + type = Socket::Constants.const_get("SOCK_#{type}") + rescue NameError + raise SocketError, "unknown socket type #{type}" + end + end + FFI::MemoryPointer.new :int, 2 do |mp| Socket::Foreign.socketpair(domain, type, protocol, mp) fd0, fd1 = mp.read_array_of_int(2) From b66bb353da8ff8edc6c0f384ad2bc75c5f7cbc4c Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Wed, 14 Nov 2012 02:00:58 -0500 Subject: [PATCH 2/4] Add specs for Socket#socketpair on X19 --- spec/ruby/library/socket/shared/socketpair.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/ruby/library/socket/shared/socketpair.rb b/spec/ruby/library/socket/shared/socketpair.rb index 41fdc8dea5..8ac8416cb2 100644 --- a/spec/ruby/library/socket/shared/socketpair.rb +++ b/spec/ruby/library/socket/shared/socketpair.rb @@ -8,4 +8,16 @@ s2.close end end + + ruby_version_is "1.9" do + it "raises SocketError if given symbol is not a Socket constants reference" do + lambda { Socket.socketpair(Socket::AF_UNIX, :NO_EXIST, 0) }.should raise_error(SocketError) + end + + it "not raises SocketError if given symbol references a Socket constant" do + [ :DGRAM, :RAW, :RDM, :SEQPACKET, :STREAM ].each do |socket_type| + lambda { Socket.socketpair(Socket::AF_UNIX, socket_type, 0) }.should_not raise_error(SocketError) + end + end + end end From e0cfca44c3e623495738b244ef6db4ef9790cfde Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Fri, 16 Nov 2012 11:45:13 -0500 Subject: [PATCH 3/4] Fix Socket#socketpair when socket type is not a Integer or String X18 --- lib/18/socket.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/18/socket.rb b/lib/18/socket.rb index 52a01feb26..c981d9f61c 100644 --- a/lib/18/socket.rb +++ b/lib/18/socket.rb @@ -682,6 +682,8 @@ def self.socketpair(domain, type, protocol, klass=self) else raise SocketError, "unknown socket type #{type}" end + elsif !type.kind_of? Integer + raise Errno::EPROTONOSUPPORT, type.inspect end FFI::MemoryPointer.new :int, 2 do |mp| From 477e3ecab06c873a70f53eb219ef2d9c71c1d125 Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Fri, 16 Nov 2012 11:45:50 -0500 Subject: [PATCH 4/4] Add spec for Socket#socketpair on X18 --- spec/ruby/library/socket/shared/socketpair.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/ruby/library/socket/shared/socketpair.rb b/spec/ruby/library/socket/shared/socketpair.rb index 8ac8416cb2..21fa710d70 100644 --- a/spec/ruby/library/socket/shared/socketpair.rb +++ b/spec/ruby/library/socket/shared/socketpair.rb @@ -20,4 +20,10 @@ end end end + + ruby_version_is ""..."1.9" do + it "raises Errno::EPROTONOSUPPORT if socket type is not a String or Integer" do + lambda { Socket.socketpair(Socket::AF_UNIX, :DGRAM, 0) }.should raise_error(Errno::EPROTONOSUPPORT) + end + end end