netty: prevent IndexOutOfBoundsException when no handler is passed to BufferingHandler

This commit is contained in:
Jihun Cho 2019-02-13 11:48:59 -08:00
parent 8e6fa122a6
commit 5f60f22b6a
2 changed files with 27 additions and 1 deletions

View File

@ -465,7 +465,7 @@ final class ProtocolNegotiators {
* This check is necessary as a channel may be registered with different event loops during it
* lifetime and we only want to configure it once.
*/
if (handlers != null) {
if (handlers != null && handlers.length > 0) {
for (ChannelHandler handler : handlers) {
ctx.pipeline().addBefore(ctx.name(), null, handler);
}

View File

@ -32,6 +32,7 @@ import io.grpc.Grpc;
import io.grpc.SecurityLevel;
import io.grpc.internal.GrpcAttributes;
import io.grpc.internal.testing.TestUtils;
import io.grpc.netty.ProtocolNegotiators.AbstractBufferingHandler;
import io.grpc.netty.ProtocolNegotiators.HostPort;
import io.grpc.netty.ProtocolNegotiators.ServerTlsHandler;
import io.grpc.netty.ProtocolNegotiators.TlsNegotiator;
@ -584,6 +585,24 @@ public class ProtocolNegotiatorsTest {
elg.shutdownGracefully();
}
@Test(expected = Test.None.class /* no exception expected */)
@SuppressWarnings("TestExceptionChecker")
public void bufferingHandler_shouldNotThrowForEmptyHandler() throws Exception {
LocalAddress addr = new LocalAddress("local");
ChannelFuture unused = new Bootstrap()
.channel(LocalChannel.class)
.handler(new BufferingHandlerWithoutHandlers())
.group(group)
.register().sync();
ChannelFuture sf = new ServerBootstrap()
.channel(LocalServerChannel.class)
.childHandler(new ChannelHandlerAdapter() {})
.group(group)
.bind(addr);
// sync will trigger client's NoHandlerBufferingHandler which should not throw
sf.sync();
}
private static class FakeGrpcHttp2ConnectionHandler extends GrpcHttp2ConnectionHandler {
static GrpcHttp2ConnectionHandler noopHandler() {
@ -629,4 +648,11 @@ public class ProtocolNegotiatorsTest {
private static ByteBuf bb(String s, Channel c) {
return ByteBufUtil.writeUtf8(c.alloc(), s);
}
private static class BufferingHandlerWithoutHandlers extends AbstractBufferingHandler {
public BufferingHandlerWithoutHandlers(ChannelHandler... handlers) {
super(handlers);
}
}
}