Skip to content

Commit

Permalink
Fixed StatisticsSubmessageData unaligned access (#3253)
Browse files Browse the repository at this point in the history
* Refs #16671: Replaced casting and direct access with memcpy block

Signed-off-by: Javier Santiago <javiersantiago@eprosima.com>

* Added new reference accessor for fraction in Time_t

Signed-off-by: Javier Santiago <javiersantiago@eprosima.com>

Signed-off-by: Javier Santiago <javiersantiago@eprosima.com>
(cherry picked from commit 5591a85)

Co-authored-by: jsantiago-eProsima <90755661+jsantiago-eProsima@users.noreply.github.com>
  • Loading branch information
mergify[bot] and jsan-rt authored Feb 24, 2023
1 parent 37db467 commit 1f08239
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
5 changes: 5 additions & 0 deletions include/fastdds/rtps/common/Time_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ class RTPS_DllAPI Time_t
*/
uint32_t fraction() const;

/**
* Retrieve the fraction field by ref.
*/
uint32_t& fraction();

/**
* Sets fraction field and updates the nanoseconds.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/cpp/rtps/common/Time_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ uint32_t Time_t::fraction() const
return fraction_;
}

uint32_t& Time_t::fraction()
{
return fraction_;
}

void Time_t::fraction(
uint32_t frac)
{
Expand Down
26 changes: 19 additions & 7 deletions src/cpp/statistics/rtps/messages/RTPSStatisticsMessages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
#ifndef _STATISTICS_RTPS_MESSAGES_RTPSSTATISTICSMESSAGES_HPP_
#define _STATISTICS_RTPS_MESSAGES_RTPSSTATISTICSMESSAGES_HPP_

#include <cstddef>
#include <cstdint>
#include <cstring>

#include <fastdds/rtps/common/CDRMessage_t.h>
#include <fastdds/rtps/common/Types.h>
Expand Down Expand Up @@ -193,16 +195,26 @@ inline void set_statistics_submessage_from_transport(
statistics_pos += RTPSMESSAGE_SUBMESSAGEHEADER_SIZE;

// Set current timestamp and sequence
auto submessage = (StatisticsSubmessageData*)(&send_buffer[statistics_pos]);
auto current_pos = &send_buffer[statistics_pos];
Time_t ts;
Time_t::now(ts);

submessage->destination = destination;
submessage->ts.seconds = ts.seconds();
submessage->ts.fraction = ts.fraction();
submessage->seq.sequence = sequence.sequence;
submessage->seq.bytes = sequence.bytes;
submessage->seq.bytes_high = sequence.bytes_high;
/*
* This set of memcpy blocks is intended to prevent an undefined behavior caused when casting from an octet* to a StatisticsSubmessageData*
* since these classes have different alignment.
*/

memcpy((char*)current_pos + offsetof(StatisticsSubmessageData, destination), &destination, sizeof(destination));
memcpy((char*)current_pos + offsetof(StatisticsSubmessageData, ts.seconds), &ts.seconds(),
sizeof(StatisticsSubmessageData::ts.seconds));
memcpy((char*)current_pos + offsetof(StatisticsSubmessageData, ts.fraction), &ts.fraction(),
sizeof(StatisticsSubmessageData::ts.fraction));
memcpy((char*)current_pos + offsetof(StatisticsSubmessageData, seq.sequence), &sequence.sequence,
sizeof(sequence.sequence));
memcpy((char*)current_pos + offsetof(StatisticsSubmessageData, seq.bytes), &sequence.bytes,
sizeof(sequence.bytes));
memcpy((char*)current_pos + offsetof(StatisticsSubmessageData, seq.bytes_high), &sequence.bytes_high,
sizeof(sequence.bytes_high));
}
#endif // FASTDDS_STATISTICS
}
Expand Down

0 comments on commit 1f08239

Please sign in to comment.