From c929d3fe0d432d017e2bca041c562d2c65677f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Sun, 16 Mar 2025 16:25:45 +0800 Subject: [PATCH] Do not allow the reading of floats as integers and vice versa (#387) --- include/rfl/avro/Reader.hpp | 32 +++++++++++++++++----------- include/rfl/bson/Reader.hpp | 20 +++++++++++------ include/rfl/capnproto/Reader.hpp | 16 +++++++------- include/rfl/cbor/Reader.hpp | 14 +++++++----- include/rfl/flexbuf/Reader.hpp | 9 ++++++-- include/rfl/json/Reader.hpp | 5 +++++ include/rfl/msgpack/Reader.hpp | 4 ++++ include/rfl/toml/Reader.hpp | 4 ++++ include/rfl/ubjson/Reader.hpp | 14 +++++++----- include/rfl/xml/Reader.hpp | 4 ++++ include/rfl/yaml/Reader.hpp | 3 +++ tests/CMakeLists.txt | 8 +++++++ tests/README.md | 2 +- tests/avro/test_tutorial_example.cpp | 2 +- tests/avro/write_and_read.hpp | 2 +- tests/capnproto/write_and_read.hpp | 2 +- tests/json/test_json_schema4.cpp | 2 -- 17 files changed, 98 insertions(+), 45 deletions(-) diff --git a/include/rfl/avro/Reader.hpp b/include/rfl/avro/Reader.hpp index 1902949d..6a3b442a 100644 --- a/include/rfl/avro/Reader.hpp +++ b/include/rfl/avro/Reader.hpp @@ -85,8 +85,7 @@ struct Reader { avro_value_get_boolean(_var.val_, &result); return (result != 0); - } else if constexpr (std::is_floating_point>() || - std::is_integral>()) { + } else if constexpr (std::is_floating_point>()) { 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(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(result); + } else { + return rfl::error( + "Could not cast to numeric value. The type must be float " + "or double."); + } + + } else if constexpr (std::is_integral>()) { + 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(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(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) { int value = 0; diff --git a/include/rfl/bson/Reader.hpp b/include/rfl/bson/Reader.hpp index 3ec64de0..02de12ab 100644 --- a/include/rfl/bson/Reader.hpp +++ b/include/rfl/bson/Reader.hpp @@ -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, rfl::Bytestring>()) { if (btype != BSON_TYPE_BINARY) { @@ -123,17 +124,23 @@ struct Reader { const auto data = internal::ptr_cast(value.v_binary.data); return rfl::Bytestring(data, data + value.v_binary.data_len); + } else if constexpr (std::is_same, 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::is_integral>()) { - switch (btype) { - case BSON_TYPE_DOUBLE: - return static_cast(value.v_double); + } else if constexpr (std::is_floating_point>()) { + 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(value.v_double); + + } else if constexpr (std::is_integral>()) { + switch (btype) { case BSON_TYPE_INT32: return static_cast(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, bson_oid_t>()) { if (btype != BSON_TYPE_OID) { return error("Could not cast to OID."); diff --git a/include/rfl/capnproto/Reader.hpp b/include/rfl/capnproto/Reader.hpp index 3a115481..767eccf8 100644 --- a/include/rfl/capnproto/Reader.hpp +++ b/include/rfl/capnproto/Reader.hpp @@ -78,8 +78,13 @@ class Reader { } return _var.val_.as(); - } else if constexpr (std::is_floating_point>() || - std::is_integral>()) { + } else if constexpr (std::is_floating_point>()) { + if (type != capnp::DynamicValue::FLOAT) { + return error("Could not cast to float."); + } + return static_cast(_var.val_.as()); + + } else if constexpr (std::is_integral>()) { switch (type) { case capnp::DynamicValue::INT: return static_cast(_var.val_.as()); @@ -87,13 +92,8 @@ class Reader { case capnp::DynamicValue::UINT: return static_cast(_var.val_.as()); - case capnp::DynamicValue::FLOAT: - return static_cast(_var.val_.as()); - 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) { diff --git a/include/rfl/cbor/Reader.hpp b/include/rfl/cbor/Reader.hpp index 59e3d736..e3a40538 100644 --- a/include/rfl/cbor/Reader.hpp +++ b/include/rfl/cbor/Reader.hpp @@ -71,6 +71,7 @@ class Reader { return error("Could not cast to string."); } return _var.val_->as(); + } else if constexpr (std::is_same, rfl::Bytestring>()) { if (!_var.val_->is_byte_string()) { @@ -79,25 +80,28 @@ class Reader { const auto vec = _var.val_->as>(); const auto data = internal::ptr_cast(vec.data()); return rfl::Bytestring(data, data + vec.size()); + } else if constexpr (std::is_same, bool>()) { if (!_var.val_->is_bool()) { return error("Could not cast to boolean."); } return _var.val_->as(); - } else if constexpr (std::is_floating_point>() || - std::is_integral>()) { + + } else if constexpr (std::is_floating_point>()) { if (_var.val_->is_double()) { return static_cast(_var.val_->as()); } + return error("Could not cast to double."); + + } else if constexpr (std::is_integral>()) { if (_var.val_->is_int64()) { return static_cast(_var.val_->as()); } if (_var.val_->is_uint64()) { return static_cast(_var.val_->as()); } - 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, "Unsupported type."); } diff --git a/include/rfl/flexbuf/Reader.hpp b/include/rfl/flexbuf/Reader.hpp index b1946ca3..d23a3717 100644 --- a/include/rfl/flexbuf/Reader.hpp +++ b/include/rfl/flexbuf/Reader.hpp @@ -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, rfl::Bytestring>()) { if (!_var.IsBlob()) { @@ -82,21 +83,25 @@ struct Reader { const auto blob = _var.AsBlob(); const auto data = internal::ptr_cast(blob.data()); return rfl::Bytestring(data, data + blob.size()); + } else if constexpr (std::is_same, bool>()) { if (!_var.IsBool()) { return error("Could not cast to boolean."); } return _var.AsBool(); + } else if constexpr (std::is_floating_point>()) { - if (!_var.IsNumeric()) { + if (!_var.IsFloat()) { return error("Could not cast to double."); } return static_cast(_var.AsDouble()); + } else if constexpr (std::is_integral>()) { - if (!_var.IsNumeric()) { + if (!_var.IsIntOrUint()) { return error("Could not cast to int."); } return static_cast(_var.AsInt64()); + } else { static_assert(rfl::always_false_v, "Unsupported type."); } diff --git a/include/rfl/json/Reader.hpp b/include/rfl/json/Reader.hpp index 5cc032d7..4bcfbf45 100644 --- a/include/rfl/json/Reader.hpp +++ b/include/rfl/json/Reader.hpp @@ -109,26 +109,31 @@ struct Reader { return error("Could not cast to string."); } return std::string(r); + } else if constexpr (std::is_same, 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>()) { if (!yyjson_is_num(_var.val_)) { return error("Could not cast to double."); } return static_cast(yyjson_get_num(_var.val_)); + } else if constexpr (std::is_unsigned>()) { if (!yyjson_is_int(_var.val_)) { return error("Could not cast to int."); } return static_cast(yyjson_get_uint(_var.val_)); + } else if constexpr (std::is_integral>()) { if (!yyjson_is_int(_var.val_)) { return error("Could not cast to int."); } return static_cast(yyjson_get_sint(_var.val_)); + } else { static_assert(rfl::always_false_v, "Unsupported type."); } diff --git a/include/rfl/msgpack/Reader.hpp b/include/rfl/msgpack/Reader.hpp index 10f325b6..8e9039b7 100644 --- a/include/rfl/msgpack/Reader.hpp +++ b/include/rfl/msgpack/Reader.hpp @@ -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, 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(bin.ptr); return rfl::Bytestring(data, data + bin.size); + } else if constexpr (std::is_same, bool>()) { if (type != MSGPACK_OBJECT_BOOLEAN) { return error("Could not cast to boolean."); } return _var.via.boolean; + } else if constexpr (std::is_floating_point>()) { 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>()) { if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) { return static_cast(_var.via.u64); diff --git a/include/rfl/toml/Reader.hpp b/include/rfl/toml/Reader.hpp index 90d7ce27..a586a3a8 100644 --- a/include/rfl/toml/Reader.hpp +++ b/include/rfl/toml/Reader.hpp @@ -60,21 +60,25 @@ struct Reader { return error("Could not cast to std::string!"); } return _var->as_string(); + } else if constexpr (std::is_same, bool>()) { if (!_var->is_boolean()) { return error("Could not cast to bool!"); } return _var->as_boolean(); + } else if constexpr (std::is_floating_point>()) { if (!_var->is_floating()) { return error("Could not cast to double!"); } return static_cast>(_var->as_floating()); + } else if constexpr (std::is_integral>()) { if (!_var->is_integer()) { return error("Could not cast to int64_t!"); } return static_cast>(_var->as_integer()); + } else { static_assert(rfl::always_false_v, "Unsupported type."); } diff --git a/include/rfl/ubjson/Reader.hpp b/include/rfl/ubjson/Reader.hpp index c07cbc9a..8ae37af7 100644 --- a/include/rfl/ubjson/Reader.hpp +++ b/include/rfl/ubjson/Reader.hpp @@ -71,6 +71,7 @@ class Reader { return error("Could not cast to string."); } return _var.val_->as(); + } else if constexpr (std::is_same, rfl::Bytestring>()) { if (!_var.val_->is>()) { @@ -79,25 +80,28 @@ class Reader { const auto vec = _var.val_->as>(); const auto data = internal::ptr_cast(vec.data()); return rfl::Bytestring(data, data + vec.size()); + } else if constexpr (std::is_same, bool>()) { if (!_var.val_->is_bool()) { return error("Could not cast to boolean."); } return _var.val_->as(); - } else if constexpr (std::is_floating_point>() || - std::is_integral>()) { + + } else if constexpr (std::is_floating_point>()) { if (_var.val_->is_double()) { return static_cast(_var.val_->as()); } + return error("Could not cast to floating point value."); + + } else if constexpr (std::is_integral>()) { if (_var.val_->is_int64()) { return static_cast(_var.val_->as()); } if (_var.val_->is_uint64()) { return static_cast(_var.val_->as()); } - 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, "Unsupported type."); } diff --git a/include/rfl/xml/Reader.hpp b/include/rfl/xml/Reader.hpp index 3ef521df..fdbc5242 100644 --- a/include/rfl/xml/Reader.hpp +++ b/include/rfl/xml/Reader.hpp @@ -83,8 +83,10 @@ struct Reader { if constexpr (std::is_same, std::string>()) { return std::visit(get_value, _var.node_or_attribute_); + } else if constexpr (std::is_same, bool>()) { return std::visit(get_value, _var.node_or_attribute_) == "true"; + } else if constexpr (std::is_floating_point>()) { 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>()) { 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, "Unsupported type."); } diff --git a/include/rfl/yaml/Reader.hpp b/include/rfl/yaml/Reader.hpp index c6c97501..e6c2cfd2 100644 --- a/include/rfl/yaml/Reader.hpp +++ b/include/rfl/yaml/Reader.hpp @@ -76,11 +76,14 @@ struct Reader { std::is_same, bool>() || std::is_floating_point>()) { return _var.node_.as>(); + } else if constexpr (std::is_integral>()) { return static_cast(_var.node_.as>()); + } else { static_assert(rfl::always_false_v, "Unsupported type."); } + } catch (std::exception& e) { return error(e.what()); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0ccde04c..ae98d010 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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() diff --git a/tests/README.md b/tests/README.md index 0715a987..6bcfee9e 100644 --- a/tests/README.md +++ b/tests/README.md @@ -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 ``` diff --git a/tests/avro/test_tutorial_example.cpp b/tests/avro/test_tutorial_example.cpp index 72ca6245..a198b319 100644 --- a/tests/avro/test_tutorial_example.cpp +++ b/tests/avro/test_tutorial_example.cpp @@ -39,7 +39,7 @@ TEST(avro, test_tutorial_example) { const auto serialized1 = rfl::avro::write(person, schema.value()); const auto res = rfl::avro::read(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); } diff --git a/tests/avro/write_and_read.hpp b/tests/avro/write_and_read.hpp index 20154fa5..71779de7 100644 --- a/tests/avro/write_and_read.hpp +++ b/tests/avro/write_and_read.hpp @@ -13,7 +13,7 @@ void write_and_read(const auto& _struct) { const auto serialized1 = rfl::avro::write(_struct); const auto res = rfl::avro::read(serialized1); EXPECT_TRUE(res && true) << "Test failed on read. Error: " - << res.error().value().what(); + << res.error().what(); const auto serialized2 = rfl::avro::write(res.value()); EXPECT_EQ(serialized1, serialized2); } diff --git a/tests/capnproto/write_and_read.hpp b/tests/capnproto/write_and_read.hpp index 81692be4..1e9d7b59 100644 --- a/tests/capnproto/write_and_read.hpp +++ b/tests/capnproto/write_and_read.hpp @@ -13,7 +13,7 @@ void write_and_read(const auto& _struct) { const auto serialized1 = rfl::capnproto::write(_struct); const auto res = rfl::capnproto::read(serialized1); EXPECT_TRUE(res && true) << "Test failed on read. Error: " - << res.error().value().what(); + << res.error().what(); const auto serialized2 = rfl::capnproto::write(res.value()); EXPECT_EQ(serialized1, serialized2); } diff --git a/tests/json/test_json_schema4.cpp b/tests/json/test_json_schema4.cpp index 4d9cf3c5..541d40e7 100644 --- a/tests/json/test_json_schema4.cpp +++ b/tests/json/test_json_schema4.cpp @@ -65,8 +65,6 @@ TEST(json, test_json_schema4) { const auto json_schema = rfl::json::to_schema(); - 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"}}}})";