Do not depend on the implementation detail of Unpooled.buffer(int) when accessing backing array. (#8865)

Motivation:

We should not depend on the implementation detail of Unpooled.buffer(int) to allocate the exact size of backing byte[] as depending on the implementation it may return a buffer with a bigger backing array.

Modifications:

Explicit allocate the byte[] and wrap it in the ByteBuf. This way we are sure that ByteBuf.array() returns an byte[] which has the exact length and content we expect.

Result:

More correct and safe usage of ByteBuf.array()
This commit is contained in:
Norman Maurer 2019-02-15 09:38:36 -08:00
parent 23190bd787
commit d036b24a4b

View File

@ -152,7 +152,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
int b = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key2).replaceAll("")) / int b = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key2).replaceAll("")) /
BEGINNING_SPACE.matcher(key2).replaceAll("").length()); BEGINNING_SPACE.matcher(key2).replaceAll("").length());
long c = req.content().readLong(); long c = req.content().readLong();
ByteBuf input = Unpooled.buffer(16); ByteBuf input = Unpooled.wrappedBuffer(new byte[16]).setIndex(0, 0);
input.writeInt(a); input.writeInt(a);
input.writeInt(b); input.writeInt(b);
input.writeLong(c); input.writeLong(c);