Skip to content

Commit

Permalink
Validity checks on read_encapsulation [10576] (#104)
Browse files Browse the repository at this point in the history
* Refs 10547. Add test to check DDS encapsulation.

Signed-off-by: Miguel Company <[email protected]>

* Refs 10547. Add test to check Corba encapsulation.

Signed-off-by: Miguel Company <[email protected]>

* Refs 10547. Perform checks on Cdr::read_encapsulation.

Signed-off-by: Miguel Company <[email protected]>

* Refs 10576. Fix warning on aarch64.

Signed-off-by: Miguel Company <[email protected]>
  • Loading branch information
MiguelCompany authored Feb 15, 2021
1 parent 14f8934 commit 4bf0cfa
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 14 deletions.
29 changes: 15 additions & 14 deletions src/cpp/Cdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ Cdr& Cdr::read_encapsulation()
if (m_cdrType == DDS_CDR)
{
(*this) >> dummy;
if (0 != dummy)
{
throw BadParamException("Unexpected non-zero initial byte received in Cdr::read_encapsulation");
}
}

// Get the ecampsulation byte.
Expand All @@ -84,28 +88,25 @@ Cdr& Cdr::read_encapsulation()
m_swapBytes = !m_swapBytes;
m_endianness = (encapsulationKind & 0x1);
}
}
catch (Exception& ex)
{
setState(state_before_error);
ex.raise();
}

// If it is DDS_CDR type, view if contains a parameter list.
if (encapsulationKind & DDS_CDR_WITH_PL)
{
// Check encapsulationKind correctness
uint8_t allowed_kind_mask = LITTLE_ENDIANNESS;
if (m_cdrType == DDS_CDR)
{
m_plFlag = DDS_CDR_WITH_PL;
allowed_kind_mask |= DDS_CDR_WITH_PL;
}
else

if (0 != (encapsulationKind & ~allowed_kind_mask))
{
throw BadParamException("Unexpected CDR type received in Cdr::read_encapsulation");
}
}

try
{
// If it is DDS_CDR type, view if contains a parameter list.
if ((encapsulationKind & DDS_CDR_WITH_PL) && ((m_cdrType == DDS_CDR)))
{
m_plFlag = DDS_CDR_WITH_PL;
}

if (m_cdrType == DDS_CDR)
{
(*this) >> m_options;
Expand Down
83 changes: 83 additions & 0 deletions test/SimpleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@

#include <fastcdr/Cdr.h>
#include <fastcdr/FastCdr.h>

#include <fastcdr/exceptions/BadParamException.h>
#include <fastcdr/exceptions/Exception.h>
#include <fastcdr/exceptions/NotEnoughMemoryException.h>

#include <stdio.h>
#include <limits>
Expand Down Expand Up @@ -363,6 +366,86 @@ TEST(FastBufferTests, Constructors)
}
}

TEST(CDRTests, DDSEncapsulation)
{
char encapsulation[4]{ 0, 0, 0, 0 };
eprosima::fastcdr::FastBuffer buffer(encapsulation, 4);
eprosima::fastcdr::Cdr cdr(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::DDS_CDR);

// First encapsulation byte should be 0
{
for (uint16_t i = 1; i < 256; ++i)
{
cdr.reset();
encapsulation[0] = static_cast<char>(i & 0xFF);
EXPECT_THROW(cdr.read_encapsulation(), eprosima::fastcdr::exception::BadParamException);
}

cdr.reset();
encapsulation[0] = 0;
EXPECT_NO_THROW(cdr.read_encapsulation());
}

std::array<bool, 256> valid_encapsulations;
valid_encapsulations.fill(false);

// Valid representation identifiers from table 10.3
valid_encapsulations[0x00] = true; // CDR_BE
valid_encapsulations[0x01] = true; // CDR_LE
valid_encapsulations[0x02] = true; // PL_CDR_BE
valid_encapsulations[0x03] = true; // PL_CDR_LE

// TODO(Miguel C): Change when more encapsulations are supported
// valid_encapsulations[0x10] = true; // CDR2_BE
// valid_encapsulations[0x11] = true; // CDR2_LE
// valid_encapsulations[0x12] = true; // PL_CDR2_BE
// valid_encapsulations[0x13] = true; // PL_CDR2_LE
// valid_encapsulations[0x14] = true; // D_CDR_BE
// valid_encapsulations[0x15] = true; // D_CDR_LE
// valid_encapsulations[0x04] = true; // XML

for (uint16_t i = 0; i < 256; ++i)
{
cdr.reset();
encapsulation[1] = static_cast<char>(i & 0xFF);
if (valid_encapsulations[i])
{
EXPECT_NO_THROW(cdr.read_encapsulation());
}
else
{
EXPECT_THROW(cdr.read_encapsulation(), eprosima::fastcdr::exception::BadParamException);
}
}
}

TEST(CDRTests, CorbaEncapsulation)
{
char encapsulation[1]{ 0 };
eprosima::fastcdr::FastBuffer buffer(encapsulation, 1);
eprosima::fastcdr::Cdr cdr(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN, eprosima::fastcdr::Cdr::CORBA_CDR);

std::array<bool, 256> valid_encapsulations;
valid_encapsulations.fill(false);

valid_encapsulations[0x00] = true; // BIG_ENDIAN
valid_encapsulations[0x01] = true; // LITTLE_ENDIAN

for (uint16_t i = 0; i < 256; ++i)
{
cdr.reset();
encapsulation[0] = static_cast<char>(i & 0xFF);
if (valid_encapsulations[i])
{
EXPECT_NO_THROW(cdr.read_encapsulation());
}
else
{
EXPECT_THROW(cdr.read_encapsulation(), eprosima::fastcdr::exception::BadParamException);
}
}
}

TEST(CDRTests, Octet)
{
check_good_case(octet_t);
Expand Down

0 comments on commit 4bf0cfa

Please sign in to comment.