Make sure we don't mix integers and floats in msgpack (#354)

This commit is contained in:
Dr. Patrick Urbanke (劉自成)
2025-02-19 00:09:28 +01:00
committed by GitHub
parent 5f0abe6d77
commit b7af660b50
3 changed files with 144 additions and 57 deletions
+113 -54
View File
@@ -4,6 +4,7 @@
#include <optional> #include <optional>
#include <ostream> #include <ostream>
#include <string> #include <string>
#include <type_traits>
#include <variant> #include <variant>
#include <vector> #include <vector>
@@ -39,7 +40,9 @@ class Generic {
template <class T, template <class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, typename std::enable_if<std::is_convertible_v<T, VariantType>,
bool>::type = true> bool>::type = true>
Generic(const T& _value) : value_(_value) {} Generic(const T& _value) {
value_ = _value;
}
template <class T, template <class T,
typename std::enable_if<std::is_convertible_v<T, VariantType>, typename std::enable_if<std::is_convertible_v<T, VariantType>,
@@ -65,7 +68,16 @@ class Generic {
typename std::enable_if<std::is_convertible_v<T, VariantType>, typename std::enable_if<std::is_convertible_v<T, VariantType>,
bool>::type = true> bool>::type = true>
auto& operator=(const T& _value) { auto& operator=(const T& _value) {
value_ = _value; using Type = std::remove_cvref_t<T>;
if constexpr (std::is_same_v<Type, bool>) {
value_.emplace<0>(_value);
} else if constexpr (std::is_integral_v<Type>) {
value_.emplace<1>(static_cast<int64_t>(_value));
} else if constexpr (std::is_floating_point_v<Type>) {
value_.emplace<2>(static_cast<double>(_value));
} else {
value_ = _value;
}
return *this; return *this;
} }
@@ -81,92 +93,137 @@ class Generic {
/// Casts the underlying value to an rfl::Generic::Array or returns an /// Casts the underlying value to an rfl::Generic::Array or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Array. /// rfl::Error, if the underlying value is not an rfl::Generic::Array.
Result<Array> to_array() const noexcept { Result<Array> to_array() const noexcept {
if (const auto* ptr = std::get_if<Array>(&value_)) { return std::visit(
return *ptr; [](auto _v) -> Result<Array> {
} else { using V = std::remove_cvref_t<decltype(_v)>;
return error( if constexpr (std::is_same_v<V, Array>) {
"rfl::Generic: Could not cast the underlying value to an " return _v;
"rfl::Generic::Array."); } 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 /// Casts the underlying value to a boolean or returns an rfl::Error, if the
/// underlying value is not a boolean. /// underlying value is not a boolean.
Result<bool> to_bool() const noexcept { Result<bool> to_bool() const noexcept {
if (const bool* ptr = std::get_if<bool>(&value_)) { return std::visit(
return *ptr; [](auto _v) -> Result<bool> {
} else { using V = std::remove_cvref_t<decltype(_v)>;
return error( if constexpr (std::is_same_v<V, bool>) {
"rfl::Generic: Could not cast the underlying value to a boolean."); 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 /// Casts the underlying value to a double or returns an rfl::Error, if the
/// underlying value is not a double. /// underlying value is not a double.
Result<double> to_double() const noexcept { Result<double> to_double() const noexcept {
if (const double* ptr = std::get_if<double>(&value_)) { return std::visit(
return *ptr; [](auto _v) -> Result<double> {
} else { using V = std::remove_cvref_t<decltype(_v)>;
return error( if constexpr (std::is_same_v<V, double>) {
"rfl::Generic: Could not cast the underlying value to a double."); 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 /// Casts the underlying value to an integer or returns an rfl::Error, if the
/// underlying value is not an integer. /// underlying value is not an integer.
Result<int> to_int() const noexcept { Result<int> to_int() const noexcept {
if (const int64_t* ptr = std::get_if<int64_t>(&value_)) { return std::visit(
return static_cast<int>(*ptr); [](auto _v) -> Result<int> {
} else { using V = std::remove_cvref_t<decltype(_v)>;
return error( if constexpr (std::is_same_v<V, int64_t>) {
"rfl::Generic: Could not cast the underlying value to an integer."); return static_cast<int>(_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 /// Casts the underlying value to an int64 or returns an rfl::Error, if the
/// underlying value is not an integer. /// underlying value is not an integer.
Result<int64_t> to_int64() const noexcept { Result<int64_t> to_int64() const noexcept {
if (const int64_t* ptr = std::get_if<int64_t>(&value_)) { return std::visit(
return *ptr; [](auto _v) -> Result<int64_t> {
} else { using V = std::remove_cvref_t<decltype(_v)>;
return error( if constexpr (std::is_same_v<V, int64_t>) {
"rfl::Generic: Could not cast the underlying value to an int64."); 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 /// Casts the underlying value to an rfl::Generic::Object or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Object. /// rfl::Error, if the underlying value is not an rfl::Generic::Object.
Result<Object> to_object() const noexcept { Result<Object> to_object() const noexcept {
if (const auto* ptr = std::get_if<Object>(&value_)) { return std::visit(
return *ptr; [](auto _v) -> Result<Object> {
} else { using V = std::remove_cvref_t<decltype(_v)>;
return error( if constexpr (std::is_same_v<V, Object>) {
"rfl::Generic: Could not cast the underlying value to an " return _v;
"rfl::Generic::Object."); } 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 /// Casts the underlying value to rfl::Generic::Null or returns an
/// rfl::Error, if the underlying value is not rfl::Generic::Null. /// rfl::Error, if the underlying value is not rfl::Generic::Null.
Result<std::nullopt_t> to_null() const noexcept { Result<std::nullopt_t> to_null() const noexcept {
if (const auto* ptr = std::get_if<std::nullopt_t>(&value_)) { return std::visit(
return *ptr; [](auto _v) -> Result<std::nullopt_t> {
} else { using V = std::remove_cvref_t<decltype(_v)>;
return error( if constexpr (std::is_same_v<V, std::nullopt_t>) {
"rfl::Generic: Could not cast the underlying value to " return _v;
"rfl::Generic::Null."); } 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 /// Casts the underlying value to a string or returns an rfl::Error, if the
/// underlying value is not a string. /// underlying value is not a string.
Result<std::string> to_string() const noexcept { Result<std::string> to_string() const noexcept {
if (const auto* ptr = std::get_if<std::string>(&value_)) { return std::visit(
return *ptr; [](auto _v) -> Result<std::string> {
} else { using V = std::remove_cvref_t<decltype(_v)>;
return error( if constexpr (std::is_same_v<V, std::string>) {
"rfl::Generic: Could not cast the underlying value to a string."); return _v;
} } else {
return error(
"rfl::Generic: Could not cast the underlying value to a "
"string.");
}
},
value_);
} }
/// Returns the underlying variant. /// Returns the underlying variant.
@@ -204,7 +261,9 @@ inline Result<int> to_int(const Generic& _g) noexcept { return _g.to_int(); }
/// Casts the underlying value to an int64 or returns an rfl::Error, if the /// Casts the underlying value to an int64 or returns an rfl::Error, if the
/// underlying value is not an integer. /// underlying value is not an integer.
inline Result<long> to_int64(const Generic& _g) noexcept { return _g.to_int64(); } inline Result<long> to_int64(const Generic& _g) noexcept {
return _g.to_int64();
}
/// Casts the underlying value to an rfl::Generic::Object or returns an /// Casts the underlying value to an rfl::Generic::Object or returns an
/// rfl::Error, if the underlying value is not an rfl::Generic::Object. /// rfl::Error, if the underlying value is not an rfl::Generic::Object.
+7 -3
View File
@@ -77,12 +77,16 @@ struct Reader {
return error("Could not cast to boolean."); return error("Could not cast to boolean.");
} }
return _var.via.boolean; return _var.via.boolean;
} else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>() || } else if constexpr (std::is_floating_point<std::remove_cvref_t<T>>()) {
std::is_integral<std::remove_cvref_t<T>>()) {
if (type == MSGPACK_OBJECT_FLOAT32 || type == MSGPACK_OBJECT_FLOAT64 || if (type == MSGPACK_OBJECT_FLOAT32 || type == MSGPACK_OBJECT_FLOAT64 ||
type == MSGPACK_OBJECT_FLOAT) { type == MSGPACK_OBJECT_FLOAT) {
return static_cast<T>(_var.via.f64); return static_cast<T>(_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<std::remove_cvref_t<T>>()) {
if (type == MSGPACK_OBJECT_POSITIVE_INTEGER) {
return static_cast<T>(_var.via.u64); return static_cast<T>(_var.via.u64);
} else if (type == MSGPACK_OBJECT_NEGATIVE_INTEGER) { } else if (type == MSGPACK_OBJECT_NEGATIVE_INTEGER) {
return static_cast<T>(_var.via.i64); return static_cast<T>(_var.via.i64);
+24
View File
@@ -0,0 +1,24 @@
#include <cassert>
#include <iostream>
#include <rfl.hpp>
#include <string>
#include <vector>
#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<rfl::UnderlyingEnums>(msgpack_data_before);
msgpack_data_after = *rfl::msgpack::read<rfl::Generic>(tmp_msgpack);
EXPECT_EQ(msgpack_data_before.to_double().value(),
msgpack_data_after.to_double().value());
}
} // namespace test_generic_float