core: Network: fix uncaught bad_lexical_cast in isValidCidr()
Change-Id: Ibeac1bb5d637324e880f00a0ae0ff9e65506efd4
Refs: #3858
diff --git a/core/network.cpp b/core/network.cpp
index be7d9c6..3360e62 100644
--- a/core/network.cpp
+++ b/core/network.cpp
@@ -25,9 +25,8 @@
#include "network.hpp"
-#include <boost/algorithm/string/classification.hpp>
-#include <boost/algorithm/string/split.hpp>
#include <boost/utility/value_init.hpp>
+#include <algorithm>
namespace nfd {
@@ -61,24 +60,28 @@
bool
Network::isValidCidr(const std::string& cidr)
{
- std::vector<std::string> splitCidr;
- boost::split(splitCidr, cidr, boost::is_any_of("/"));
- if (splitCidr.size() != 2) {
+ auto pos = cidr.find('/');
+ if (pos == std::string::npos) {
return false;
}
boost::system::error_code invalidIp;
- boost::asio::ip::address_v4::from_string(splitCidr[0], invalidIp);
+ boost::asio::ip::address_v4::from_string(cidr.substr(0, pos), invalidIp);
if (invalidIp) {
return false;
}
- auto prefixLenStr = splitCidr[1];
- if (prefixLenStr.length() <= 0 ||
- !std::all_of(prefixLenStr.begin(), prefixLenStr.end(), ::isdigit)) {
+ auto prefixLenStr = cidr.substr(pos + 1);
+ if (!std::all_of(prefixLenStr.begin(), prefixLenStr.end(), ::isdigit)) {
return false;
}
- int prefixLen = boost::lexical_cast<int>(prefixLenStr);
+ int prefixLen = -1;
+ try {
+ prefixLen = boost::lexical_cast<int>(prefixLenStr);
+ }
+ catch (const boost::bad_lexical_cast&) {
+ return false;
+ }
if (prefixLen < 0 || prefixLen > 32) {
return false;
}
diff --git a/tests/core/network.t.cpp b/tests/core/network.t.cpp
index 7f9bcd9..76555a8 100644
--- a/tests/core/network.t.cpp
+++ b/tests/core/network.t.cpp
@@ -160,7 +160,7 @@
BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/24.42"), false);
BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/foo"), false);
BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/33"), false);
- //BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/999999999999999"), false); // #3858
+ BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/999999999999999"), false);
BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0/"), false);
BOOST_CHECK_EQUAL(Network::isValidCidr("192.0.0.0"), false);
BOOST_CHECK_EQUAL(Network::isValidCidr("foo/4"), false);