Do not allow the reading of floats as integers and vice versa (#387)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-03-16 16:25:45 +08:00
committed by GitHub
parent c9482719f6
commit c929d3fe0d
17 changed files with 98 additions and 45 deletions
+19 -13
View File
@@ -85,8 +85,7 @@ struct Reader {
avro_value_get_boolean(_var.val_, &result);
return (result != 0);
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (type == AVRO_DOUBLE) {
double result = 0.0;
const auto err = avro_value_get_double(_var.val_, &result);
@@ -94,7 +93,21 @@ struct Reader {
return error("Could not cast to double.");
}
return static_cast<T>(result);
} else if (type == AVRO_INT32) {
} else if (type == AVRO_FLOAT) {
float result = 0.0;
const auto err = avro_value_get_float(_var.val_, &result);
if (err) {
return error("Could not cast to float.");
}
return static_cast<T>(result);
} else {
return rfl::error(
"Could not cast to numeric value. The type must be float "
"or double.");
}
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (type == AVRO_INT32) {
int32_t result = 0;
const auto err = avro_value_get_int(_var.val_, &result);
if (err) {
@@ -108,17 +121,10 @@ struct Reader {
return error("Could not cast to int64.");
}
return static_cast<T>(result);
} else if (type == AVRO_FLOAT) {
float result = 0.0;
const auto err = avro_value_get_float(_var.val_, &result);
if (err) {
return error("Could not cast to float.");
}
return static_cast<T>(result);
} else {
return rfl::error(
"Could not cast to numeric value. The type must be integral.");
}
return rfl::error(
"Could not cast to numeric value. The type must be integral, float "
"or double.");
} else if constexpr (internal::is_literal_v<T>) {
int value = 0;
+14 -6
View File
@@ -110,6 +110,7 @@ struct Reader {
return error(
"Could not cast to string. The type must be UTF8 or symbol.");
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (btype != BSON_TYPE_BINARY) {
@@ -123,17 +124,23 @@ struct Reader {
const auto data =
internal::ptr_cast<const std::byte*>(value.v_binary.data);
return rfl::Bytestring(data, data + value.v_binary.data_len);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (btype != BSON_TYPE_BOOL) {
return error("Could not cast to boolean.");
}
return value.v_bool;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
switch (btype) {
case BSON_TYPE_DOUBLE:
return static_cast<T>(value.v_double);
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (btype != BSON_TYPE_DOUBLE) {
return error(
"Could not cast to numeric value. The type must be double, "
"int32, int64 or date_time.");
}
return static_cast<T>(value.v_double);
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
switch (btype) {
case BSON_TYPE_INT32:
return static_cast<T>(value.v_int32);
@@ -145,9 +152,10 @@ struct Reader {
default:
return error(
"Could not cast to numeric value. The type must be double, "
"Could not cast to numeric value. The type must be "
"int32, int64 or date_time.");
}
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bson_oid_t>()) {
if (btype != BSON_TYPE_OID) {
return error("Could not cast to OID.");
+8 -8
View File
@@ -78,8 +78,13 @@ class Reader {
}
return _var.val_.as<bool>();
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (type != capnp::DynamicValue::FLOAT) {
return error("Could not cast to float.");
}
return static_cast<T>(_var.val_.as<double>());
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
switch (type) {
case capnp::DynamicValue::INT:
return static_cast<T>(_var.val_.as<int64_t>());
@@ -87,13 +92,8 @@ class Reader {
case capnp::DynamicValue::UINT:
return static_cast<T>(_var.val_.as<uint64_t>());
case capnp::DynamicValue::FLOAT:
return static_cast<T>(_var.val_.as<double>());
default:
return error(
"Could not cast to numeric value. The type must be integral, "
"float or double.");
return error("Could not cast to an integer.");
}
} else if constexpr (internal::is_literal_v<T>) {
+9 -5
View File
@@ -71,6 +71,7 @@ class Reader {
return error("Could not cast to string.");
}
return _var.val_->as<std::string>();
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (!_var.val_->is_byte_string()) {
@@ -79,25 +80,28 @@ class Reader {
const auto vec = _var.val_->as<std::vector<uint8_t>>();
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
return rfl::Bytestring(data, data + vec.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.val_->is_bool()) {
return error("Could not cast to boolean.");
}
return _var.val_->as<bool>();
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (_var.val_->is_double()) {
return static_cast<T>(_var.val_->as<double>());
}
return error("Could not cast to double.");
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (_var.val_->is_int64()) {
return static_cast<T>(_var.val_->as<int64_t>());
}
if (_var.val_->is_uint64()) {
return static_cast<T>(_var.val_->as<uint64_t>());
}
return error(
"Could not cast to numeric value. The type must be integral, "
"float or double.");
return error("Could not cast to integer.");
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
+7 -2
View File
@@ -74,6 +74,7 @@ struct Reader {
return error("Could not cast to a string.");
}
return std::string(_var.AsString().c_str());
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (!_var.IsBlob()) {
@@ -82,21 +83,25 @@ struct Reader {
const auto blob = _var.AsBlob();
const auto data = internal::ptr_cast<const std::byte*>(blob.data());
return rfl::Bytestring(data, data + blob.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.IsBool()) {
return error("Could not cast to boolean.");
}
return _var.AsBool();
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (!_var.IsNumeric()) {
if (!_var.IsFloat()) {
return error("Could not cast to double.");
}
return static_cast<T>(_var.AsDouble());
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (!_var.IsNumeric()) {
if (!_var.IsIntOrUint()) {
return error("Could not cast to int.");
}
return static_cast<T>(_var.AsInt64());
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
+5
View File
@@ -109,26 +109,31 @@ struct Reader {
return error("Could not cast to string.");
}
return std::string(r);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!yyjson_is_bool(_var.val_)) {
return error("Could not cast to boolean.");
}
return yyjson_get_bool(_var.val_);
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (!yyjson_is_num(_var.val_)) {
return error("Could not cast to double.");
}
return static_cast<T>(yyjson_get_num(_var.val_));
} else if constexpr (std::is_unsigned<std::remove_cvref_t<T>>()) {
if (!yyjson_is_int(_var.val_)) {
return error("Could not cast to int.");
}
return static_cast<T>(yyjson_get_uint(_var.val_));
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (!yyjson_is_int(_var.val_)) {
return error("Could not cast to int.");
}
return static_cast<T>(yyjson_get_sint(_var.val_));
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
+4
View File
@@ -64,6 +64,7 @@ struct Reader {
}
const auto str = _var.via.str;
return std::string(str.ptr, str.size);
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (type != MSGPACK_OBJECT_BIN) {
@@ -72,11 +73,13 @@ struct Reader {
const auto bin = _var.via.bin;
const auto data = internal::ptr_cast<const std::byte*>(bin.ptr);
return rfl::Bytestring(data, data + bin.size);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (type != MSGPACK_OBJECT_BOOLEAN) {
return error("Could not cast to boolean.");
}
return _var.via.boolean;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (type == MSGPACK_OBJECT_FLOAT32 || type == MSGPACK_OBJECT_FLOAT64 ||
type == MSGPACK_OBJECT_FLOAT) {
@@ -85,6 +88,7 @@ struct Reader {
return error(
"Could not cast to numeric value. The type must be float "
"or double.");
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
return static_cast<T>(_var.via.u64);
+4
View File
@@ -60,21 +60,25 @@ struct Reader {
return error("Could not cast to std::string!");
}
return _var->as_string();
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var->is_boolean()) {
return error("Could not cast to bool!");
}
return _var->as_boolean();
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (!_var->is_floating()) {
return error("Could not cast to double!");
}
return static_cast<std::remove_cvref_t<T>>(_var->as_floating());
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (!_var->is_integer()) {
return error("Could not cast to int64_t!");
}
return static_cast<std::remove_cvref_t<T>>(_var->as_integer());
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
+9 -5
View File
@@ -71,6 +71,7 @@ class Reader {
return error("Could not cast to string.");
}
return _var.val_->as<std::string>();
} else if constexpr (std::is_same<std::remove_cvref_t<T>,
rfl::Bytestring>()) {
if (!_var.val_->is<std::vector<uint8_t>>()) {
@@ -79,25 +80,28 @@ class Reader {
const auto vec = _var.val_->as<std::vector<uint8_t>>();
const auto data = internal::ptr_cast<const std::byte*>(vec.data());
return rfl::Bytestring(data, data + vec.size());
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
if (!_var.val_->is_bool()) {
return error("Could not cast to boolean.");
}
return _var.val_->as<bool>();
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() ||
std::is_integral<std::remove_cvref_t<T>>()) {
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
if (_var.val_->is_double()) {
return static_cast<T>(_var.val_->as<double>());
}
return error("Could not cast to floating point value.");
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
if (_var.val_->is_int64()) {
return static_cast<T>(_var.val_->as<int64_t>());
}
if (_var.val_->is_uint64()) {
return static_cast<T>(_var.val_->as<uint64_t>());
}
return error(
"Could not cast to numeric value. The type must be integral, "
"float or double.");
return error("Could not cast to integer.");
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
+4
View File
@@ -83,8 +83,10 @@ struct Reader {
if constexpr (std::is_same<std::remove_cvref_t<T>, std::string>()) {
return std::visit(get_value, _var.node_or_attribute_);
} else if constexpr (std::is_same<std::remove_cvref_t<T>, bool>()) {
return std::visit(get_value, _var.node_or_attribute_) == "true";
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
const auto str = std::visit(get_value, _var.node_or_attribute_);
try {
@@ -93,6 +95,7 @@ struct Reader {
return error("Could not cast '" + std::string(str) +
"' to floating point value.");
}
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
const auto str = std::visit(get_value, _var.node_or_attribute_);
try {
@@ -100,6 +103,7 @@ struct Reader {
} catch (std::exception& e) {
return error("Could not cast '" + std::string(str) + "' to integer.");
}
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
+3
View File
@@ -76,11 +76,14 @@ struct Reader {
std::is_same<std::remove_cvref_t<T>, bool>() ||
std::is_floating_point<std::remove_cvref_t<T>>()) {
return _var.node_.as<std::remove_cvref_t<T>>();
} else if constexpr (std::is_integral<std::remove_cvref_t<T>>()) {
return static_cast<T>(_var.node_.as<std::remove_cvref_t<int64_t>>());
} else {
static_assert(rfl::always_false_v<T>, "Unsupported type.");
}
} catch (std::exception& e) {
return error(e.what());
}
+8
View File
@@ -12,10 +12,18 @@ if (REFLECTCPP_JSON)
add_subdirectory(json_c_arrays_and_inheritance)
endif ()
if (REFLECTCPP_AVRO)
add_subdirectory(avro)
endif()
if (REFLECTCPP_BSON)
add_subdirectory(bson)
endif()
if (REFLECTCPP_CAPNPROTO)
add_subdirectory(capnproto)
endif()
if (REFLECTCPP_CBOR)
add_subdirectory(cbor)
endif()
+1 -1
View File
@@ -48,7 +48,7 @@ To run the tests, do the following:
./build/tests/msgpack/reflect-cpp-msgpack-tests
./build/tests/json/reflect-cpp-json-tests
./build/tests/toml/reflect-cpp-toml-tests
./build/tests/toml/reflect-cpp-ubjson-tests
./build/tests/ubjson/reflect-cpp-ubjson-tests
./build/tests/xml/reflect-cpp-xml-tests
./build/tests/yaml/reflect-cpp-yaml-tests
```
+1 -1
View File
@@ -39,7 +39,7 @@ TEST(avro, test_tutorial_example) {
const auto serialized1 = rfl::avro::write(person, schema.value());
const auto res = rfl::avro::read<Person>(serialized1, schema.value());
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().value().what();
<< res.error().what();
const auto serialized2 = rfl::avro::write(res.value(), schema.value());
EXPECT_EQ(serialized1, serialized2);
}
+1 -1
View File
@@ -13,7 +13,7 @@ void write_and_read(const auto& _struct) {
const auto serialized1 = rfl::avro::write<Ps...>(_struct);
const auto res = rfl::avro::read<T, Ps...>(serialized1);
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().value().what();
<< res.error().what();
const auto serialized2 = rfl::avro::write<Ps...>(res.value());
EXPECT_EQ(serialized1, serialized2);
}
+1 -1
View File
@@ -13,7 +13,7 @@ void write_and_read(const auto& _struct) {
const auto serialized1 = rfl::capnproto::write<Ps...>(_struct);
const auto res = rfl::capnproto::read<T, Ps...>(serialized1);
EXPECT_TRUE(res && true) << "Test failed on read. Error: "
<< res.error().value().what();
<< res.error().what();
const auto serialized2 = rfl::capnproto::write<Ps...>(res.value());
EXPECT_EQ(serialized1, serialized2);
}
-2
View File
@@ -65,8 +65,6 @@ TEST(json, test_json_schema4) {
const auto json_schema =
rfl::json::to_schema<Person, rfl::DefaultIfMissing>();
std::cout << json_schema << std::endl;
const std::string expected =
R"({"$schema":"https://json-schema.org/draft/2020-12/schema","$ref":"#/definitions/test_json_schema__Person","definitions":{"rfl__Generic":{"anyOf":[{"anyOf":[{"type":"boolean"},{"type":"integer"},{"type":"number"},{"type":"string"},{"type":"object","additionalProperties":{"$ref":"#/definitions/rfl__Generic"}},{"type":"array","items":{"$ref":"#/definitions/rfl__Generic"}}]},{"type":"null"}]},"test_json_schema__Circle":{"type":"object","properties":{"radius":{"type":"number"}},"required":[]},"test_json_schema__Circle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Circle"]},"radius":{"type":"number"}},"required":[]},"test_json_schema__Person":{"type":"object","properties":{"firstName":{"type":"string"},"lastName":{"type":"string"},"email":{"type":"string","description":"Must be a proper email in the form xxx@xxx.xxx.","pattern":"^[a-zA-Z0-9._%+\\-]+@[a-zA-Z0-9.\\-]+\\.[a-zA-Z]{2,}$"},"town":{"type":"string"},"color":{"type":"string","enum":["red","green","blue"]},"age":{"allOf":[{"minimum":0,"type":"integer"},{"maximum":130,"type":"integer"}]},"salary":{"type":"number"},"children":{"type":"array","description":"The person's children. Pass an empty array for no children.","items":{"$ref":"#/definitions/test_json_schema__Person"}},"variant":{"anyOf":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}]},"tuple":{"type":"array","prefixItems":[{"type":"string","enum":["red","green","blue"]},{"type":"array","items":{"$ref":"#/definitions/test_json_schema__Person"}},{"type":"integer"}],"items":false},"taggedUnion":{"anyOf":[{"$ref":"#/definitions/test_json_schema__Rectangle__tagged"},{"$ref":"#/definitions/test_json_schema__Square__tagged"},{"$ref":"#/definitions/test_json_schema__Circle__tagged"}]},"fieldVariant":{"anyOf":[{"type":"object","properties":{"rectangle":{"$ref":"#/definitions/test_json_schema__Rectangle"}},"required":[]},{"type":"object","properties":{"square":{"$ref":"#/definitions/test_json_schema__Square"}},"required":[]},{"type":"object","properties":{"circle":{"$ref":"#/definitions/test_json_schema__Circle"}},"required":[]}]},"generic":{"$ref":"#/definitions/rfl__Generic"},"with_extra_fields":{"$ref":"#/definitions/test_json_schema__WithExtraFields"}},"required":[]},"test_json_schema__Rectangle":{"type":"object","properties":{"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema__Rectangle__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Rectangle"]},"width":{"type":"number"},"height":{"type":"number"}},"required":[]},"test_json_schema__Square":{"type":"object","properties":{"width":{"type":"number"}},"required":[]},"test_json_schema__Square__tagged":{"type":"object","properties":{"shape":{"type":"string","enum":["Square"]},"width":{"type":"number"}},"required":[]},"test_json_schema__WithExtraFields":{"type":"object","properties":{"f1":{"type":"string"},"f2":{"type":"string"}},"required":[],"additionalProperties":{"type":"string"}}}})";