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
45 changes: 43 additions & 2 deletions sbe-tool/src/main/cpp/otf/OtfMessageEncoder.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2025 Real Logic Limited.
* Copyright 2013-2026 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -70,6 +70,36 @@ class BasicTokenEncoder
virtual void onVarData(Token &, char *, std::uint64_t, Token &) {}
};

/*
* Largest count or length that a numInGroup or var-data length field can carry: the schema maxValue when set,
* otherwise the range of its primitive type. putAsUInt narrows to the field width, so a larger value would put a
* different count or length on the wire than the number of entries or bytes that follow it.
*/
inline std::uint64_t maxUIntValue(const Encoding &encoding)
{
std::uint64_t typeMax = UINT64_MAX;
bool isUnsigned = true;
switch (encoding.primitiveType())
{
case PrimitiveType::UINT8: typeMax = UINT8_MAX; break;
case PrimitiveType::UINT16: typeMax = UINT16_MAX; break;
case PrimitiveType::UINT32: typeMax = UINT32_MAX; break;
case PrimitiveType::INT8: typeMax = INT8_MAX; isUnsigned = false; break;
case PrimitiveType::INT16: typeMax = INT16_MAX; isUnsigned = false; break;
case PrimitiveType::INT32: typeMax = INT32_MAX; isUnsigned = false; break;
case PrimitiveType::INT64: typeMax = INT64_MAX; isUnsigned = false; break;
default: break;
}

const PrimitiveValue &maxValue = encoding.maxValue();
if (isUnsigned && 0 != maxValue.size() && maxValue.getAsUInt() < typeMax)
{
return maxValue.getAsUInt();
}

return typeMax;
}

template<typename TokenEncoder>
static void encodeComposite(
char *buffer,
Expand Down Expand Up @@ -223,11 +253,17 @@ static std::size_t encodeData(
}

const std::uint64_t dataLength = encoder.varDataLength(token);
if (dataLength > maxUIntValue(lengthToken.encoding()))
{
throw std::runtime_error("var data length too large for length type");
}

lengthToken.encoding().putAsUInt(buffer + bufferIndex + lengthToken.offset(), dataLength);

bufferIndex += dataToken.offset();

if ((bufferIndex + dataLength) > length)
// bufferIndex <= length after the prefix check above, so this subtraction cannot wrap.
if (dataLength > static_cast<std::uint64_t>(length - bufferIndex))
{
throw std::runtime_error("length too short for data field");
}
Expand Down Expand Up @@ -272,6 +308,11 @@ static std::pair<std::size_t, std::size_t> encodeGroups(
auto blockLength = static_cast<std::size_t>(token.encodedLength());
const std::uint64_t numInGroup = isPresent ? encoder.numInGroup(token) : 0;

if (numInGroup > maxUIntValue(numInGroupToken.encoding()))
{
throw std::runtime_error("numInGroup too large for numInGroup type");
}

if (isPresent)
{
if ((bufferIndex + dimensionsLength) > length)
Expand Down
1 change: 1 addition & 0 deletions sbe-tool/src/test/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ target_link_libraries(OtfMessageEncoderTest sbe gmock_main ${CMAKE_THREAD_LIBS_I
add_test(
NAME OtfMessageEncoderTest
COMMAND OtfMessageEncoderTest ${CXX_CODEC_TARGET_DIR}/code-generation-schema.sbeir
${CXX_CODEC_TARGET_DIR}/uint64-length-schema.sbeir
WORKING_DIRECTORY ${CXX_CODEC_TARGET_DIR})
# ---------------------------------------------------------------------------------------------------
# sbe.cpp.generate.enum.parse: compile-and-run test for the generated fromString()/operator>>.
Expand Down
110 changes: 109 additions & 1 deletion sbe-tool/src/test/cpp/otf/OtfMessageEncoderTest.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2025 Real Logic Limited.
* Copyright 2013-2026 Real Logic Limited.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,9 +16,11 @@

#include <cstdint>
#include <cstring>
#include <limits>
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "gtest/gtest.h"
Expand All @@ -38,6 +40,7 @@ using namespace code::generation::test;

// IR (.sbeir) for the example schema, produced by SbeTool; supplied on argv or $SBE_EXAMPLE_IR.
static std::string g_irFile;
static std::string g_uint64IrFile = "uint64-length-schema.sbeir";

// =================================================================================================
// Encoding::put* / putAs* <-> get* / getAs*
Expand Down Expand Up @@ -340,6 +343,107 @@ TEST_F(OtfMessageEncoderTest, ManyGroupEntriesAndNestedGroups)
assertOtfRoundTripIsIdentical(m_buffer, sizeof(m_buffer));
}

// =================================================================================================
// A count or length that does not fit in its wire field is rejected rather than truncated
// =================================================================================================
namespace {

struct OversizedEncoder : public OtfMessageEncoder::BasicTokenEncoder
{
std::string name;
std::uint64_t value;
OversizedEncoder(std::string n, std::uint64_t v) : name(std::move(n)), value(v) {}

std::uint64_t numInGroup(Token &token) override { return token.name() == name ? value : 0; }
std::uint64_t varDataLength(Token &token) override { return token.name() == name ? value : 0; }
};

std::string encodeCarError(OversizedEncoder &encoder, std::size_t capacity)
{
IrDecoder ir;
EXPECT_EQ(ir.decode(g_irFile.c_str()), 0) << "failed to load IR: " << g_irFile;
auto msgTokens = ir.message(Car::sbeTemplateId(), Car::sbeSchemaVersion());
EXPECT_TRUE(msgTokens);

std::vector<char> buffer(capacity, 0);
try
{
OtfMessageEncoder::encode(
buffer.data(), buffer.size(), Car::sbeSchemaVersion(), Car::sbeBlockLength(), msgTokens, encoder);
}
catch (const std::runtime_error &e)
{
return e.what();
}

return "";
}

} // namespace

TEST_F(OtfMessageEncoderTest, RejectsNumInGroupTooLargeForNumInGroupType)
{
// room for every entry, so only the uint16 numInGroup can reject the count
OversizedEncoder encoder("fuelFigures", 65536);
EXPECT_NE(encodeCarError(encoder, 4 * 1024 * 1024).find("numInGroup too large"), std::string::npos);
}

TEST_F(OtfMessageEncoderTest, RejectsVarDataLengthTooLargeForLengthType)
{
OversizedEncoder encoder("manufacturer", 65536);
EXPECT_NE(encodeCarError(encoder, 1024 * 1024).find("var data length too large"), std::string::npos);
}

TEST_F(OtfMessageEncoderTest, RejectsVarDataLengthAboveSchemaMaxValue)
{
// the uint32 length of color has maxValue 1073741824
OversizedEncoder encoder("color", 1073741825);
EXPECT_NE(encodeCarError(encoder, 8192).find("var data length too large"), std::string::npos);
}

// A uint64 length has no tighter wire-field bound, so UINT64_MAX passes the range check. The remaining bounds
// check must compare against the remaining buffer without adding to bufferIndex, which would wrap.
namespace {

struct HugeVarDataEncoder : public OtfMessageEncoder::BasicTokenEncoder
{
std::uint64_t varDataLength(Token &) override { return std::numeric_limits<std::uint64_t>::max(); }
};

std::string encodeUint64Error(OtfMessageEncoder::BasicTokenEncoder &encoder)
{
IrDecoder ir;
EXPECT_EQ(ir.decode(g_uint64IrFile.c_str()), 0) << "failed to load IR: " << g_uint64IrFile;
auto msgTokens = ir.message(1, 0);
EXPECT_TRUE(msgTokens);

std::vector<char> buffer(8192, 0);
try
{
OtfMessageEncoder::encode(buffer.data(), buffer.size(), 0, 4, msgTokens, encoder);
}
catch (const std::runtime_error &e)
{
return e.what();
}

return "";
}

} // namespace

TEST(OtfUint64LengthEncoderTest, RejectsVarDataLengthThatWouldWrap)
{
if (g_uint64IrFile.empty())
{
GTEST_SKIP() << "uint64-length schema IR not available";
}

HugeVarDataEncoder encoder;
EXPECT_NE(
encodeUint64Error(encoder).find("length too short for data field"), std::string::npos);
}

// =================================================================================================
// OtfHeaderEncoder writes a header that OtfHeaderDecoder reads back unchanged
// =================================================================================================
Expand Down Expand Up @@ -378,5 +482,9 @@ int main(int argc, char **argv)
{
g_irFile = argv[1];
}
if (argc > 2)
{
g_uint64IrFile = argv[2];
}
return RUN_ALL_TESTS();
}
Loading