Skip to content

Commit

Permalink
HPackDecoder incorrectly calculates required buffer length causing G1…
Browse files Browse the repository at this point in the history
… Humongous Allocation and OOM (#465)
  • Loading branch information
crazylulululu authored Apr 29, 2024
1 parent b069e52 commit 6d568cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ void decodeString(final ByteArrayBuffer buffer, final ByteBuffer src) throws HPa
}
}

int getTmpBufSize() {
return tmpBuf == null ? 0 : tmpBuf.capacity();
}

private void clearState() {

if (this.tmpBuf != null) {
Expand All @@ -182,7 +186,7 @@ private void ensureCapacity(final int extra) {
if (this.tmpBuf == null) {
this.tmpBuf = CharBuffer.allocate(Math.max(256, extra));
}
final int requiredCapacity = this.tmpBuf.remaining() + extra;
final int requiredCapacity = this.tmpBuf.position() + extra;
if (requiredCapacity > this.tmpBuf.capacity()) {
expandCapacity(requiredCapacity);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,23 @@ public void testBasicStringCoding() throws Exception {
Assertions.assertEquals("this and that and Huffman", strBuf.toString());
}

@Test
public void testEnsureCapacity() throws Exception {

final HPackEncoder encoder = new HPackEncoder(StandardCharsets.US_ASCII);
final HPackDecoder decoder = new HPackDecoder(StandardCharsets.UTF_8);

final ByteArrayBuffer buffer = new ByteArrayBuffer(16);
encoder.encodeString(buffer, "this and that", false);

final StringBuilder strBuf = new StringBuilder();
for (int i = 0; i < 1000; i++) {
decoder.decodeString(wrap(buffer), strBuf);
strBuf.delete(0,strBuf.length());
}
Assertions.assertEquals(decoder.getTmpBufSize(), 256);
}

static final int SWISS_GERMAN_HELLO[] = {
0x47, 0x72, 0xFC, 0x65, 0x7A, 0x69, 0x5F, 0x7A, 0xE4, 0x6D, 0xE4
};
Expand Down

0 comments on commit 6d568cc

Please sign in to comment.