Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ntop: Measure address size in bytes #61

Merged
merged 1 commit into from Dec 23, 2023
Merged

Conversation

hanazuki
Copy link
Contributor

IPAddr.ntop takes the binary representation of an IP address, whose length should be 4 or 16 bytes (not characters/codepoints).

This patch fixes the bug that IPAddr.ntop doesn't handle Strings in a multibyte encoding correctly.

Fixes: #56

@hanazuki hanazuki requested a review from knu as a code owner November 27, 2023 07:43
Copy link
Member

@knu knu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPAddr.ntop() (naturally) expects a binary string, so in terms of user-friendliness, it would make sense to check the encoding and raise an EncodingError if the given string is not binary.

@hanazuki
Copy link
Contributor Author

Makes sense. I just though such a change would break compatibility, but actually the affected user codes have never worked correctly.

Copy link
Member

@knu knu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I think using bytesize here is technically correct, accepting a non-binary string is not what we want. Would you care to add encoding check?

@hanazuki
Copy link
Contributor Author

@knu I just updated the patch. Please take a look.

Copy link
Member

@knu knu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

test/test_ipaddr.rb Show resolved Hide resolved
@@ -111,7 +111,11 @@ def self.new_ntoh(addr)
# Convert a network byte ordered string form of an IP address into
# human readable form.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add something like this here? "It expects a string encoded in Encoding::ASCII_8BIT."

lib/ipaddr.rb Outdated
@@ -110,8 +110,13 @@ def self.new_ntoh(addr)

# Convert a network byte ordered string form of an IP address into
# human readable form.
# It expects the string to be encoded in Encoding::US_ASCII (BINARY).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Encoding::US_ASCII is different from ASCII_8BIT. US_ASCII is a 7-bit encoding where codes above 127 cannot be contained.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copy&pasted wrong string 😓

`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: ruby#56
@knu knu merged commit c08e874 into ruby:master Dec 23, 2023
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

unsupported address family
2 participants