Skip to content
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
15 changes: 15 additions & 0 deletions source/common/common/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include <chrono>
#include <cmath>
#include <cstdint>
#include <ios>
#include <iostream>
#include <iterator>
#include <regex>
#include <string>
Expand Down Expand Up @@ -221,6 +223,19 @@ std::string DateFormatter::now(TimeSource& time_source) {
return fromTime(time_source.systemTime());
}

MutableMemoryStreamBuffer::MutableMemoryStreamBuffer(char* base, size_t size) {
this->setp(base, base + size);
}

OutputBufferStream::OutputBufferStream(char* data, size_t size)
: MutableMemoryStreamBuffer{data, size}, std::ostream{static_cast<std::streambuf*>(this)} {}

int OutputBufferStream::bytesWritten() const { return pptr() - pbase(); }

absl::string_view OutputBufferStream::contents() const {
return absl::string_view(pbase(), bytesWritten());
}

ConstMemoryStreamBuffer::ConstMemoryStreamBuffer(const char* data, size_t size) {
// std::streambuf won't modify `data`, but the interface still requires a char* for convenience,
// so we need to const_cast.
Expand Down
27 changes: 27 additions & 0 deletions source/common/common/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <chrono>
#include <cstdint>
#include <ios>
#include <set>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -110,6 +111,32 @@ class RealTimeSource : public TimeSource {
MonotonicTime monotonicTime() override { return std::chrono::steady_clock::now(); }
};

/**
* Class used for creating non-memory allocating std::ostream.
*/
class MutableMemoryStreamBuffer : public std::streambuf {
public:
MutableMemoryStreamBuffer(char* base, size_t size);
};

/**
* std::ostream class that serializes writes into the provided buffer.
*/
class OutputBufferStream : private MutableMemoryStreamBuffer, public std::ostream {
public:
OutputBufferStream(char* data, size_t size);

/**
* @return the number of bytes written prior to the "put" pointer into the buffer.
*/
int bytesWritten() const;

/**
* @return a string view of the written bytes.
*/
absl::string_view contents() const;
};

/**
* Class used for creating non-copying std::istream's. See InputConstMemoryStream below.
*/
Expand Down
1 change: 1 addition & 0 deletions test/common/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ envoy_cc_test(
],
deps = [
"//source/common/common:utility_lib",
"//test/common/stats:stat_test_utility_lib",
"//test/test_common:simulated_time_system_lib",
"//test/test_common:test_time_lib",
"//test/test_common:utility_lib",
Expand Down
56 changes: 56 additions & 0 deletions test/common/common/utility_test.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <array>
#include <chrono>
#include <cmath>
#include <cstdint>
Expand All @@ -8,6 +9,7 @@

#include "common/common/utility.h"

#include "test/common/stats/stat_test_utility.h"
#include "test/test_common/simulated_time_system.h"
#include "test/test_common/test_time.h"
#include "test/test_common/utility.h"
Expand Down Expand Up @@ -116,6 +118,60 @@ TEST(DateUtil, NowToMilliseconds) {
EXPECT_EQ(12345067, DateUtil::nowToMilliseconds(test_time));
}

TEST(OutputBufferStream, FailsOnWriteToEmptyBuffer) {
constexpr char data = 'x';
OutputBufferStream ostream{nullptr, 0};
ASSERT_TRUE(ostream.good());

ostream << data;

EXPECT_TRUE(ostream.bad());
}

TEST(OutputBufferStream, CanWriteToBuffer) {
constexpr char data[] = "123";
std::array<char, 3> buffer;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forget if ASAN is able to find issues with data members like this allocated in the stack. Consider instead:

std::unique_ptr<char[]> buffer(new char[3]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ASAN doesn't complain about it.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you were to do something like:

OutputBufferStream ostream{buffer.data(), buffer.size()+1};
ostream << "lots of bytes";

Would ASAN detect the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes ASAN detected that.

We're saying the buffer is bigger than it is (i.e. buffer.size() + 1) and trying to write to memory we don't have in that case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great. std::array works for me.


OutputBufferStream ostream{buffer.data(), buffer.size()};
ASSERT_EQ(ostream.bytesWritten(), 0);

ostream << data;

EXPECT_EQ(ostream.contents(), data);
EXPECT_EQ(ostream.bytesWritten(), 3);
}

TEST(OutputBufferStream, CannotOverwriteBuffer) {
constexpr char data[] = "123";
std::array<char, 2> buffer;

OutputBufferStream ostream{buffer.data(), buffer.size()};
ASSERT_EQ(ostream.bytesWritten(), 0);

// Initial write should stop before overflowing.
ostream << data << std::endl;
EXPECT_EQ(ostream.contents(), "12");
EXPECT_EQ(ostream.bytesWritten(), 2);

// Try a subsequent write, which shouldn't change anything since
// the buffer is full.
ostream << data << std::endl;
EXPECT_EQ(ostream.contents(), "12");
EXPECT_EQ(ostream.bytesWritten(), 2);
}

TEST(OutputBufferStream, DoesNotAllocateMemoryEvenIfWeTryToOverflowBuffer) {
constexpr char data[] = "123";
std::array<char, 2> buffer;
Stats::TestUtil::MemoryTest memory_test;

OutputBufferStream ostream{buffer.data(), buffer.size()};
ostream << data << std::endl;

EXPECT_EQ(memory_test.consumedBytes(), 0);
EXPECT_EQ(ostream.contents(), "12");
}

TEST(InputConstMemoryStream, All) {
{
InputConstMemoryStream istream{nullptr, 0};
Expand Down