Skip to content

Commit 4516fc1

Browse files
eregonandrykonchin
authored andcommitted
[GR-18163] Properly handle argument Socket::INADDR_ANY in Socket.sockaddr_in (#3364)
PullRequest: truffleruby/4100
2 parents c4e65f6 + 88e9b2c commit 4516fc1

File tree

5 files changed

+11
-4
lines changed

5 files changed

+11
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Compatibility:
2222
* Promote `File#path` and `File#to_path` to `IO#path` and `IO#to_path` and make IO#new accept an optional `path:` keyword argument (#3039, @moste00)
2323
* Display "unhandled exception" as the message for `RuntimeError` instances with an empty message (#3255, @nirvdrum).
2424
* Set `RbConfig::CONFIG['configure_args']` for openssl and libyaml (#3170, #3303, @eregon).
25+
* Support `Socket.sockaddr_in(port, Socket::INADDR_ANY)` (#3361, @mtortonesi).
2526

2627
Performance:
2728

lib/truffle/socket/truffle/foreign.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,22 @@ def self.pack_sockaddr_in(host, port, family = ::Socket::AF_UNSPEC,
266266
# to succeed on some platforms (most notably, Solaris).
267267
Integer(port)
268268
type = ::Socket::SOCK_DGRAM
269-
rescue ArgumentError
269+
rescue ArgumentError, TypeError
270270
# Ignored.
271271
end
272272
end
273273

274+
if Primitive.nil?(port)
275+
port = 0
276+
end
277+
274278
hints = Addrinfo.new
275279

276280
hints[:ai_family] = family
277281
hints[:ai_socktype] = type
278282
hints[:ai_flags] = flags
279283

280-
if host and host.empty?
284+
if host == ::Socket::INADDR_ANY or (host and host.empty?)
281285
host = '0.0.0.0'
282286
end
283287

spec/ruby/library/socket/shared/pack_sockaddr.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
sockaddr_in = Socket.public_send(@method, nil, '127.0.0.1')
1919
Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '127.0.0.1']
20+
21+
sockaddr_in = Socket.public_send(@method, 80, Socket::INADDR_ANY)
22+
Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '0.0.0.0']
2023
end
2124

2225
platform_is_not :solaris do

spec/ruby/library/socket/socket/pack_sockaddr_in_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
require_relative '../fixtures/classes'
33
require_relative '../shared/pack_sockaddr'
44

5-
describe "Socket#pack_sockaddr_in" do
5+
describe "Socket.pack_sockaddr_in" do
66
it_behaves_like :socket_pack_sockaddr_in, :pack_sockaddr_in
77
end

spec/tags/library/socket/socket/pack_sockaddr_in_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)