Skip to content

Commit

Permalink
[GR-18163] Properly handle argument Socket::INADDR_ANY in Socket.sock…
Browse files Browse the repository at this point in the history
…addr_in (#3364)

PullRequest: truffleruby/4100
  • Loading branch information
eregon authored and andrykonchin committed Jan 8, 2024
2 parents c4e65f6 + 88e9b2c commit 4516fc1
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Compatibility:
* 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)
* Display "unhandled exception" as the message for `RuntimeError` instances with an empty message (#3255, @nirvdrum).
* Set `RbConfig::CONFIG['configure_args']` for openssl and libyaml (#3170, #3303, @eregon).
* Support `Socket.sockaddr_in(port, Socket::INADDR_ANY)` (#3361, @mtortonesi).

Performance:

Expand Down
8 changes: 6 additions & 2 deletions lib/truffle/socket/truffle/foreign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,22 @@ def self.pack_sockaddr_in(host, port, family = ::Socket::AF_UNSPEC,
# to succeed on some platforms (most notably, Solaris).
Integer(port)
type = ::Socket::SOCK_DGRAM
rescue ArgumentError
rescue ArgumentError, TypeError
# Ignored.
end
end

if Primitive.nil?(port)
port = 0
end

hints = Addrinfo.new

hints[:ai_family] = family
hints[:ai_socktype] = type
hints[:ai_flags] = flags

if host and host.empty?
if host == ::Socket::INADDR_ANY or (host and host.empty?)
host = '0.0.0.0'
end

Expand Down
3 changes: 3 additions & 0 deletions spec/ruby/library/socket/shared/pack_sockaddr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

sockaddr_in = Socket.public_send(@method, nil, '127.0.0.1')
Socket.unpack_sockaddr_in(sockaddr_in).should == [0, '127.0.0.1']

sockaddr_in = Socket.public_send(@method, 80, Socket::INADDR_ANY)
Socket.unpack_sockaddr_in(sockaddr_in).should == [80, '0.0.0.0']
end

platform_is_not :solaris do
Expand Down
2 changes: 1 addition & 1 deletion spec/ruby/library/socket/socket/pack_sockaddr_in_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
require_relative '../fixtures/classes'
require_relative '../shared/pack_sockaddr'

describe "Socket#pack_sockaddr_in" do
describe "Socket.pack_sockaddr_in" do
it_behaves_like :socket_pack_sockaddr_in, :pack_sockaddr_in
end
1 change: 0 additions & 1 deletion spec/tags/library/socket/socket/pack_sockaddr_in_tags.txt

This file was deleted.

0 comments on commit 4516fc1

Please sign in to comment.