From 8d06d097bc313eb8483bddfe716ccf39c50366f5 Mon Sep 17 00:00:00 2001 From: Ara Ayvazyan Date: Tue, 22 May 2018 20:47:35 -0700 Subject: [PATCH] Fix broken ByteBuffer copy ctor --- src/cpp/util/byte_buffer_cc.cc | 5 +++-- test/cpp/util/byte_buffer_test.cc | 7 +++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/cpp/util/byte_buffer_cc.cc b/src/cpp/util/byte_buffer_cc.cc index 8700f96d8dc..a7e16454352 100644 --- a/src/cpp/util/byte_buffer_cc.cc +++ b/src/cpp/util/byte_buffer_cc.cc @@ -43,8 +43,9 @@ Status ByteBuffer::Dump(std::vector* slices) const { return Status::OK; } -ByteBuffer::ByteBuffer(const ByteBuffer& buf) - : buffer_(grpc_byte_buffer_copy(buf.buffer_)) {} +ByteBuffer::ByteBuffer(const ByteBuffer& buf) : buffer_(nullptr) { + operator=(buf); +} ByteBuffer& ByteBuffer::operator=(const ByteBuffer& buf) { if (this != &buf) { diff --git a/test/cpp/util/byte_buffer_test.cc b/test/cpp/util/byte_buffer_test.cc index 47a5b7f03a2..b48a53eed1d 100644 --- a/test/cpp/util/byte_buffer_test.cc +++ b/test/cpp/util/byte_buffer_test.cc @@ -38,6 +38,13 @@ const char* kContent2 = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy world"; class ByteBufferTest : public ::testing::Test {}; +TEST_F(ByteBufferTest, CopyCtor) { + ByteBuffer buffer1; + EXPECT_FALSE(buffer1.Valid()); + ByteBuffer buffer2 = buffer1; + EXPECT_FALSE(buffer2.Valid()); +} + TEST_F(ByteBufferTest, CreateFromSingleSlice) { Slice s(kContent1); ByteBuffer buffer(&s, 1);