From da755fbed2b3975b2abe44ac03bc433ff939fbdd Mon Sep 17 00:00:00 2001 From: Freddie Lamble Date: Tue, 24 Nov 2026 08:38:24 +0000 Subject: [PATCH] More graceful handling of invalid timestamps ### Problem statement A timestamp that is not a valid `bdlt::Datetime` failed the decode of the whole content header, which `Framer::appendFrame` reported as a channel exception and `Connection` escalated to a connection level syntax error. ### Proposed changes Handle it as the unsupported `DECIMAL` field value already is: consume the field so that whatever follows it still decodes, log the error, and leave the value unset. `decodeTimestamp` takes a `bsl::optional` and returns false only when the field could not be read, which is the case that leaves the read position meaningless, so a truncated read is still a decode failure. --- src/rmq/rmqamqpt/rmqamqpt_basicproperties.cpp | 4 +- src/rmq/rmqamqpt/rmqamqpt_types.cpp | 30 +++++-- src/rmq/rmqamqpt/rmqamqpt_types.h | 18 +++- src/tests/rmqamqp/rmqamqp_framer.t.cpp | 70 +++++++++++++++ .../rmqamqpt/rmqamqpt_basicproperties.t.cpp | 28 ++++++ src/tests/rmqamqpt/rmqamqpt_types.t.cpp | 87 +++++++++++++++---- 6 files changed, 211 insertions(+), 26 deletions(-) diff --git a/src/rmq/rmqamqpt/rmqamqpt_basicproperties.cpp b/src/rmq/rmqamqpt/rmqamqpt_basicproperties.cpp index f4e06cf9..9a5353f3 100644 --- a/src/rmq/rmqamqpt/rmqamqpt_basicproperties.cpp +++ b/src/rmq/rmqamqpt/rmqamqpt_basicproperties.cpp @@ -341,9 +341,7 @@ bool BasicProperties::decode(BasicProperties* props, } } if (flags & maskForProperty(TIMESTAMP)) { - properties.timestamp = bdlt::Datetime(); - if (!rmqamqpt::Types::decodeTimestamp(&*properties.timestamp, - &buffer)) { + if (!rmqamqpt::Types::decodeTimestamp(&properties.timestamp, &buffer)) { BALL_LOG_ERROR << "Decoding fail for basic property: " << PROPERTY_NAMES[TIMESTAMP]; success = false; diff --git a/src/rmq/rmqamqpt/rmqamqpt_types.cpp b/src/rmq/rmqamqpt/rmqamqpt_types.cpp index f90ca5b2..9725ec60 100644 --- a/src/rmq/rmqamqpt/rmqamqpt_types.cpp +++ b/src/rmq/rmqamqpt/rmqamqpt_types.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -291,11 +292,16 @@ bool Types::decodeFieldValue(rmqt::FieldValue* outValue, *outValue = val; } break; case rmqamqpt::FieldValue::TIMESTAMP: { - bdlt::Datetime timestamp; + bsl::optional timestamp; if (!decodeTimestamp(×tamp, buffer)) { return false; } - *outValue = timestamp; + if (timestamp.has_value()) { + *outValue = timestamp.value(); + } + else { + outValue->reset(); + } } break; case rmqamqpt::FieldValue::FIELD_TABLE: { bsl::shared_ptr table = @@ -550,7 +556,8 @@ void Types::encodeTimestamp(Writer& output, const bdlt::Datetime& timestamp) bdlt::EpochUtil::convertToTimeT64(timestamp))); } -bool Types::decodeTimestamp(bdlt::Datetime* timestamp, rmqamqpt::Buffer* buffer) +bool Types::decodeTimestamp(bsl::optional* timestamp, + rmqamqpt::Buffer* buffer) { BSLS_ASSERT(timestamp); @@ -561,12 +568,23 @@ bool Types::decodeTimestamp(bdlt::Datetime* timestamp, rmqamqpt::Buffer* buffer) return false; } - if (0 != bdlt::EpochUtil::convertFromTimeT64(timestamp, seconds)) { - BALL_LOG_ERROR << "Timestamp not representable as a datetime: " + bdlt::Datetime value; + const bool representable = + 0 == bdlt::EpochUtil::convertFromTimeT64(&value, seconds); + + BSLS_REVIEW_OPT(representable && + "AMQP timestamp outside the bdlt::Datetime range"); + + if (!representable) { + BALL_LOG_ERROR << "Timestamp not representable as a datetime, " + "decoding as unset: " << seconds; - return false; + timestamp->reset(); + return true; } + *timestamp = value; + return true; } diff --git a/src/rmq/rmqamqpt/rmqamqpt_types.h b/src/rmq/rmqamqpt/rmqamqpt_types.h index 55c092af..28c4be87 100644 --- a/src/rmq/rmqamqpt/rmqamqpt_types.h +++ b/src/rmq/rmqamqpt/rmqamqpt_types.h @@ -23,11 +23,13 @@ #include #include +#include #include #include #include #include +#include #include #include @@ -57,6 +59,12 @@ class Types { static void encodeByteVector(Writer& output, const bsl::vector& data); + /// Read a field value out of `buffer` and advance past it. + /// + /// A value this library cannot represent is still consumed, so that + /// whatever follows it decodes, and `value` is left unset. An unset + /// value cannot be told apart from the `NO_FIELD` ('V') type, so + /// re-encoding writes it back out as 'V' and the original is lost. static bool decodeFieldValue(rmqt::FieldValue* value, rmqamqpt::Buffer* buffer); @@ -75,7 +83,15 @@ class Types { static void encodeTimestamp(Writer& output, const bdlt::Datetime& timestamp); - static bool decodeTimestamp(bdlt::Datetime* timestamp, + /// Read an AMQP `timestamp` out of `buffer` and advance past it. + /// + /// Return false when there is not enough data for the field, leaving + /// `buffer` untouched. Nothing further can be decoded from it. + /// + /// Return true when the field was read, leaving `buffer` positioned at + /// whatever follows it. A value outside the range a `bdlt::Datetime` + /// can hold is not a decode failure: `timestamp` is left unset. + static bool decodeTimestamp(bsl::optional* timestamp, rmqamqpt::Buffer* buffer); template diff --git a/src/tests/rmqamqp/rmqamqp_framer.t.cpp b/src/tests/rmqamqp/rmqamqp_framer.t.cpp index 466eb904..730ce371 100644 --- a/src/tests/rmqamqp/rmqamqp_framer.t.cpp +++ b/src/tests/rmqamqp/rmqamqp_framer.t.cpp @@ -76,6 +76,32 @@ class ContentDecodeTests : public ::testing::Test { return rmqamqpt::Frame(frameType, channel, data); } + /// Build a content header frame around an already encoded payload. + rmqamqpt::Frame makeRawHeaderFrame(uint16_t channel, + const uint8_t* payload, + bsl::size_t payloadSize) + { + bsl::shared_ptr > data = + bsl::make_shared >(); + + rmqamqpt::Writer writer(data.get()); + rmqamqpt::Types::write( + writer, static_cast(rmqamqpt::Constants::HEADER)); + rmqamqpt::Types::write(writer, bdlb::BigEndianUint16::make(channel)); + rmqamqpt::Types::write(writer, + bdlb::BigEndianUint32::make(payloadSize)); + + for (bsl::size_t i = 0; i < payloadSize; ++i) { + rmqamqpt::Types::write(writer, payload[i]); + } + rmqamqpt::Types::write(writer, rmqamqpt::Constants::FRAME_END); + + assert(data->size() == + rmqamqpt::Frame::calculateFrameSize(payloadSize)); + + return rmqamqpt::Frame(rmqamqpt::Constants::HEADER, channel, data); + } + rmqamqpt::Frame makeBodyFrame(rmqamqpt::Constants::AMQPFrameType frameType, uint16_t channel, const rmqamqpt::ContentBody& contentBody) @@ -348,6 +374,50 @@ TEST_F(ContentDecodeTests, ContentHeaderBodyEncodeDecode) EXPECT_EQ(memcmp(message.payload(), data, 5), 0); } +TEST_F(ContentDecodeTests, + ContentHeaderUnrepresentableTimestampIsNotAnException) +{ + // A timestamp the decoder cannot represent is a limit of the type + // decoded into, not a protocol error, so it must not fail the channel. + // The encoder cannot express this header, so it is written by hand. + bsl::vector payload; + rmqamqpt::Writer payloadWriter(&payload); + rmqamqpt::Types::write(payloadWriter, + bdlb::BigEndianUint16::make(static_cast( + rmqamqpt::Constants::BASIC))); + rmqamqpt::Types::write(payloadWriter, bdlb::BigEndianUint16::make(0)); + rmqamqpt::Types::write(payloadWriter, bdlb::BigEndianUint64::make(5)); + rmqamqpt::Types::write(payloadWriter, + bdlb::BigEndianUint16::make(0x0040)); // timestamp + rmqamqpt::Types::write(payloadWriter, + bdlb::BigEndianInt64::make(1795442484034LL)); + + uint16_t channel; + rmqamqp::Framer framer; + rmqamqp::Message received; + + rmqamqpt::Frame frame = + makeRawHeaderFrame(2, payload.data(), payload.size()); + + EXPECT_THAT(framer.appendFrame(&channel, &received, frame), + Eq(rmqamqp::Framer::PARTIAL)); + EXPECT_EQ(channel, 2); + + const uint8_t* body = reinterpret_cast("hello"); + rmqamqpt::ContentBody contentBody(body, 5); + frame = makeBodyFrame(rmqamqpt::Constants::BODY, 2, contentBody); + + EXPECT_THAT(framer.appendFrame(&channel, &received, frame), + Eq(rmqamqp::Framer::OK)); + EXPECT_EQ(channel, 2); + + ASSERT_TRUE(received.is()); + rmqt::Message message = received.the(); + EXPECT_EQ(message.payloadSize(), 5); + EXPECT_EQ(memcmp(message.payload(), body, 5), 0); + EXPECT_TRUE(message.properties().timestamp.isNull()); +} + TEST_F(ContentDecodeTests, ContentBodyWithoutHeader) { rmqamqpt::Frame frame; diff --git a/src/tests/rmqamqpt/rmqamqpt_basicproperties.t.cpp b/src/tests/rmqamqpt/rmqamqpt_basicproperties.t.cpp index d1570132..8b52c68c 100644 --- a/src/tests/rmqamqpt/rmqamqpt_basicproperties.t.cpp +++ b/src/tests/rmqamqpt/rmqamqpt_basicproperties.t.cpp @@ -565,6 +565,34 @@ TEST(Methods_BasicProperties, TruncatedScalarPropertiesFailToDecode) } } +TEST(Methods_BasicProperties, OutOfRangeTimestampKeepsOtherProperties) +{ + // A timestamp in milliseconds is out of range read as seconds. The + // properties either side of it must survive. The encoder cannot + // express this, so the property block is written by hand. + const uint16_t flags = 0x0080 | 0x0040 | 0x0008; + + bsl::vector data; + rmqamqpt::Writer writer(&data); + rmqamqpt::Types::write(writer, bdlb::BigEndianUint16::make(flags)); + rmqamqpt::Types::encodeShortString(writer, "mid"); + rmqamqpt::Types::write(writer, bdlb::BigEndianInt64::make(1795442484034LL)); + rmqamqpt::Types::encodeShortString(writer, "app"); + + rmqamqpt::BasicProperties basicProps; + EXPECT_THAT(rmqamqpt::BasicProperties::decode( + &basicProps, data.data(), data.size()), + Eq(true)); + + EXPECT_FALSE(basicProps.timestamp()); + + ASSERT_TRUE(basicProps.messageId()); + EXPECT_THAT(basicProps.messageId().value(), Eq("mid")); + + ASSERT_TRUE(basicProps.appId()); + EXPECT_THAT(basicProps.appId().value(), Eq("app")); +} + TEST(Methods_BasicProperties, TruncatedHeaderTableLengthFailsToDecode) { // headers flag, with a partial field table length prefix diff --git a/src/tests/rmqamqpt/rmqamqpt_types.t.cpp b/src/tests/rmqamqpt/rmqamqpt_types.t.cpp index 11453cae..d629bfcd 100644 --- a/src/tests/rmqamqpt/rmqamqpt_types.t.cpp +++ b/src/tests/rmqamqpt/rmqamqpt_types.t.cpp @@ -22,10 +22,13 @@ #include #include +#include + #include #include #include #include +#include #include #include @@ -609,11 +612,12 @@ TEST(TypesEncoding, Timestamp) EXPECT_THAT(storage[i], Eq(inspect[i])); } - bdlt::Datetime decoded; + bsl::optional decoded; rmqamqpt::Buffer buf(storage.data(), storage.size()); EXPECT_THAT(rmqamqpt::Types::decodeTimestamp(&decoded, &buf), Eq(true)); - EXPECT_THAT(decoded, Eq(millennium)); + ASSERT_TRUE(decoded.has_value()); + EXPECT_THAT(decoded.value(), Eq(millennium)); } TEST(TypesTruncatedDecode, TruncatedTimestampFails) @@ -621,7 +625,7 @@ TEST(TypesTruncatedDecode, TruncatedTimestampFails) const uint8_t truncated[] = {0x00, 0x00, 0x00, 0x00, 0x38, 0x6D, 0x43}; for (bsl::size_t length = 0; length < sizeof(truncated); ++length) { - bdlt::Datetime decoded; + bsl::optional decoded; rmqamqpt::Buffer buffer(truncated, length); EXPECT_THAT(rmqamqpt::Types::decodeTimestamp(&decoded, &buffer), @@ -631,32 +635,83 @@ TEST(TypesTruncatedDecode, TruncatedTimestampFails) } } -TEST(TypesTruncatedDecode, OutOfRangeTimestampFails) +// A timestamp in milliseconds rather than the seconds the protocol +// specifies. Any millisecond stamp after 1978-01-11 is out of range. +const bsls::Types::Int64 MILLISECOND_TIMESTAMP = 1795442484034LL; + +TEST(TypesTruncatedDecode, OutOfRangeTimestampDecodesAsUnset) { - // Seconds since the epoch that no bdlt::Datetime can represent - const uint8_t cases[][8] = { - {0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}, - {0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; + const bsls::Types::Int64 cases[] = { + bsl::numeric_limits::max(), + bsl::numeric_limits::min(), + MILLISECOND_TIMESTAMP}; for (bsl::size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); ++i) { - bdlt::Datetime decoded; - rmqamqpt::Buffer buffer(cases[i], sizeof(cases[i])); + BufferType storage; + rmqamqpt::Writer writer(&storage); + rmqamqpt::Types::write(writer, bdlb::BigEndianInt64::make(cases[i])); + + // Starts set, to show it is cleared + bsl::optional decoded = bdlt::Datetime(2000, 1, 1); + rmqamqpt::Buffer buffer(storage.data(), storage.size()); EXPECT_THAT(rmqamqpt::Types::decodeTimestamp(&decoded, &buffer), - Eq(false)) + Eq(true)) << "case: " << i; + EXPECT_FALSE(decoded.has_value()) << "case: " << i; + EXPECT_THAT(buffer.available(), Eq(0u)) << "case: " << i; } } -TEST(TypesTruncatedDecode, OutOfRangeTimestampFieldValueFails) +TEST(TypesTruncatedDecode, OutOfRangeTimestampFieldValueDecodesAsUnset) { - const uint8_t data[] = { - 'T', 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; + BufferType storage; + rmqamqpt::Writer writer(&storage); + rmqamqpt::Types::write(writer, rmqamqpt::FieldValue::TIMESTAMP); + rmqamqpt::Types::write(writer, + bdlb::BigEndianInt64::make(MILLISECOND_TIMESTAMP)); rmqt::FieldValue value; - rmqamqpt::Buffer buffer(data, sizeof(data)); + rmqamqpt::Buffer buffer(storage.data(), storage.size()); + + EXPECT_THAT(rmqamqpt::Types::decodeFieldValue(&value, &buffer), Eq(true)); + EXPECT_TRUE(value.isUnset()); + EXPECT_THAT(buffer.available(), Eq(0u)); +} + +TEST(TypesFieldTableDecode, OutOfRangeTimestampAndLaterFieldsStillDecode) +{ + // The 8 octets must still be consumed so that whatever follows the + // timestamp in the table is read from the right place. + BufferType fields; + rmqamqpt::Writer fieldWriter(&fields); + rmqamqpt::Types::encodeShortString(fieldWriter, "at"); + rmqamqpt::Types::write(fieldWriter, rmqamqpt::FieldValue::TIMESTAMP); + rmqamqpt::Types::write(fieldWriter, + bdlb::BigEndianInt64::make(MILLISECOND_TIMESTAMP)); + rmqamqpt::Types::encodeShortString(fieldWriter, "x"); + rmqamqpt::Types::write(fieldWriter, rmqamqpt::FieldValue::SHORT_SHORT_INT); + rmqamqpt::Types::write(fieldWriter, static_cast(7)); + + BufferType storage; + rmqamqpt::Writer writer(&storage); + rmqamqpt::Types::write( + writer, + bdlb::BigEndianUint32::make(static_cast(fields.size()))); + for (bsl::size_t i = 0; i < fields.size(); ++i) { + rmqamqpt::Types::write(writer, fields[i]); + } + + rmqamqpt::Buffer buffer(storage.data(), storage.size()); + rmqt::FieldTable ft; - EXPECT_THAT(rmqamqpt::Types::decodeFieldValue(&value, &buffer), Eq(false)); + EXPECT_THAT(rmqamqpt::Types::decodeFieldTable(&ft, &buffer), Eq(true)); + EXPECT_THAT(ft.size(), Eq(2u)); + + EXPECT_TRUE(ft["at"].isUnset()); + + EXPECT_TRUE(ft["x"].is()); + EXPECT_THAT(ft["x"].the(), Eq(7)); } TEST(TypesFieldTableDecode, DecimalIsSkippedAndLaterFieldsStillDecode)