diff --git a/AK/IPv6Address.h b/AK/IPv6Address.h index 34bcb646e6b..a9fbef9f4a0 100644 --- a/AK/IPv6Address.h +++ b/AK/IPv6Address.h @@ -16,7 +16,7 @@ # include # include #else -# include +# include #endif #include #include @@ -51,25 +51,24 @@ public: #ifdef KERNEL ErrorOr> to_string() const #else - DeprecatedString to_deprecated_string() const + ErrorOr to_string() const #endif { if (is_zero()) { #ifdef KERNEL return Kernel::KString::try_create("::"sv); #else - return "::"sv; + return "::"_short_string; #endif } - // TODO: Error propagation StringBuilder builder; if (is_ipv4_mapped()) { #ifdef KERNEL return Kernel::KString::formatted("::ffff:{}.{}.{}.{}", m_data[12], m_data[13], m_data[14], m_data[15]); #else - return DeprecatedString::formatted("::ffff:{}.{}.{}.{}", m_data[12], m_data[13], m_data[14], m_data[15]); + return String::formatted("::ffff:{}.{}.{}.{}", m_data[12], m_data[13], m_data[14], m_data[15]); #endif } @@ -99,24 +98,24 @@ public: for (int i = 0; i < 8;) { if (longest_zero_span_start.has_value() && longest_zero_span_start.value() == i) { if (longest_zero_span_start.value() + zero_span_length >= 8) - builder.append("::"sv); + TRY(builder.try_append("::"sv)); else - builder.append(':'); + TRY(builder.try_append(':')); i += zero_span_length; continue; } if (i == 0) - builder.appendff("{:x}", group(i)); + TRY(builder.try_appendff("{:x}", group(i))); else - builder.appendff(":{:x}", group(i)); + TRY(builder.try_appendff(":{:x}", group(i))); i++; } #ifdef KERNEL return Kernel::KString::try_create(builder.string_view()); #else - return builder.string_view(); + return builder.to_string(); #endif } @@ -298,7 +297,7 @@ template<> struct Formatter : Formatter { ErrorOr format(FormatBuilder& builder, IPv6Address const& value) { - return Formatter::format(builder, value.to_deprecated_string()); + return Formatter::format(builder, TRY(value.to_string())); } }; #endif diff --git a/Tests/AK/TestIPv6Address.cpp b/Tests/AK/TestIPv6Address.cpp index 250d225762e..41073241647 100644 --- a/Tests/AK/TestIPv6Address.cpp +++ b/Tests/AK/TestIPv6Address.cpp @@ -55,14 +55,18 @@ TEST_CASE(should_get_groups_by_index) TEST_CASE(should_convert_to_string) { - EXPECT_EQ("102:304:506:708:90a:b0c:d0e:f10"sv, IPv6Address({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }).to_deprecated_string()); - EXPECT_EQ("::"sv, IPv6Address().to_deprecated_string()); - EXPECT_EQ("::1"sv, IPv6Address({ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 }).to_deprecated_string()); - EXPECT_EQ("1::"sv, IPv6Address({ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }).to_deprecated_string()); - EXPECT_EQ("102:0:506:708:900::10"sv, IPv6Address({ 1, 2, 0, 0, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 16 }).to_deprecated_string()); - EXPECT_EQ("102:0:506:708:900::"sv, IPv6Address({ 1, 2, 0, 0, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 }).to_deprecated_string()); - EXPECT_EQ("::304:506:708:90a:b0c:d0e:f10"sv, IPv6Address({ 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }).to_deprecated_string()); - EXPECT_EQ("102:304::708:90a:b0c:d0e:f10"sv, IPv6Address({ 1, 2, 3, 4, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }).to_deprecated_string()); + auto to_string = [&](IPv6Address::in6_addr_t const& data) -> String { + return MUST(IPv6Address(data).to_string()); + }; + + EXPECT_EQ("102:304:506:708:90a:b0c:d0e:f10"sv, to_string({ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 })); + EXPECT_EQ("::"sv, MUST(IPv6Address().to_string())); + EXPECT_EQ("::1"sv, to_string({ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 })); + EXPECT_EQ("1::"sv, to_string({ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 })); + EXPECT_EQ("102:0:506:708:900::10"sv, to_string({ 1, 2, 0, 0, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 16 })); + EXPECT_EQ("102:0:506:708:900::"sv, to_string({ 1, 2, 0, 0, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0 })); + EXPECT_EQ("::304:506:708:90a:b0c:d0e:f10"sv, to_string({ 0, 0, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 })); + EXPECT_EQ("102:304::708:90a:b0c:d0e:f10"sv, to_string({ 1, 2, 3, 4, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 })); } TEST_CASE(should_make_ipv6_address_from_string) @@ -86,7 +90,7 @@ TEST_CASE(ipv4_mapped_ipv6) IPv6Address mapped_address(ipv4_address_to_map); EXPECT(mapped_address.is_ipv4_mapped()); EXPECT_EQ(ipv4_address_to_map, mapped_address.ipv4_mapped_address().value()); - EXPECT_EQ("::ffff:192.168.0.1"sv, mapped_address.to_deprecated_string()); + EXPECT_EQ("::ffff:192.168.0.1"sv, MUST(mapped_address.to_string())); EXPECT_EQ(IPv4Address(192, 168, 1, 9), IPv6Address::from_string("::FFFF:192.168.1.9"sv).value().ipv4_mapped_address().value()); EXPECT(!IPv6Address::from_string("::abcd:192.168.1.9"sv).has_value()); } diff --git a/Userland/Libraries/LibC/arpa/inet.cpp b/Userland/Libraries/LibC/arpa/inet.cpp index b404bd968d6..028d67ef2f3 100644 --- a/Userland/Libraries/LibC/arpa/inet.cpp +++ b/Userland/Libraries/LibC/arpa/inet.cpp @@ -27,8 +27,13 @@ char const* inet_ntop(int af, void const* src, char* dst, socklen_t len) errno = ENOSPC; return nullptr; } - auto str = IPv6Address(((in6_addr const*)src)->s6_addr).to_deprecated_string(); - if (!str.copy_characters_to_buffer(dst, len)) { + auto str_or_error = IPv6Address(((in6_addr const*)src)->s6_addr).to_string(); + if (str_or_error.is_error()) { + errno = ENOMEM; + return nullptr; + } + auto str = str_or_error.release_value(); + if (!str.bytes_as_string_view().copy_characters_to_buffer(dst, len)) { errno = ENOSPC; return nullptr; }