diff --git a/include/fastcdr/Cdr.h b/include/fastcdr/Cdr.h index ded8b543..e4048df1 100644 --- a/include/fastcdr/Cdr.h +++ b/include/fastcdr/Cdr.h @@ -3650,6 +3650,86 @@ class Cdr return *this; } + /*! + * @brief Decodes an optional member of a type according to the encoding algorithm used, applying the + * try-construct policy to its value. + * @param[out] member_value A reference of the variable where the optional member value will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when @p consistency_flag is + * @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize_member( + optional<_T>& member_value, + CdrTryConstructFlag consistency_flag) + { + return deserialize_member_try_construct(member_value, consistency_flag); + } + + /*! + * @brief Decodes an optional member of a type according to the encoding algorithm used, applying the + * try-construct policy to its value. + * @param[out] member_value A reference of the variable where the optional member value will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @param[in] max_length Maximum number of characters or elements of the value. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when @p consistency_flag is + * @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize_member( + optional<_T>& member_value, + CdrTryConstructFlag consistency_flag, + uint32_t max_length) + { + return deserialize_member_try_construct(member_value, consistency_flag, max_length); + } + + /*! + * @brief Decodes an optional member of an external according to the encoding algorithm used, applying + * the try-construct policy to its value. + * @param[out] member_value A reference of the variable where the optional member value will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when the external is locked or when + * @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize_member( + optional>& member_value, + CdrTryConstructFlag consistency_flag) + { + return deserialize_member_try_construct(member_value, consistency_flag); + } + + /*! + * @brief Decodes an optional member of an external according to the encoding algorithm used, applying + * the try-construct policy to its value. + * @param[out] member_value A reference of the variable where the optional member value will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @param[in] max_length Maximum number of characters or elements of the value. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when the external is locked or when + * @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize_member( + optional>& member_value, + CdrTryConstructFlag consistency_flag, + uint32_t max_length) + { + return deserialize_member_try_construct(member_value, consistency_flag, max_length); + } + /*! * @brief Tells to the encoder a new type and its members starts to be encoded. * @param[in,out] current_state State of the encoder previous of calling this function. @@ -3818,6 +3898,122 @@ class Cdr return *this; } + /*! + * @brief Decodes an optional from the buffer, applying the try-construct policy to its value. + * @param[out] value A reference to the variable where the optional will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when @p consistency_flag is + * @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize( + optional<_T>& value, + CdrTryConstructFlag consistency_flag) + { + return deserialize_try_construct(value, consistency_flag); + } + + /*! + * @brief Decodes an optional from the buffer, applying the try-construct policy to its value. + * @param[out] value A reference to the variable where the optional will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @param[in] max_length Maximum number of characters or elements of the value. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when @p consistency_flag is + * @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize( + optional<_T>& value, + CdrTryConstructFlag consistency_flag, + uint32_t max_length) + { + return deserialize_try_construct(value, consistency_flag, max_length); + } + + /*! + * @brief Decodes an external from the buffer, applying the try-construct policy to its value. + * @param[out] value A reference to the variable where the external will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when the external is locked or when + * @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize( + external<_T>& value, + CdrTryConstructFlag consistency_flag) + { + return deserialize_try_construct(value, consistency_flag); + } + + /*! + * @brief Decodes an external from the buffer, applying the try-construct policy to its value. + * @param[out] value A reference to the variable where the external will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @param[in] max_length Maximum number of characters or elements of the value. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when the external is locked or when + * @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize( + external<_T>& value, + CdrTryConstructFlag consistency_flag, + uint32_t max_length) + { + return deserialize_try_construct(value, consistency_flag, max_length); + } + + /*! + * @brief Decodes an optional of an external from the buffer, applying the try-construct policy to its + * value. + * @param[out] value A reference to the variable where the optional will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when the external is locked or when + * @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize( + optional>& value, + CdrTryConstructFlag consistency_flag) + { + return deserialize_try_construct(value, consistency_flag); + } + + /*! + * @brief Decodes an optional of an external from the buffer, applying the try-construct policy to its + * value. + * @param[out] value A reference to the variable where the optional will be stored. + * @param[in] consistency_flag Try-construct policy applied when the value does not fit. + * @param[in] max_length Maximum number of characters or elements of the value. + * @return Reference to the eprosima::fastcdr::Cdr object. + * @exception exception::BadParamException This exception is thrown when the external is locked or when + * @p consistency_flag is @ref CdrTryConstructFlag::FAIL and the value does not fit. + * @exception exception::NotEnoughMemoryException This exception is thrown when trying to decode from a buffer + * position that exceeds the internal memory size. + */ + template + Cdr& deserialize( + optional>& value, + CdrTryConstructFlag consistency_flag, + uint32_t max_length) + { + return deserialize_try_construct(value, consistency_flag, max_length); + } + /*! * @brief Encodes an empty DHEADER if the encoding version is XCDRv2. * After serializing the members's type, @ref set_xcdrv2_dheader must be called to set the correct DHEADER value @@ -3836,6 +4032,166 @@ class Cdr private: + /*! + * @brief Decodes an optional member of a type according to the encoding algorithm used, applying the + * try-construct policy to its value. + * Implementation shared by the public overloads taking a @ref CdrTryConstructFlag . + */ + template + Cdr& deserialize_member_try_construct( + optional<_T>& member_value, + CdrTryConstructFlag consistency_flag, + _Args&&... args) + { + if (EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_) + { + Cdr::state current_state(*this); + MemberId member_id; + xcdr1_deserialize_member_header(member_id, current_state); + auto prev_offset = offset_; + member_value.reset(0 < current_state.member_size_); + if (0 < current_state.member_size_) + { + deserialize(member_value, consistency_flag, std::forward<_Args>(args)...); + } + size_t member_size {current_state.member_size_}; + size_t diff {offset_ - prev_offset}; + if (member_size < diff) + { + throw exception::BadParamException( + "Member size provided by member header is lower than real decoded member size"); + } + + // Skip unused bytes + offset_ += (member_size - diff); + } + else + { + deserialize(member_value, consistency_flag, std::forward<_Args>(args)...); + } + return *this; + } + + /*! + * @brief Decodes an optional member of an external according to the encoding algorithm used, applying the + * try-construct policy to its value. + * Implementation shared by the public overloads taking a @ref CdrTryConstructFlag . + */ + template + Cdr& deserialize_member_try_construct( + optional>& member_value, + CdrTryConstructFlag consistency_flag, + _Args&&... args) + { + if (member_value.has_value() && member_value.value().is_locked()) + { + throw exception::BadParamException("External member is locked"); + } + + if (EncodingAlgorithmFlag::PLAIN_CDR == current_encoding_) + { + Cdr::state current_state(*this); + MemberId member_id; + xcdr1_deserialize_member_header(member_id, current_state); + auto prev_offset = offset_; + member_value.reset(0 < current_state.member_size_); + if (0 < current_state.member_size_) + { + deserialize(member_value, consistency_flag, std::forward<_Args>(args)...); + } + size_t member_size {current_state.member_size_}; + size_t diff {offset_ - prev_offset}; + if (member_size < diff) + { + throw exception::BadParamException( + "Member size provided by member header is lower than real decoded member size"); + } + + // Skip unused bytes + offset_ += (member_size - diff); + } + else + { + deserialize(member_value, consistency_flag, std::forward<_Args>(args)...); + } + return *this; + } + + /*! + * @brief Decodes an optional from the buffer, applying the try-construct policy to its value. + * Implementation shared by the public overloads taking a @ref CdrTryConstructFlag . + */ + template + Cdr& deserialize_try_construct( + optional<_T>& value, + CdrTryConstructFlag consistency_flag, + _Args&&... args) + { + bool is_present = true; + if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_) + { + deserialize_framing(is_present); + } + value.reset(is_present); + if (is_present) + { + deserialize(*value, consistency_flag, std::forward<_Args>(args)...); + } + return *this; + } + + /*! + * @brief Decodes an external from the buffer, applying the try-construct policy to its value. + * Implementation shared by the public overloads taking a @ref CdrTryConstructFlag . + */ + template + Cdr& deserialize_try_construct( + external<_T>& value, + CdrTryConstructFlag consistency_flag, + _Args&&... args) + { + if (value.is_locked()) + { + throw exception::BadParamException("External member is locked"); + } + + if (!value) + { + value = external<_T>{new typename external<_T>::type()}; + } + + deserialize(*value, consistency_flag, std::forward<_Args>(args)...); + return *this; + } + + /*! + * @brief Decodes an optional of an external from the buffer, applying the try-construct policy to its value. + * Implementation shared by the public overloads taking a @ref CdrTryConstructFlag . + */ + template + Cdr& deserialize_try_construct( + optional>& value, + CdrTryConstructFlag consistency_flag, + _Args&&... args) + { + if (value.has_value() && value.value().is_locked()) + { + throw exception::BadParamException("External member is locked"); + } + + bool is_present = true; + if (CdrVersion::XCDRv2 == cdr_version_ && EncodingAlgorithmFlag::PL_CDR2 != current_encoding_) + { + deserialize_framing(is_present); + } + value.reset(is_present); + if (is_present) + { + deserialize(*value, consistency_flag, std::forward<_Args>(args)...); + } + return *this; + } + Cdr( const Cdr&) = delete; diff --git a/test/xcdr/type_consistency.cpp b/test/xcdr/type_consistency.cpp index d3e9244c..9b63b5c1 100644 --- a/test/xcdr/type_consistency.cpp +++ b/test/xcdr/type_consistency.cpp @@ -414,6 +414,92 @@ void deserialize( } // namespace fastcdr } // namespace eprosima +/*! + * Type serialized by the try-construct tests on optional and external members. Each member is wider than its + * counterpart in @ref TryConstructWrapperDst , and a trailing member makes a wrong number of consumed bytes visible. + */ +struct TryConstructWrapperSrc +{ + optional opt_str; + + std::vector ext_seq; + + optional opt_ext_str; + + uint16_t trailer {0}; +}; + +/*! + * Type deserialized by the try-construct tests on optional and external members, mirroring the code generated by + * Fast DDS-Gen for @c \@optional and @c \@external members annotated with @c \@try_construct . Members are decoded + * with @c Cdr::deserialize_member so that the XCDRv1 member header of the optional members is handled. + */ +struct TryConstructWrapperDst +{ + optional> opt_str; + + external> ext_seq; + + optional> opt_ext_str; + + uint16_t trailer {0}; + + //! Policy applied to every bounded member while decoding. + CdrTryConstructFlag flag {CdrTryConstructFlag::FAIL}; +}; + +namespace eprosima { +namespace fastcdr { + +template<> +void serialize( + Cdr& cdr, + const TryConstructWrapperSrc& data) +{ + Cdr::state current_status(cdr); + cdr.begin_serialize_type(current_status, cdr.get_encoding_flag()); + cdr << MemberId(0) << data.opt_str; + cdr << MemberId(1) << data.ext_seq; + cdr << MemberId(2) << data.opt_ext_str; + cdr << MemberId(3) << data.trailer; + cdr.end_serialize_type(current_status); +} + +template<> +void deserialize( + Cdr& cdr, + TryConstructWrapperDst& data) +{ + cdr.deserialize_type(cdr.get_encoding_flag(), [&data](Cdr& cdr_inner, const MemberId& mid) -> bool + { + bool ret_value = true; + + switch (mid.id) + { + case 0: + cdr_inner.deserialize_member(data.opt_str, data.flag); + break; + case 1: + cdr_inner.deserialize(data.ext_seq, data.flag, 2); + break; + case 2: + cdr_inner.deserialize_member(data.opt_ext_str, data.flag, 3); + break; + case 3: + cdr_inner >> data.trailer; + break; + default: + ret_value = false; + break; + } + + return ret_value; + }); +} + +} // namespace fastcdr +} // namespace eprosima + // ===================================================================================== // std::string // ===================================================================================== @@ -2571,6 +2657,162 @@ TEST_P(XCdrTypeConsistencyTest, StdVectorUnion_TrimConsumesWholeSequence) } +// ===================================================================================== +// optional and external members +// ===================================================================================== + +namespace { + +//! Fills a @ref TryConstructWrapperSrc whose bounded members all exceed the bounds of @ref TryConstructWrapperDst . +TryConstructWrapperSrc oversized_wrapper() +{ + TryConstructWrapperSrc input; + input.opt_str = std::string("abcdefgh"); + input.ext_seq = {1, 2, 3, 4}; + input.opt_ext_str = std::string("uvwxyz"); + input.trailer = 0xBEEF; + return input; +} + +} // namespace + +/*! + * @brief Checks that @c TRIM is applied to the value held by optional and external members. + * + * The policy is forwarded through the optional (and its presence flag or XCDRv1 member header) and through the + * external to the bounded value. The case is repeated for XCDRv1 and XCDRv2, in big and little endian. + * + * @test XCdrTypeConsistencyTest.OptionalExternal_Trim + * @pre A structure with an optional string of 8 characters, an external sequence of 4 elements and an optional + * external string of 6 characters is serialized, followed by a trailing member. + * @post Deserializing with @c TRIM into bounds of 5 characters, 2 elements and 3 characters does not throw, trims + * every value and leaves the trailing member correctly decoded. + */ +TEST_P(XCdrTypeConsistencyTest, OptionalExternal_Trim) +{ + const EncodingAlgorithmFlag encoding {std::get<0>(GetParam())}; + const Cdr::Endianness endianness {std::get<1>(GetParam())}; + + char buffer[TEST_BUFFER_LENGTH]; + serialize_into(buffer, sizeof(buffer), oversized_wrapper(), encoding, endianness); + + FastBuffer fast_buffer(buffer, sizeof(buffer)); + Cdr cdr_des(fast_buffer, endianness, get_version_from_algorithm(encoding)); + cdr_des.set_encoding_flag(encoding); + + TryConstructWrapperDst output; + output.flag = CdrTryConstructFlag::TRIM; + ASSERT_NO_THROW(cdr_des >> output); + ASSERT_TRUE(output.opt_str.has_value()); + EXPECT_STREQ(output.opt_str->c_str(), "abcde"); + ASSERT_TRUE(output.ext_seq); + const std::vector expected_seq {1, 2}; + EXPECT_EQ(*output.ext_seq, expected_seq); + ASSERT_TRUE(output.opt_ext_str.has_value()); + EXPECT_EQ(**output.opt_ext_str, "uvw"); + EXPECT_EQ(output.trailer, 0xBEEF); +} + +/*! + * @brief Checks that @c DEFAULT_VALUE is applied to the value held by optional and external members. + * + * The optional members stay present: only their value is reset. The case is repeated for XCDRv1 and XCDRv2, in big + * and little endian. + * + * @test XCdrTypeConsistencyTest.OptionalExternal_DefaultValue + * @pre The same oversized structure as in @c OptionalExternal_Trim is serialized. + * @post Deserializing with @c DEFAULT_VALUE does not throw, leaves every value empty and leaves the trailing member + * correctly decoded. + */ +TEST_P(XCdrTypeConsistencyTest, OptionalExternal_DefaultValue) +{ + const EncodingAlgorithmFlag encoding {std::get<0>(GetParam())}; + const Cdr::Endianness endianness {std::get<1>(GetParam())}; + + char buffer[TEST_BUFFER_LENGTH]; + serialize_into(buffer, sizeof(buffer), oversized_wrapper(), encoding, endianness); + + FastBuffer fast_buffer(buffer, sizeof(buffer)); + Cdr cdr_des(fast_buffer, endianness, get_version_from_algorithm(encoding)); + cdr_des.set_encoding_flag(encoding); + + TryConstructWrapperDst output; + output.flag = CdrTryConstructFlag::DEFAULT_VALUE; + ASSERT_NO_THROW(cdr_des >> output); + ASSERT_TRUE(output.opt_str.has_value()); + EXPECT_STREQ(output.opt_str->c_str(), ""); + ASSERT_TRUE(output.ext_seq); + EXPECT_TRUE(output.ext_seq->empty()); + ASSERT_TRUE(output.opt_ext_str.has_value()); + EXPECT_TRUE((*output.opt_ext_str)->empty()); + EXPECT_EQ(output.trailer, 0xBEEF); +} + +/*! + * @brief Checks that @c FAIL is applied to the value held by optional and external members. + * + * The case is repeated for XCDRv1 and XCDRv2, in big and little endian. + * + * @test XCdrTypeConsistencyTest.OptionalExternal_Fail + * @pre The same oversized structure as in @c OptionalExternal_Trim is serialized. + * @post Deserializing with @c FAIL throws @c BadParamException . + */ +TEST_P(XCdrTypeConsistencyTest, OptionalExternal_Fail) +{ + const EncodingAlgorithmFlag encoding {std::get<0>(GetParam())}; + const Cdr::Endianness endianness {std::get<1>(GetParam())}; + + char buffer[TEST_BUFFER_LENGTH]; + serialize_into(buffer, sizeof(buffer), oversized_wrapper(), encoding, endianness); + + FastBuffer fast_buffer(buffer, sizeof(buffer)); + Cdr cdr_des(fast_buffer, endianness, get_version_from_algorithm(encoding)); + cdr_des.set_encoding_flag(encoding); + + TryConstructWrapperDst output; + output.flag = CdrTryConstructFlag::FAIL; + EXPECT_THROW(cdr_des >> output, BadParamException); +} + +/*! + * @brief Checks that absent optional members are decoded as absent when a try-construct policy is given. + * + * The case is repeated for XCDRv1 and XCDRv2, in big and little endian. + * + * @test XCdrTypeConsistencyTest.OptionalExternal_Absent + * @pre A structure whose optional members are empty is serialized, with an external sequence inside its bound. + * @post Deserializing with @c FAIL does not throw, leaves the optional members empty and leaves the trailing member + * correctly decoded. + */ +TEST_P(XCdrTypeConsistencyTest, OptionalExternal_Absent) +{ + const EncodingAlgorithmFlag encoding {std::get<0>(GetParam())}; + const Cdr::Endianness endianness {std::get<1>(GetParam())}; + + TryConstructWrapperSrc input; + input.ext_seq = {7}; + input.trailer = 0xBEEF; + + char buffer[TEST_BUFFER_LENGTH]; + serialize_into(buffer, sizeof(buffer), input, encoding, endianness); + + FastBuffer fast_buffer(buffer, sizeof(buffer)); + Cdr cdr_des(fast_buffer, endianness, get_version_from_algorithm(encoding)); + cdr_des.set_encoding_flag(encoding); + + TryConstructWrapperDst output; + output.opt_str = fixed_string<5>("x"); + output.opt_ext_str = external{new std::string("x")}; + output.flag = CdrTryConstructFlag::FAIL; + ASSERT_NO_THROW(cdr_des >> output); + EXPECT_FALSE(output.opt_str.has_value()); + ASSERT_TRUE(output.ext_seq); + EXPECT_EQ(*output.ext_seq, std::vector{7}); + EXPECT_FALSE(output.opt_ext_str.has_value()); + EXPECT_EQ(output.trailer, 0xBEEF); +} + + INSTANTIATE_TEST_SUITE_P( XCdrTest, XCdrTypeConsistencyTest,