From 299b33bc43269c06256de47679bd84d2d03def59 Mon Sep 17 00:00:00 2001 From: Chen He Date: Tue, 10 Sep 2024 17:51:05 -0400 Subject: [PATCH 1/2] Add support for parsing shortened ipv4 addresses such as "127.1" --- groups/nts/ntsa/ntsa_ipv4address.cpp | 31 ++++++++++++++++---------- groups/nts/ntsa/ntsa_ipv4address.t.cpp | 14 ++++++++++++ 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/groups/nts/ntsa/ntsa_ipv4address.cpp b/groups/nts/ntsa/ntsa_ipv4address.cpp index 87c5172c..c8aa2a7d 100644 --- a/groups/nts/ntsa/ntsa_ipv4address.cpp +++ b/groups/nts/ntsa/ntsa_ipv4address.cpp @@ -20,9 +20,12 @@ BSLS_IDENT_RCSID(ntsa_ipv4address_cpp, "$Id$ $CSID$") #include #include +#include #include +#include #include + namespace BloombergLP { namespace ntsa { @@ -90,22 +93,25 @@ bool Ipv4Address::parse(const bslstl::StringRef& text) NTSCFG_NOEXCEPT Representation newValue; newValue.d_asDword = 0; - bsl::uint32_t byteAtIndex = 0; + bsl::uint64_t numAtIndex = 0; while (current != end) { const char ch = *current; if (NTSCFG_LIKELY(ch >= '0' && ch <= '9')) { - byteAtIndex *= 10; - byteAtIndex += ch - '0'; + numAtIndex *= 10; + numAtIndex += ch - '0'; + if (numAtIndex > bsl::numeric_limits::max()) { + return false; + } } else { if (ch == '.') { - if (byteAtIndex > 255) { + if (numAtIndex > 255) { return false; } newValue.d_asBytes[index] = - NTSCFG_WARNING_NARROW(bsl::uint8_t, byteAtIndex); - byteAtIndex = 0; + NTSCFG_WARNING_NARROW(bsl::uint8_t, numAtIndex); + numAtIndex = 0; ++index; if (index > 3) { return false; @@ -119,17 +125,18 @@ bool Ipv4Address::parse(const bslstl::StringRef& text) NTSCFG_NOEXCEPT ++current; } - if (index != 3) { + bsl::uint64_t numOctetsLeft = 4 - index; + if (numAtIndex >= bsl::pow(256,numOctetsLeft)) { return false; } - if (byteAtIndex > 255) { - return false; + for (size_t oct = 0; oct < numOctetsLeft; ++oct) { + bsl::uint8_t octet = NTSCFG_WARNING_NARROW(bsl::uint8_t, + numAtIndex % 256); + newValue.d_asBytes[3 - oct] = octet; + numAtIndex /= 256; } - newValue.d_asBytes[index] = - NTSCFG_WARNING_NARROW(bsl::uint8_t, byteAtIndex); - d_value.d_asDword = newValue.d_asDword; return true; diff --git a/groups/nts/ntsa/ntsa_ipv4address.t.cpp b/groups/nts/ntsa/ntsa_ipv4address.t.cpp index e028ceb1..32f494b3 100644 --- a/groups/nts/ntsa/ntsa_ipv4address.t.cpp +++ b/groups/nts/ntsa/ntsa_ipv4address.t.cpp @@ -291,6 +291,20 @@ void Ipv4AddressTest::verifyParsing() { "x.2.3.4.5", {0x00, 0x00, 0x00, 0x00}, false}, { "256.256.256.256", {0x00, 0x00, 0x00, 0x00}, false}, {"9999.9999.9999.9999", {0x00, 0x00, 0x00, 0x00}, false}, + + { "127.1", {0x7F, 0x00, 0x00, 0x01}, true}, + { "127.168.257", {0x7F, 0xA8, 0x01, 0x01}, true}, + { "127.2.1", {0x7F, 0x02, 0x00, 0x01}, true}, + { "199.11315476", {0xC7, 0xAC, 0xA9, 0x14}, true}, + { "255.16777215", {0xFF, 0xFF, 0xFF, 0xFF}, true}, + { "255.255.65535", {0xFF, 0xFF, 0xFF, 0xFF}, true}, + { "1", {0x00, 0x00, 0x00, 0x01}, true}, + { "4294967295", {0xFF, 0xFF, 0xFF, 0xFF}, true}, + { "4294967296", {0x00, 0x00, 0x00, 0x00}, false}, + { "256.1", {0x00, 0x00, 0x00, 0x00}, false}, + { "255.16777216", {0x00, 0x00, 0x00, 0x00}, false}, + { "255.255.65536", {0xFF, 0xFF, 0xFF, 0xFF}, false}, + { "99999999999999999", {0x00, 0x00, 0x00, 0x00}, false}, }; enum { NUM_DATA = sizeof(DATA) / sizeof(DATA[0]) }; From 212c3d0e10a069870d9f5d7a68a5f77cead80f81 Mon Sep 17 00:00:00 2001 From: Chen He Date: Fri, 13 Sep 2024 10:16:57 -0400 Subject: [PATCH 2/2] Update groups/nts/ntsa/ntsa_ipv4address.cpp Co-authored-by: Alex Vasylenko --- groups/nts/ntsa/ntsa_ipv4address.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/groups/nts/ntsa/ntsa_ipv4address.cpp b/groups/nts/ntsa/ntsa_ipv4address.cpp index c8aa2a7d..119d1906 100644 --- a/groups/nts/ntsa/ntsa_ipv4address.cpp +++ b/groups/nts/ntsa/ntsa_ipv4address.cpp @@ -19,8 +19,8 @@ BSLS_IDENT_RCSID(ntsa_ipv4address_cpp, "$Id$ $CSID$") #include -#include #include +#include #include #include #include