Validate ascii values when creating from CQL

Although the code for it existed already, the validation function
hasn't been invoked properly. This change fixes that, adding
a validating check when converting from text to specific value
type and throwing a marshal exception if some characters
are not ASCII.

Fixes #5421

Closes #7532
This commit is contained in:
Piotr Wojtczak
2020-11-02 13:39:02 +01:00
committed by Avi Kivity
parent 364ddab148
commit caa3c471c0
2 changed files with 15 additions and 0 deletions

View File

@@ -100,6 +100,13 @@ BOOST_AUTO_TEST_CASE(test_byte_type_string_conversions) {
BOOST_REQUIRE_EQUAL(byte_type->to_string(bytes()), "");
}
BOOST_AUTO_TEST_CASE(test_ascii_type_string_conversions) {
BOOST_REQUIRE(ascii_type->equal(ascii_type->from_string("ascii"), ascii_type->decompose("ascii")));
BOOST_REQUIRE_EQUAL(ascii_type->to_string(ascii_type->decompose("ascii")), "ascii");
test_parsing_fails(ascii_type, "¡Hola!");
}
BOOST_AUTO_TEST_CASE(test_short_type_string_conversions) {
BOOST_REQUIRE(short_type->equal(short_type->from_string("12345"), short_type->decompose(int16_t(12345))));
BOOST_REQUIRE_EQUAL(short_type->to_string(short_type->decompose(int16_t(12345))), "12345");

View File

@@ -2360,6 +2360,14 @@ struct from_string_visitor {
sstring_view s;
bytes operator()(const reversed_type_impl& r) { return r.underlying_type()->from_string(s); }
template <typename T> bytes operator()(const integer_type_impl<T>& t) { return decompose_value(parse_int(t, s)); }
bytes operator()(const ascii_type_impl&) {
auto bv = bytes_view(reinterpret_cast<const int8_t*>(s.begin()), s.size());
if (utils::ascii::validate(bv)) {
return to_bytes(bv);
} else {
throw marshal_exception(format("Value not compatible with type {}: '{}'", ascii_type_name, s));
}
}
bytes operator()(const string_type_impl&) {
return to_bytes(bytes_view(reinterpret_cast<const int8_t*>(s.begin()), s.size()));
}