network_topology_strategy: validate integers

In order to prevent users from creating a network topology
strategy instance with invalid inputs, it's not enough to use
std::stol() on the input: a string "3abc" still returns the number '3',
but will later confuse cqlsh and other drivers, when they ask for
topology strategy details.
The error message is now more human readable, since for incorrect
numeric inputs it used to return a rather cryptic message:
    ServerError: stol()
This commit fixes the issue and comes with a simple test.

Fixes #3801
Tests: unit(dev)
Message-Id: <7aaae83d003738f047d28727430ca0a5cec6b9c6.1583478000.git.sarna@scylladb.com>
This commit is contained in:
Piotr Sarna
2020-03-06 08:00:34 +01:00
committed by Pekka Enberg
parent 30d2826358
commit 5b7a35e02b
2 changed files with 14 additions and 0 deletions

View File

@@ -85,6 +85,10 @@ network_topology_strategy::network_topology_strategy(
"NetworkTopologyStrategy");
}
if (val.empty() || std::any_of(val.begin(), val.end(), [] (char c) {return !isdigit(c);})) {
throw exceptions::configuration_exception(
format("Replication factor must be numeric and non-negative, found '{}'", val));
}
_dc_rep_factor.emplace(key, std::stol(val));
_datacenteres.push_back(key);
}

View File

@@ -37,6 +37,7 @@
#include <boost/range/algorithm/adjacent_find.hpp>
#include <boost/algorithm/cxx11/iota.hpp>
#include "test/lib/log.hh"
#include "test/lib/cql_test_env.hh"
using namespace locator;
@@ -597,4 +598,13 @@ SEASTAR_TEST_CASE(testCalculateEndpoints) {
});
}
SEASTAR_TEST_CASE(test_invalid_dcs) {
return do_with_cql_env_thread([] (auto& e) {
for (auto& incorrect : std::vector<std::string>{"3\"", "", "!!!", "abcb", "!3", "-5", "0x123"}) {
BOOST_REQUIRE_THROW(e.execute_cql("CREATE KEYSPACE abc WITH REPLICATION "
"= {'class': 'NetworkTopologyStrategy', 'dc1':'" + incorrect + "'}").get(),
exceptions::configuration_exception);
};
});
}