Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/cpp/Cdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,17 @@ Cdr& Cdr::read_encapsulation()

if (0 == alignment)
{
// The option alignment counts padding bytes at the end of
// the stream. A truncated buffer may not actually contain
// them; moving end_ back past the read cursor would make
// the remaining length underflow and later reads run off
// the buffer. Refuse such input instead.
if ((end_ - offset_) < option_align)
{
throw NotEnoughMemoryException(
NotEnoughMemoryException::NOT_ENOUGH_MEMORY_MESSAGE_DEFAULT);
}

end_ -= option_align;
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/cdr/SimpleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,34 @@ TEST(CDRTests, DDSEncapsulation)
}
}

TEST(CDRTests, DDSEncapsulationTruncatedOptionAlignment)
{
// Regression test: an encapsulation header whose options request trailing
// alignment padding on a buffer that is too short to contain it used to
// move end_ before the read cursor. The remaining length then underflowed
// and later reads ran off the buffer (segfault). It must be refused.
{
// 4-byte header only: PLAIN_CDR_BE, options {0x00, 0x01} -> align 1,
// but no payload byte for the claimed padding.
char encapsulation[4]{ 0, 0, 0, 1 };
eprosima::fastcdr::FastBuffer buffer(encapsulation, sizeof(encapsulation));
eprosima::fastcdr::Cdr cdr(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
EXPECT_THROW(cdr.read_encapsulation(),
eprosima::fastcdr::exception::NotEnoughMemoryException);
}

{
// Same option alignment, but the buffer is long enough to hold the
// padding: read_encapsulation must still succeed.
char encapsulation[8]{ 0, 0, 0, 1, 0, 0, 0, 0 };
eprosima::fastcdr::FastBuffer buffer(encapsulation, sizeof(encapsulation));
eprosima::fastcdr::Cdr cdr(buffer, eprosima::fastcdr::Cdr::DEFAULT_ENDIAN,
eprosima::fastcdr::CdrVersion::DDS_CDR);
EXPECT_NO_THROW(cdr.read_encapsulation());
}
}

TEST(CDRTests, CorbaEncapsulation)
{
char encapsulation[1]{ 0 };
Expand Down