From b7af660b5003350d1d52c4050cad1708f11c01cb 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: Wed, 19 Feb 2025 00:09:28 +0100 Subject: [PATCH] Make sure we don't mix integers and floats in msgpack (#354) --- include/rfl/Generic.hpp | 167 ++++++++++++++++++--------- include/rfl/msgpack/Reader.hpp | 10 +- tests/msgpack/test_generic_float.cpp | 24 ++++ 3 files changed, 144 insertions(+), 57 deletions(-) create mode 100644 tests/msgpack/test_generic_float.cpp diff --git a/include/rfl/Generic.hpp b/include/rfl/Generic.hpp index 1faeda8e..77bbbcba 100644 --- a/include/rfl/Generic.hpp +++ b/include/rfl/Generic.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -39,7 +40,9 @@ class Generic { template , bool>::type = true> - Generic(const T& _value) : value_(_value) {} + Generic(const T& _value) { + value_ = _value; + } template , @@ -65,7 +68,16 @@ class Generic { typename std::enable_if, bool>::type = true> auto& operator=(const T& _value) { - value_ = _value; + using Type = std::remove_cvref_t; + if constexpr (std::is_same_v) { + value_.emplace<0>(_value); + } else if constexpr (std::is_integral_v) { + value_.emplace<1>(static_cast(_value)); + } else if constexpr (std::is_floating_point_v) { + value_.emplace<2>(static_cast(_value)); + } else { + value_ = _value; + } return *this; } @@ -81,92 +93,137 @@ class Generic { /// Casts the underlying value to an rfl::Generic::Array or returns an /// rfl::Error, if the underlying value is not an rfl::Generic::Array. Result to_array() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return error( - "rfl::Generic: Could not cast the underlying value to an " - "rfl::Generic::Array."); - } + return std::visit( + [](auto _v) -> Result { + using V = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return _v; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to an " + "rfl::Generic::Array."); + } + }, + value_); } /// Casts the underlying value to a boolean or returns an rfl::Error, if the /// underlying value is not a boolean. Result to_bool() const noexcept { - if (const bool* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return error( - "rfl::Generic: Could not cast the underlying value to a boolean."); - } + return std::visit( + [](auto _v) -> Result { + using V = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return _v; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to a " + "boolean."); + } + }, + value_); } /// Casts the underlying value to a double or returns an rfl::Error, if the /// underlying value is not a double. Result to_double() const noexcept { - if (const double* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return error( - "rfl::Generic: Could not cast the underlying value to a double."); - } + return std::visit( + [](auto _v) -> Result { + using V = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return _v; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to a " + "double."); + } + }, + value_); } /// Casts the underlying value to an integer or returns an rfl::Error, if the /// underlying value is not an integer. Result to_int() const noexcept { - if (const int64_t* ptr = std::get_if(&value_)) { - return static_cast(*ptr); - } else { - return error( - "rfl::Generic: Could not cast the underlying value to an integer."); - } + return std::visit( + [](auto _v) -> Result { + using V = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return static_cast(_v); + } else { + return error( + "rfl::Generic: Could not cast the underlying value to an " + "integer."); + } + }, + value_); } /// Casts the underlying value to an int64 or returns an rfl::Error, if the /// underlying value is not an integer. Result to_int64() const noexcept { - if (const int64_t* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return error( - "rfl::Generic: Could not cast the underlying value to an int64."); - } + return std::visit( + [](auto _v) -> Result { + using V = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return _v; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to an " + "int64."); + } + }, + value_); } /// Casts the underlying value to an rfl::Generic::Object or returns an /// rfl::Error, if the underlying value is not an rfl::Generic::Object. Result to_object() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return error( - "rfl::Generic: Could not cast the underlying value to an " - "rfl::Generic::Object."); - } + return std::visit( + [](auto _v) -> Result { + using V = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return _v; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to an " + "rfl::Generic::Object."); + } + }, + value_); } /// Casts the underlying value to rfl::Generic::Null or returns an /// rfl::Error, if the underlying value is not rfl::Generic::Null. Result to_null() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return error( - "rfl::Generic: Could not cast the underlying value to " - "rfl::Generic::Null."); - } + return std::visit( + [](auto _v) -> Result { + using V = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return _v; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to " + "rfl::Generic::Null."); + } + }, + value_); } /// Casts the underlying value to a string or returns an rfl::Error, if the /// underlying value is not a string. Result to_string() const noexcept { - if (const auto* ptr = std::get_if(&value_)) { - return *ptr; - } else { - return error( - "rfl::Generic: Could not cast the underlying value to a string."); - } + return std::visit( + [](auto _v) -> Result { + using V = std::remove_cvref_t; + if constexpr (std::is_same_v) { + return _v; + } else { + return error( + "rfl::Generic: Could not cast the underlying value to a " + "string."); + } + }, + value_); } /// Returns the underlying variant. @@ -204,7 +261,9 @@ inline Result to_int(const Generic& _g) noexcept { return _g.to_int(); } /// Casts the underlying value to an int64 or returns an rfl::Error, if the /// underlying value is not an integer. -inline Result to_int64(const Generic& _g) noexcept { return _g.to_int64(); } +inline Result to_int64(const Generic& _g) noexcept { + return _g.to_int64(); +} /// Casts the underlying value to an rfl::Generic::Object or returns an /// rfl::Error, if the underlying value is not an rfl::Generic::Object. diff --git a/include/rfl/msgpack/Reader.hpp b/include/rfl/msgpack/Reader.hpp index 815eb436..ecc6a33f 100644 --- a/include/rfl/msgpack/Reader.hpp +++ b/include/rfl/msgpack/Reader.hpp @@ -77,12 +77,16 @@ struct Reader { return error("Could not cast to boolean."); } return _var.via.boolean; - } else if constexpr (std::is_floating_point>() || - std::is_integral>()) { + } else if constexpr (std::is_floating_point>()) { if (type == MSGPACK_OBJECT_FLOAT32 || type == MSGPACK_OBJECT_FLOAT64 || type == MSGPACK_OBJECT_FLOAT) { return static_cast(_var.via.f64); - } else if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) { + } + 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); } else if (type == MSGPACK_OBJECT_NEGATIVE_INTEGER) { return static_cast(_var.via.i64); diff --git a/tests/msgpack/test_generic_float.cpp b/tests/msgpack/test_generic_float.cpp new file mode 100644 index 00000000..49d9e30f --- /dev/null +++ b/tests/msgpack/test_generic_float.cpp @@ -0,0 +1,24 @@ +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_generic_float { + +TEST(msgpack, test_generic_float) { + rfl::Generic msgpack_data_before{}; + rfl::Generic msgpack_data_after{}; + + msgpack_data_before = 1.4F; + + auto tmp_msgpack = + rfl::msgpack::write(msgpack_data_before); + msgpack_data_after = *rfl::msgpack::read(tmp_msgpack); + + EXPECT_EQ(msgpack_data_before.to_double().value(), + msgpack_data_after.to_double().value()); +} +} // namespace test_generic_float