From 61e0450dbf83192f3ba6eca6d0c2eda257395b51 Mon Sep 17 00:00:00 2001 From: Miguel Company Date: Mon, 9 Mar 2020 10:28:44 +0100 Subject: [PATCH] Force aligned param length (#1047) * Refs #7837. Remove friend class ParameterList from QosPolicies * Refs #7837. Added octet vector (de)serialization methods to QosPolicy. * Refs #7837. Templatization of UserDataQosPolicy into GenericDataQosPolicy * Refs #7837. Converting TopicDataQosPolicy and GroupDataQosPolicy. * Refs #7837. Middle class to avoid linker problems. * Refs #7837. Fixing DataRepresentationQosPolicy. * Avoid duplicate serialization of length * Refs #7837. Fixed padding skip. * Refs #7837. Fixing Doxygen. * Refs #7837. Fixing blackbox tests. --- include/fastrtps/qos/QosPolicies.h | 408 +++++++-------------- src/cpp/qos/QosPolicies.cpp | 162 ++++---- test/blackbox/BlackboxTestsPubSubBasic.cpp | 4 +- 3 files changed, 215 insertions(+), 359 deletions(-) diff --git a/include/fastrtps/qos/QosPolicies.h b/include/fastrtps/qos/QosPolicies.h index af90ce7bad3..6edce8754d4 100644 --- a/include/fastrtps/qos/QosPolicies.h +++ b/include/fastrtps/qos/QosPolicies.h @@ -79,6 +79,17 @@ class QosPolicy static uint32_t get_cdr_serialized_size( const std::vector& data); + static bool serialize_generic_data( + rtps::CDRMessage_t* msg, + uint16_t pid, + const std::vector& data); + + static bool deserialize_generic_data( + rtps::CDRMessage_t* msg, + uint16_t size, + size_t max_size, + std::vector& data); + public: bool hasChanged; @@ -108,8 +119,6 @@ typedef enum DurabilityQosPolicyKind : rtps::octet */ class DurabilityQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI DurabilityQosPolicy() @@ -199,8 +208,6 @@ class DurabilityQosPolicy : public Parameter_t, public QosPolicy */ class DeadlineQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI DeadlineQosPolicy() @@ -258,8 +265,6 @@ class DeadlineQosPolicy : public Parameter_t, public QosPolicy */ class LatencyBudgetQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI LatencyBudgetQosPolicy() @@ -332,8 +337,6 @@ typedef enum LivelinessQosPolicyKind : rtps::octet */ class LivelinessQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI LivelinessQosPolicy() @@ -404,8 +407,6 @@ typedef enum ReliabilityQosPolicyKind : rtps::octet */ class ReliabilityQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI ReliabilityQosPolicy() @@ -492,8 +493,6 @@ enum OwnershipQosPolicyKind : rtps::octet */ class OwnershipQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI OwnershipQosPolicy() @@ -562,8 +561,6 @@ enum DestinationOrderQosPolicyKind : rtps::octet */ class DestinationOrderQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI DestinationOrderQosPolicy() @@ -616,41 +613,42 @@ class DestinationOrderQosPolicy : public Parameter_t, public QosPolicy /** - * Class UserDataQosPolicy, to transmit user data during the discovery phase. + * Class GenericDataQosPolicy, base class to transmit user data during the discovery phase. */ -class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceLimitedVector +class GenericDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceLimitedVector { - friend class ParameterList; using ResourceLimitedOctetVector = ResourceLimitedVector; public: - RTPS_DllAPI UserDataQosPolicy() - : Parameter_t(PID_USER_DATA, 0) + RTPS_DllAPI GenericDataQosPolicy( + ParameterId_t pid) + : Parameter_t(pid, 0) , QosPolicy(false) , ResourceLimitedOctetVector() { } - RTPS_DllAPI UserDataQosPolicy( + RTPS_DllAPI GenericDataQosPolicy( + ParameterId_t pid, uint16_t in_length) - : Parameter_t(PID_USER_DATA, in_length) + : Parameter_t(pid, in_length) , QosPolicy(false) , ResourceLimitedOctetVector() { } /** - * Construct from another UserDataQosPolicy. + * Construct from another GenericDataQosPolicy. * - * The resulting UserDataQosPolicy will have the same size limits + * The resulting GenericDataQosPolicy will have the same size limits * as the input attribute * * @param data data to copy in the newly created object */ - RTPS_DllAPI UserDataQosPolicy( - const UserDataQosPolicy& data) - : Parameter_t(PID_USER_DATA, data.length) + RTPS_DllAPI GenericDataQosPolicy( + const GenericDataQosPolicy& data) + : Parameter_t(data.Pid, data.length) , QosPolicy(false) , ResourceLimitedOctetVector(data) { @@ -660,20 +658,23 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL * Construct from underlying collection type. * * Useful to easy integration on old APIs where a traditional container was used. - * The resulting UserDataQosPolicy will always be unlimited in size + * The resulting GenericDataQosPolicy will always be unlimited in size * - * @param data data to copy in the newly created object + * @param pid Id of the parameter + * @param data Data to copy in the newly created object */ - RTPS_DllAPI UserDataQosPolicy( + RTPS_DllAPI GenericDataQosPolicy( + ParameterId_t pid, const collection_type& data) - : Parameter_t(PID_USER_DATA, 0) + : Parameter_t(pid, 0) , QosPolicy(false) , ResourceLimitedOctetVector() { assign(data.begin(), data.end()); + length = (size() + 7) & ~3; } - virtual RTPS_DllAPI ~UserDataQosPolicy() + virtual RTPS_DllAPI ~GenericDataQosPolicy() { } @@ -681,14 +682,14 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL * Copies data from underlying collection type. * * Useful to easy integration on old APIs where a traditional container was used. - * The resulting UserDataQosPolicy will keep the current size limit. + * The resulting GenericDataQosPolicy will keep the current size limit. * If the input data is larger than the current limit size, the elements exceeding * that maximum will be silently discarded. * * @param b object to be copied * @return reference to the current object. */ - UserDataQosPolicy& operator =( + GenericDataQosPolicy& operator =( const collection_type& b) { if (collection_ != b) @@ -702,16 +703,16 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL } /** - * Copies another UserDataQosPolicy. + * Copies another GenericDataQosPolicy. * - * The resulting UserDataQosPolicy will have the same size limit + * The resulting GenericDataQosPolicy will have the same size limit * as the input parameter, so all data in the input will be copied. * * @param b object to be copied * @return reference to the current object. */ - UserDataQosPolicy& operator =( - const UserDataQosPolicy& b) + GenericDataQosPolicy& operator =( + const GenericDataQosPolicy& b) { QosPolicy::operator=(b); Parameter_t::operator=(b); @@ -722,7 +723,7 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL } bool operator ==( - const UserDataQosPolicy& b) const + const GenericDataQosPolicy& b) const { return collection_ == b.collection_ && Parameter_t::operator ==(b) && @@ -755,7 +756,7 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL /** * @return const reference to the internal raw data. - * */ + */ inline const std::vector& dataVec() const { return collection_; @@ -777,8 +778,11 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL * @param msg Message to append the QoS Policy to. * @return True if the modified CDRMessage is valid. */ - bool addToCDRMessage( - rtps::CDRMessage_t* msg) const override; + bool inline addToCDRMessage( + rtps::CDRMessage_t* msg) const override + { + return QosPolicy::serialize_generic_data(msg, Pid, collection_); + } /** * Reads QoS from the specified CDR message @@ -786,9 +790,18 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL * @param size Size of the QoS Policy field to read * @return True if the parameter was correctly taken. */ - bool readFromCDRMessage( + bool inline readFromCDRMessage( rtps::CDRMessage_t* msg, - uint16_t size) override; + uint16_t size) override + { + if (QosPolicy::deserialize_generic_data(msg, size, max_size(), collection_)) + { + length = size; + return true; + } + + return false; + } /** * Returns raw data vector. @@ -808,8 +821,83 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL { assign(vec.begin(), vec.end()); } + + /** + * Returns raw data vector. + * @return raw data as vector of octets. + * */ + RTPS_DllAPI inline std::vector getValue() const + { + return collection_; + } + + /** + * Sets raw data vector. + * @param vec raw data to set. + * */ + RTPS_DllAPI inline void setValue( + const std::vector& vec) + { + assign(vec.begin(), vec.end()); + } }; +/** + * Class TemplateDataQosPolicy, base template for user data qos policies. + */ +template +class TemplateDataQosPolicy : public GenericDataQosPolicy +{ +public: + + RTPS_DllAPI TemplateDataQosPolicy() + : GenericDataQosPolicy(TPid) + { + } + + RTPS_DllAPI TemplateDataQosPolicy( + uint16_t in_length) + : GenericDataQosPolicy(TPid, in_length) + { + } + + /** + * Construct from another TemplateDataQosPolicy. + * + * The resulting TemplateDataQosPolicy will have the same size limits + * as the input attribute + * + * @param data data to copy in the newly created object + */ + RTPS_DllAPI TemplateDataQosPolicy( + const TemplateDataQosPolicy& data) + : GenericDataQosPolicy(data) + { + } + + /** + * Construct from underlying collection type. + * + * Useful to easy integration on old APIs where a traditional container was used. + * The resulting TemplateDataQosPolicy will always be unlimited in size + * + * @param data data to copy in the newly created object + */ + RTPS_DllAPI TemplateDataQosPolicy( + const collection_type& data) + : GenericDataQosPolicy(TPid, data) + { + } + + virtual RTPS_DllAPI ~TemplateDataQosPolicy() + { + } +}; + +using UserDataQosPolicy = TemplateDataQosPolicy; +using TopicDataQosPolicy = TemplateDataQosPolicy; +using GroupDataQosPolicy = TemplateDataQosPolicy; + /** * Class TimeBasedFilterQosPolicy, to indicate the Time Based Filter Qos. * This QosPolicy can be defined and is transmitted to the rest of the network but is not implemented in this version. @@ -817,8 +905,6 @@ class UserDataQosPolicy : public Parameter_t, public QosPolicy, public ResourceL */ class TimeBasedFilterQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI TimeBasedFilterQosPolicy() @@ -890,8 +976,6 @@ enum PresentationQosPolicyAccessScopeKind : rtps::octet */ class PresentationQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI PresentationQosPolicy() @@ -1004,7 +1088,6 @@ class Partition_t */ class PartitionQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; friend class rtps::EDP; public: @@ -1279,211 +1362,6 @@ class PartitionQosPolicy : public Parameter_t, public QosPolicy uint32_t Npartitions_; }; - -/** - * Class TopicDataQosPolicy, to indicate the Topic Data. - */ -class TopicDataQosPolicy : public Parameter_t, public QosPolicy -{ - friend class ParameterList; - -public: - - RTPS_DllAPI TopicDataQosPolicy() - : Parameter_t(PID_TOPIC_DATA, 0) - , QosPolicy(false) - , value{} - { - } - - RTPS_DllAPI TopicDataQosPolicy( - uint16_t in_length) - : Parameter_t(PID_TOPIC_DATA, in_length) - , QosPolicy(false) - , value{} - { - } - - virtual RTPS_DllAPI ~TopicDataQosPolicy() - { - } - - bool operator ==( - const TopicDataQosPolicy& b) const - { - return (this->value == b.value) && - Parameter_t::operator ==(b) && - QosPolicy::operator ==(b); - } - - virtual uint32_t cdr_serialized_size() const override - { - return QosPolicy::get_cdr_serialized_size(value); - } - - /** - * Appends QoS to the specified CDR message. - * @param msg Message to append the QoS Policy to. - * @return True if the modified CDRMessage is valid. - */ - bool addToCDRMessage( - rtps::CDRMessage_t* msg) const override; - - /** - * Reads QoS from the specified CDR message - * @param msg Message from where the QoS Policy has to be taken. - * @param size Size of the QoS Policy field to read - * @return True if the parameter was correctly taken. - */ - bool readFromCDRMessage( - rtps::CDRMessage_t* msg, - uint16_t size) override; - - /** - * Appends topic data. - * @param oc Data octet. - */ - RTPS_DllAPI inline void push_back( - rtps::octet oc) - { - value.push_back(oc); - } - - /** - * Clears all topic data. - */ - RTPS_DllAPI inline void clear() override - { - value.clear(); - hasChanged = true; - } - - /** - * Overrides topic data vector. - * @param ocv Topic data octet vector. - */ - RTPS_DllAPI inline void setValue( - std::vector ocv) - { - value = ocv; - } - - /** - * Returns topic data - * @return Vector of data octets. - */ - RTPS_DllAPI inline std::vector getValue() const - { - return value; - } - -private: - - std::vector value; -}; - -/** - * Class GroupDataQosPolicy, to indicate the Group Data. - */ -class GroupDataQosPolicy : public Parameter_t, public QosPolicy -{ - friend class ParameterList; - -public: - - RTPS_DllAPI GroupDataQosPolicy() - : Parameter_t(PID_GROUP_DATA, 0) - , QosPolicy(false) - , value{} - { - } - - RTPS_DllAPI GroupDataQosPolicy( - uint16_t in_length) - : Parameter_t(PID_GROUP_DATA, in_length) - , QosPolicy(false) - , value{} - { - } - - virtual RTPS_DllAPI ~GroupDataQosPolicy() - { - } - - bool operator ==( - const GroupDataQosPolicy& b) const - { - return (this->value == b.value) && - Parameter_t::operator ==(b) && - QosPolicy::operator ==(b); - } - - virtual uint32_t cdr_serialized_size() const override - { - return QosPolicy::get_cdr_serialized_size(value); - } - - /** - * Appends QoS to the specified CDR message. - * @param msg Message to append the QoS Policy to. - * @return True if the modified CDRMessage is valid. - */ - bool addToCDRMessage( - rtps::CDRMessage_t* msg) const override; - - /** - * Reads QoS from the specified CDR message - * @param msg Message from where the QoS Policy has to be taken. - * @param size Size of the QoS Policy field to read - * @return True if the parameter was correctly taken. - */ - bool readFromCDRMessage( - rtps::CDRMessage_t* msg, - uint16_t size) override; - - /** - * Appends group data. - * @param oc Data octet. - */ - RTPS_DllAPI inline void push_back( - rtps::octet oc) - { - value.push_back(oc); - } - - /** - * Clears all group data. - */ - RTPS_DllAPI inline void clear() override - { - value.clear(); - hasChanged = true; - } - - /** - * Overrides group data vector. - * @param ocv Group data octet vector. - */ - RTPS_DllAPI inline void setValue( - std::vector ocv) - { - value = ocv; - } - - /** - * Returns group data - * @return Vector of data octets. - */ - RTPS_DllAPI inline std::vector getValue() const - { - return value; - } - -private: - - std::vector value; -}; - /** * Enum HistoryQosPolicyKind, different kinds of History Qos for HistoryQosPolicy. */ @@ -1500,8 +1378,6 @@ enum HistoryQosPolicyKind : rtps::octet */ class HistoryQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI HistoryQosPolicy() @@ -1564,8 +1440,6 @@ class HistoryQosPolicy : public Parameter_t, public QosPolicy */ class ResourceLimitsQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: int32_t max_samples; @@ -1627,8 +1501,6 @@ class ResourceLimitsQosPolicy : public Parameter_t, public QosPolicy */ class DurabilityServiceQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI DurabilityServiceQosPolicy() @@ -1699,8 +1571,6 @@ class DurabilityServiceQosPolicy : public Parameter_t, public QosPolicy */ class LifespanQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI LifespanQosPolicy() @@ -1757,8 +1627,6 @@ class LifespanQosPolicy : public Parameter_t, public QosPolicy */ class OwnershipStrengthQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI OwnershipStrengthQosPolicy() @@ -1818,8 +1686,6 @@ class OwnershipStrengthQosPolicy : public Parameter_t, public QosPolicy */ class TransportPriorityQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: uint32_t value; @@ -1909,8 +1775,6 @@ typedef enum DataRepresentationId : int16_t */ class DataRepresentationQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: std::vector m_value; @@ -1928,6 +1792,8 @@ class DataRepresentationQosPolicy : public Parameter_t, public QosPolicy std::swap(*this, reset); } + virtual uint32_t cdr_serialized_size() const override; + /** * Appends QoS to the specified CDR message. * @param msg Message to append the QoS Policy to. @@ -1958,8 +1824,6 @@ enum TypeConsistencyKind : uint16_t */ class TypeConsistencyEnforcementQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: TypeConsistencyKind m_kind; @@ -2016,8 +1880,6 @@ class TypeConsistencyEnforcementQosPolicy : public Parameter_t, public QosPolicy */ class DisablePositiveACKsQosPolicy : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: RTPS_DllAPI DisablePositiveACKsQosPolicy() @@ -2077,8 +1939,6 @@ class DisablePositiveACKsQosPolicy : public Parameter_t, public QosPolicy */ class TypeIdV1 : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: types::TypeIdentifier m_type_identifier; @@ -2166,8 +2026,6 @@ class TypeIdV1 : public Parameter_t, public QosPolicy */ class TypeObjectV1 : public Parameter_t, public QosPolicy { - friend class ParameterList; - public: types::TypeObject m_type_object; diff --git a/src/cpp/qos/QosPolicies.cpp b/src/cpp/qos/QosPolicies.cpp index 94149d2f8e2..bf75fc1c988 100644 --- a/src/cpp/qos/QosPolicies.cpp +++ b/src/cpp/qos/QosPolicies.cpp @@ -39,6 +39,55 @@ uint32_t QosPolicy::get_cdr_serialized_size( return 2 + 2 + 4 + data_size; } +bool QosPolicy::serialize_generic_data( + rtps::CDRMessage_t* msg, + uint16_t pid, + const std::vector& data) +{ + bool valid = CDRMessage::addUInt16(msg, pid); + uint16_t siz = static_cast(data.size()); + siz = (siz + 3) & ~3; + valid &= CDRMessage::addUInt16(msg, static_cast(4 + siz)); + valid &= CDRMessage::addOctetVector(msg, &data, true); + return valid; +} + +bool QosPolicy::deserialize_generic_data( + rtps::CDRMessage_t* msg, + uint16_t size, + size_t max_size, + std::vector& data) +{ + uint32_t pos_ref = msg->pos; + + // Read size of data + uint32_t len; + if (!CDRMessage::readUInt32(msg, &len)) + { + return false; + } + + if ( (len + sizeof(uint32_t) > size) // Exceeds parameter length + || (len > max_size) ) // Exceeds size limit + { + return false; + } + + // Either the data is size limited and already has max_size() allocated + // or it is not limited and we resize if needed + data.resize(len); + if (!CDRMessage::readData(msg, data.data(), len)) + { + return false; + } + + // Skip padding + msg->pos += ( (len + 3) & ~3) - len; + + // Should have consumed whole size + return (pos_ref + size == msg->pos); +} + bool DurabilityQosPolicy::addToCDRMessage( CDRMessage_t* msg) const { @@ -371,82 +420,6 @@ bool PartitionQosPolicy::readFromCDRMessage( return valid; } -bool UserDataQosPolicy::addToCDRMessage( - CDRMessage_t* msg) const -{ - bool valid = CDRMessage::addUInt16(msg, Pid); - uint32_t siz = (uint32_t)size(); - uint32_t align = ((siz + 3) & ~3) - siz; - valid &= CDRMessage::addUInt16(msg, static_cast(4 + siz)); - valid &= CDRMessage::addUInt32(msg, siz); - valid &= CDRMessage::addData(msg, collection_.data(), siz); - for (uint32_t count = 0; count < align; ++count) - { - valid &= CDRMessage::addOctet(msg, 0); - } - - return valid; -} - -bool UserDataQosPolicy::readFromCDRMessage( - CDRMessage_t* msg, - uint16_t size) -{ - if (size > max_size()) - { - return false; - } - length = size; - - //Either the data is size limited and already has max_size() allocated - // or it is not limited and readOctedVector will resize if needed - return CDRMessage::readOctetVector(msg, &collection_); -} - -bool TopicDataQosPolicy::addToCDRMessage( - CDRMessage_t* msg) const -{ - bool valid = CDRMessage::addUInt16(msg, this->Pid); - valid &= CDRMessage::addUInt16(msg, this->length); - valid &= CDRMessage::addOctetVector(msg, &value); - return valid; -} - -bool TopicDataQosPolicy::readFromCDRMessage( - CDRMessage_t* msg, - uint16_t size) -{ - length = size; - - uint32_t pos_ref = msg->pos; - bool valid = CDRMessage::readOctetVector(msg, &value); - uint32_t length_diff = msg->pos - pos_ref; - valid &= (size == length_diff); - return valid; -} - -bool GroupDataQosPolicy::addToCDRMessage( - CDRMessage_t* msg) const -{ - bool valid = CDRMessage::addUInt16(msg, this->Pid); - valid &= CDRMessage::addUInt16(msg, this->length); - valid &= CDRMessage::addOctetVector(msg, &value); - return valid; -} - -bool GroupDataQosPolicy::readFromCDRMessage( - CDRMessage_t* msg, - uint16_t size) -{ - length = size; - - uint32_t pos_ref = msg->pos; - bool valid = CDRMessage::readOctetVector(msg, &value); - uint32_t length_diff = msg->pos - pos_ref; - valid &= (size == length_diff); - return valid; -} - bool HistoryQosPolicy::addToCDRMessage( CDRMessage_t* msg) const { @@ -610,13 +583,33 @@ bool TransportPriorityQosPolicy::readFromCDRMessage( return CDRMessage::readUInt32(msg, &value); } +uint32_t DataRepresentationQosPolicy::cdr_serialized_size() const +{ + // Size of data + uint32_t data_size = static_cast(m_value.size() * sizeof(uint16_t)); + // Align to next 4 byte + data_size = (data_size + 3) & ~3; + // p_id + p_length + data_size + data + return 2 + 2 + 4 + data_size; +} + bool DataRepresentationQosPolicy::addToCDRMessage( CDRMessage_t* msg) const { - bool valid = CDRMessage::addUInt32(msg, (uint32_t)m_value.size()); - for (const DataRepresentationId_t& it : m_value) + bool valid = CDRMessage::addUInt16(msg, this->Pid); + + uint16_t len = static_cast(m_value.size() * sizeof(uint16_t)) + 4; + len = (len + 3) & ~3; + + valid &= CDRMessage::addUInt16(msg, len); + valid &= CDRMessage::addUInt32(msg, static_cast(m_value.size())); + for (const DataRepresentationId_t& id : m_value) { - valid &= CDRMessage::addUInt16(msg, it); + valid &= CDRMessage::addUInt16(msg, static_cast(id)); + } + if (m_value.size() % 2 == 1) // Odd, we must align + { + valid &= CDRMessage::addUInt16(msg, uint16_t(0)); } return valid; } @@ -631,13 +624,18 @@ bool DataRepresentationQosPolicy::readFromCDRMessage( int16_t temp(0); uint32_t datasize(0); bool valid = CDRMessage::readUInt32(msg, &datasize); - for (uint32_t i = 0; i < datasize; ++i) + valid &= (datasize * sizeof(uint16_t)) <= (size - sizeof(uint32_t)); + + for (uint32_t i = 0; valid && (i < datasize); ++i) { valid &= CDRMessage::readInt16(msg, &temp); m_value.push_back(static_cast(temp)); } - uint32_t length_diff = msg->pos - pos_ref; - valid &= (size == length_diff); + if (valid) + { + msg->pos = pos_ref + size; + } + return valid; } diff --git a/test/blackbox/BlackboxTestsPubSubBasic.cpp b/test/blackbox/BlackboxTestsPubSubBasic.cpp index 0df9fa77792..d58f56945c3 100644 --- a/test/blackbox/BlackboxTestsPubSubBasic.cpp +++ b/test/blackbox/BlackboxTestsPubSubBasic.cpp @@ -336,7 +336,7 @@ TEST_P(PubSubBasic, ReceivedDynamicDataWithinSizeLimit) R"( - 8 + 4 28 @@ -378,7 +378,7 @@ TEST_P(PubSubBasic, ReceivedUserDataExceedsSizeLimit) R"( - 8 + 4 )";