Skip to content

Commit

Permalink
Always check types when deserializing
Browse files Browse the repository at this point in the history
  • Loading branch information
stephane-caron committed Aug 8, 2024
1 parent 230ade0 commit 85c6c01
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file.

### Changed

- Always check types when deserializing
- docs: Don't show include files

## [2.1.0] - 2024/05/24
Expand Down
34 changes: 0 additions & 34 deletions include/palimpsest/mpack/read.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ void read(const mpack_node_t node, T& value) {
*/
template <>
inline void read(const mpack_node_t node, bool& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_bool) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting bool, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_bool(node);
}

Expand All @@ -61,15 +59,13 @@ inline void read(const mpack_node_t node, bool& value) {
*/
template <>
inline void read(const mpack_node_t node, int8_t& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_int &&
mpack_node_type(node) != mpack_type_uint) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting int8_t, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_i8(node);
}

Expand All @@ -82,15 +78,13 @@ inline void read(const mpack_node_t node, int8_t& value) {
*/
template <>
inline void read(const mpack_node_t node, int16_t& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_int &&
mpack_node_type(node) != mpack_type_uint) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting int16_t, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_i16(node);
}

Expand All @@ -103,15 +97,13 @@ inline void read(const mpack_node_t node, int16_t& value) {
*/
template <>
inline void read(const mpack_node_t node, int32_t& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_int &&
mpack_node_type(node) != mpack_type_uint) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting int32_t, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_i32(node);
}

Expand All @@ -124,15 +116,13 @@ inline void read(const mpack_node_t node, int32_t& value) {
*/
template <>
inline void read(const mpack_node_t node, int64_t& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_int &&
mpack_node_type(node) != mpack_type_uint) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting int64_t, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_i64(node);
}

Expand All @@ -145,14 +135,12 @@ inline void read(const mpack_node_t node, int64_t& value) {
*/
template <>
inline void read(const mpack_node_t node, uint8_t& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_uint) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting uint8_t, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_u8(node);
}

Expand All @@ -165,14 +153,12 @@ inline void read(const mpack_node_t node, uint8_t& value) {
*/
template <>
inline void read(const mpack_node_t node, uint16_t& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_uint) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting uint16_t, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_u16(node);
}

Expand All @@ -185,14 +171,12 @@ inline void read(const mpack_node_t node, uint16_t& value) {
*/
template <>
inline void read(const mpack_node_t node, uint32_t& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_uint) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting uint32_t, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_u32(node);
}

Expand All @@ -205,14 +189,12 @@ inline void read(const mpack_node_t node, uint32_t& value) {
*/
template <>
inline void read(const mpack_node_t node, uint64_t& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_uint) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting uint64_t, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_u64(node);
}

Expand All @@ -225,7 +207,6 @@ inline void read(const mpack_node_t node, uint64_t& value) {
*/
template <>
inline void read(const mpack_node_t node, float& value) {
#ifndef NDEBUG
switch (mpack_node_type(node)) {
case mpack_type_int:
case mpack_type_uint:
Expand All @@ -238,7 +219,6 @@ inline void read(const mpack_node_t node, float& value) {
std::string("Expecting float, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_float(node);
}

Expand All @@ -251,7 +231,6 @@ inline void read(const mpack_node_t node, float& value) {
*/
template <>
inline void read(const mpack_node_t node, double& value) {
#ifndef NDEBUG
switch (mpack_node_type(node)) {
case mpack_type_int:
case mpack_type_uint:
Expand All @@ -264,7 +243,6 @@ inline void read(const mpack_node_t node, double& value) {
std::string("Expecting double, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = mpack_node_double(node);
}

Expand All @@ -277,14 +255,12 @@ inline void read(const mpack_node_t node, double& value) {
*/
template <>
inline void read(const mpack_node_t node, std::string& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_str) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting std::string, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
value = std::string{mpack_node_str(node), mpack_node_strlen(node)};
}

Expand All @@ -297,14 +273,12 @@ inline void read(const mpack_node_t node, std::string& value) {
*/
template <>
inline void read(const mpack_node_t node, Eigen::Vector2d& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_array) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting an array, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
assert(mpack_node_array_length(node) == 2);
read<double>(mpack_node_array_at(node, 0), value.x());
read<double>(mpack_node_array_at(node, 1), value.y());
Expand All @@ -319,14 +293,12 @@ inline void read(const mpack_node_t node, Eigen::Vector2d& value) {
*/
template <>
inline void read(const mpack_node_t node, Eigen::Vector3d& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_array) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting an array, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
assert(mpack_node_array_length(node) == 3);
read<double>(mpack_node_array_at(node, 0), value.x());
read<double>(mpack_node_array_at(node, 1), value.y());
Expand All @@ -342,14 +314,12 @@ inline void read(const mpack_node_t node, Eigen::Vector3d& value) {
*/
template <>
inline void read(const mpack_node_t node, Eigen::VectorXd& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_array) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting an array, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
const unsigned length = mpack_node_array_length(node);
assert(length == value.size());
for (size_t i = 0; i < length; ++i) {
Expand All @@ -366,14 +336,12 @@ inline void read(const mpack_node_t node, Eigen::VectorXd& value) {
*/
template <>
inline void read(const mpack_node_t node, Eigen::Quaterniond& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_array) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting an array, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
assert(mpack_node_array_length(node) == 4);
read<double>(mpack_node_array_at(node, 0), value.w());
read<double>(mpack_node_array_at(node, 1), value.x());
Expand All @@ -390,14 +358,12 @@ inline void read(const mpack_node_t node, Eigen::Quaterniond& value) {
*/
template <>
inline void read(const mpack_node_t node, Eigen::Matrix3d& value) {
#ifndef NDEBUG
if (mpack_node_type(node) != mpack_type_array) {
throw TypeError(
__FILE__, __LINE__,
std::string("Expecting an array, but deserialized node has type ") +
mpack_type_to_string(mpack_node_type(node)));
}
#endif
assert(mpack_node_array_length(node) == 9);
for (Eigen::Index i = 0; i < 3; ++i) {
for (Eigen::Index j = 0; j < 3; ++j) {
Expand Down
4 changes: 0 additions & 4 deletions tests/mpack/read_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,6 @@ TEST_F(ReadTest, ReadUnknownTypeError) {
ASSERT_THROW(mpack::read(node_, output), TypeError);
}

#ifndef NDEBUG // other type errors aren't checked in "opt" compilation mode

TEST_F(ReadTest, ReadBoolTypeError) {
data_.type = mpack_type_int;
bool output;
Expand Down Expand Up @@ -277,6 +275,4 @@ TEST_F(ReadTest, ReadEigenQuaterniondTypeError) {
ASSERT_THROW(mpack::read(node_, output), TypeError);
}

#endif // NDEBUG

} // namespace palimpsest::mpack

0 comments on commit 85c6c01

Please sign in to comment.