Skip to content

Commit

Permalink
chore: report error upon receiving overflowing echo data (#759)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardapeters authored Oct 31, 2024
1 parent 631ec6c commit 05ab9f0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
1 change: 1 addition & 0 deletions protobuf/echo/EchoErrorPolicy.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "protobuf/echo/EchoErrorPolicy.hpp"
#include <cstdlib>

namespace services
{
Expand Down
9 changes: 1 addition & 8 deletions protobuf/echo/EchoErrorPolicy.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
#ifndef PROTOBUF_ECHO_ERROR_POLICY_HPP
#define PROTOBUF_ECHO_ERROR_POLICY_HPP

#include "infra/stream/BufferingStreamReader.hpp"
#include "infra/util/BoundedDeque.hpp"
#include "infra/util/Compatibility.hpp"
#include "infra/util/Function.hpp"
#include "infra/util/Observer.hpp"
#include "infra/util/Optional.hpp"
#include "protobuf/echo/Proto.hpp"
#include "protobuf/echo/Serialization.hpp"
#include <cstdint>

namespace services
{
Expand Down
2 changes: 2 additions & 0 deletions protobuf/echo/ProtoMessageReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ namespace services
while (!stream.Empty())
{
auto range = infra::ReinterpretCastMemoryRange<const typename C::value_type>(stream.ContiguousRange());
stream.ErrorPolicy().ReportResult(range.size() <= value.max_size() - value.size());
range.shrink_from_back_to(value.max_size() - value.size());
value.insert(value.end(), range.begin(), range.end());
}
});
Expand Down
12 changes: 12 additions & 0 deletions protobuf/echo/test/TestProtoMessageReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,21 @@ TEST(ProtoMessageReceiverTest, parse_bytes)
infra::StdVectorInputStreamReader::WithStorage data(infra::inPlace, std::initializer_list<uint8_t>{ 10, 2, 5, 6 });
receiver.Feed(data);

EXPECT_FALSE(receiver.Failed());
EXPECT_EQ((infra::BoundedVector<uint8_t>::WithMaxSize<10>{ { 5, 6 } }), receiver.message.value);
}

TEST(ProtoMessageReceiverTest, parse_bytes_too_long)
{
services::ProtoMessageReceiver<test_messages::TestBytes> receiver;

infra::StdVectorInputStreamReader::WithStorage data(infra::inPlace, std::initializer_list<uint8_t>{ 10, 51, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1 });
receiver.Feed(data);

EXPECT_TRUE(receiver.Failed());
EXPECT_EQ((infra::BoundedVector<uint8_t>::WithMaxSize<50>{ { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } }), receiver.message.value);
}

TEST(ProtoMessageReceiverTest, parse_message)
{
services::ProtoMessageReceiver<test_messages::TestMessageWithMessageField> receiver;
Expand Down

0 comments on commit 05ab9f0

Please sign in to comment.