Skip to content

Commit 1ca8596

Browse files
Jianfei Hulizan
authored andcommittedFeb 20, 2020
buffer: draining any zero byte fragments (envoyproxy#9837) (#109) (#123)
Given that we allow creating zero byte fragments, it'd be good to proactively drain them. For example if someone is doing timing instrumentation and wants to know when Network::Connection data is written to the kernel, it could be useful to have a zero byte sentinel. Risk Level: Low (I don't think anyone is adding zero byte fragments yet) Testing: new unit test Docs Changes: n/a Release Notes: n/a Signed-off-by: Alyssa Wilk <alyssar@chromium.org> Signed-off-by: Jianfei Hu <jianfeih@google.com> Co-authored-by: Lizan Zhou <lizan@tetrate.io>
1 parent d6bdad8 commit 1ca8596

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed
 

‎source/common/buffer/buffer_impl.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@ void OwnedImpl::drain(uint64_t size) {
213213
}
214214
}
215215
}
216+
// Make sure to drain any zero byte fragments that might have been added as
217+
// sentinels for flushed data.
218+
while (!slices_.empty() && slices_.front()->dataSize() == 0) {
219+
slices_.pop_front();
220+
}
216221
}
217222

218223
uint64_t OwnedImpl::getRawSlices(RawSlice* out, uint64_t out_size) const {

‎test/common/buffer/owned_impl_test.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,24 @@ TEST_P(OwnedImplTest, AddBufferFragmentWithCleanup) {
7070
EXPECT_TRUE(release_callback_called_);
7171
}
7272

73+
TEST_P(OwnedImplTest, AddEmptyFragment) {
74+
char input[] = "hello world";
75+
BufferFragmentImpl frag1(input, 11, [](const void*, size_t, const BufferFragmentImpl*) {});
76+
BufferFragmentImpl frag2("", 0, [this](const void*, size_t, const BufferFragmentImpl*) {
77+
release_callback_called_ = true;
78+
});
79+
Buffer::OwnedImpl buffer;
80+
buffer.addBufferFragment(frag1);
81+
EXPECT_EQ(11, buffer.length());
82+
83+
buffer.addBufferFragment(frag2);
84+
EXPECT_EQ(11, buffer.length());
85+
86+
buffer.drain(11);
87+
EXPECT_EQ(0, buffer.length());
88+
EXPECT_TRUE(release_callback_called_);
89+
}
90+
7391
TEST_P(OwnedImplTest, AddBufferFragmentDynamicAllocation) {
7492
char input_stack[] = "hello world";
7593
char* input = new char[11];

0 commit comments

Comments
 (0)