Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: report error upon receiving overflowing echo data #759

Merged
merged 1 commit into from
Oct 31, 2024
Merged
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
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
Loading