diff options
| author | Kasumi Hanazuki <kasumi@rollingapple.net> | 2023-11-27 04:15:48 +0000 |
|---|---|---|
| committer | Hiroshi SHIBATA <hsbt@ruby-lang.org> | 2023-12-25 21:12:49 +0900 |
| commit | eb53131367d6e1d3da16b0cd6bc44284ea3589ea (patch) | |
| tree | 1b969044254962728adbaa1fcc08f92f16b83170 /lib | |
| parent | ea5776e7e4262c915f7f8b8884af420dd87d769c (diff) | |
[ruby/ipaddr] ntop: Measure address size in bytes
`IPAddr.ntop` takes the binary representation of an IP address, whose
length should be 4 or 16 *bytes* (not characters/codepoints).
The current implementation accepts strings in any encoding, but for
some values in non-BINARY encoding, it fails proper length check and
raises an `AddressFamilyError`. Since passing strings in a multibyte
encoding has never worked correctly for years, this patch makes it an
explicit error with an `InvalidAddressError`.
Fixes: https://github.com/ruby/ipaddr/issues/56
https://github.com/ruby/ipaddr/commit/a33fd14d4a
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/ipaddr.rb | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/lib/ipaddr.rb b/lib/ipaddr.rb index 03e1c18baa..07ef67a942 100644 --- a/lib/ipaddr.rb +++ b/lib/ipaddr.rb @@ -110,8 +110,13 @@ class IPAddr # Convert a network byte ordered string form of an IP address into # human readable form. + # It expects the string to be encoded in Encoding::ASCII_8BIT (BINARY). def self.ntop(addr) - case addr.size + if addr.is_a?(String) && addr.encoding != Encoding::BINARY + raise InvalidAddressError, "invalid encoding (given #{addr.encoding}, expected BINARY)" + end + + case addr.bytesize when 4 addr.unpack('C4').join('.') when 16 |
