Skip to content

Commit

Permalink
Fix bug PackedForwardBuffer#append can throw BufferOverflow exception
Browse files Browse the repository at this point in the history
  • Loading branch information
komamitsu committed May 30, 2016
1 parent aaa1e92 commit 599e0fd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Expand Up @@ -50,15 +50,17 @@ private RetentionBuffer prepareBuffer(String tag, int writeSize)
return retentionBuffer;
}

int existingDataSize = 0;
int newRetentionBufferSize;
if (retentionBuffer == null) {
newRetentionBufferSize = bufferConfig.getInitialBufferSize();
}
else{
existingDataSize = retentionBuffer.getByteBuffer().position();
newRetentionBufferSize = (int) (retentionBuffer.getByteBuffer().capacity() * bufferConfig.getBufferExpandRatio());
}

while (newRetentionBufferSize < writeSize) {
while (newRetentionBufferSize < (writeSize + existingDataSize)) {
newRetentionBufferSize *= bufferConfig.getBufferExpandRatio();
}

Expand Down
Expand Up @@ -66,4 +66,35 @@ public void testGetBufferedDataSize()
buffer.flush(sender, true);
assertThat(buffer.getBufferedDataSize(), is(0L));
}

@Test
public void testAppendIfItDoesNotThrowBufferOverflow()
throws IOException
{
PackedForwardBuffer buffer = new PackedForwardBuffer.Config().setInitialBufferSize(64 * 1024).createInstance();

StringBuilder buf = new StringBuilder();

for (int i = 0; i < 1024 * 60; i++) {
buf.append('x');
}
String str60kb = buf.toString();

for (int i = 0; i < 1024 * 40; i++) {
buf.append('x');
}
String str100kb = buf.toString();

{
Map<String, Object> map = new HashMap<String, Object>();
map.put("k", str60kb);
buffer.append("tag0", new Date().getTime(), map);
}

{
Map<String, Object> map = new HashMap<String, Object>();
map.put("k", str100kb);
buffer.append("tag0", new Date().getTime(), map);
}
}
}

0 comments on commit 599e0fd

Please sign in to comment.