From 1c6191c16636c162b21b38106e9a6289abbbcc7c Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Fri, 15 Feb 2019 09:38:36 -0800 Subject: [PATCH] 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() --- .../codec/http/websocketx/WebSocketServerHandshaker00.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java index 1d06d9cc06..994cd09103 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java @@ -152,7 +152,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { int b = (int) (Long.parseLong(BEGINNING_DIGIT.matcher(key2).replaceAll("")) / BEGINNING_SPACE.matcher(key2).replaceAll("").length()); 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(b); input.writeLong(c);