Skip to content

Commit fab4c29

Browse files
author
MarcoFalke
committed
net: Reject + sign when parsing subnet mask
It does not make sense and it is rejected by other parsers as well: >>> ipaddress.ip_network("1.2.3.0/+24") ValueError: '1.2.3.0/+24' does not appear to be an IPv4 or IPv6 network
1 parent fa89652 commit fab4c29

File tree

2 files changed

+3
-4
lines changed

2 files changed

+3
-4
lines changed

src/netbase.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -829,10 +829,9 @@ CSubNet LookupSubNet(const std::string& subnet_str)
829829
addr = static_cast<CNetAddr>(MaybeFlipIPv6toCJDNS(CService{addr.value(), /*port=*/0}));
830830
if (slash_pos != subnet_str.npos) {
831831
const std::string netmask_str{subnet_str.substr(slash_pos + 1)};
832-
uint8_t netmask;
833-
if (ParseUInt8(netmask_str, &netmask)) {
832+
if (const auto netmask{ToIntegral<uint8_t>(netmask_str)}) {
834833
// Valid number; assume CIDR variable-length subnet masking.
835-
subnet = CSubNet{addr.value(), netmask};
834+
subnet = CSubNet{addr.value(), *netmask};
836835
} else {
837836
// Invalid number; try full netmask syntax. Never allow lookup for netmask.
838837
const std::optional<CNetAddr> full_netmask{LookupHost(netmask_str, /*fAllowLookup=*/false)};

src/test/netbase_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ BOOST_AUTO_TEST_CASE(embedded_test)
150150

151151
BOOST_AUTO_TEST_CASE(subnet_test)
152152
{
153-
154153
BOOST_CHECK(LookupSubNet("1.2.3.0/24") == LookupSubNet("1.2.3.0/255.255.255.0"));
155154
BOOST_CHECK(LookupSubNet("1.2.3.0/24") != LookupSubNet("1.2.4.0/255.255.255.0"));
156155
BOOST_CHECK(LookupSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
@@ -185,6 +184,7 @@ BOOST_AUTO_TEST_CASE(subnet_test)
185184
// Check valid/invalid
186185
BOOST_CHECK(LookupSubNet("1.2.3.0/0").IsValid());
187186
BOOST_CHECK(!LookupSubNet("1.2.3.0/-1").IsValid());
187+
BOOST_CHECK(!LookupSubNet("1.2.3.0/+24").IsValid());
188188
BOOST_CHECK(LookupSubNet("1.2.3.0/32").IsValid());
189189
BOOST_CHECK(!LookupSubNet("1.2.3.0/33").IsValid());
190190
BOOST_CHECK(!LookupSubNet("1.2.3.0/300").IsValid());

0 commit comments

Comments
 (0)