spring-framework
spring-framework copied to clipboard
org.springframework.core.io.buffer.NettyDataBuffer.retainedSlice fails to release the sliced ByteBuf if ByteBuf.retainedSlice succeeds but allocation of the NettyDataBuffer fails
Here is the current implementation of NettyDataBuffer.retainedSlice: https://github.com/spring-projects/spring-framework/blob/a41f97bc2bd547dbb635f032413fe586968912f8/spring-core/src/main/java/org/springframework/core/io/buffer/NettyDataBuffer.java#L265-L269
The above version of NettyDataBuffer.retainedSlice will fail to release the slice returned by this.byteBuf.retainedSlice if this.byteBuf.retainedSlice succeeds but allocation of the new NettyDataBuffer fails.
Here is how NettyDataBuffer.retainedSlice should be fixed:
@Override
public NettyDataBuffer retainedSlice(int index, int length) {
ByteBuf slice = this.byteBuf.retainedSlice(index, length);
try {
return new NettyDataBuffer(slice, this.dataBufferFactory);
} catch(final Throwable ex) {
// Make sure that the slice is released if the allocation of NettyDataBuffer fails
try {
slice.release();
} catch(IllegalReferenceCountException ex2) {
// Ignore any IllegalReferenceCountException that gets thrown by slice.release()
}
throw ex;
}
}
The fixed version of NettyDataBuffer.retainedSlice will ensure that the slice is released if this.byteBuf.retainedSlice succeeds but the allocation of the NettyDataBuffer fails.