@@ -851,27 +851,39 @@ private void wrap(ChannelHandlerContext ctx, boolean inUnwrap) throws SSLExcepti |
851 | 851 |
|
852 | 852 | SSLEngineResult result; |
853 | 853 |
|
854 | | - if (buf.readableBytes() > MAX_PLAINTEXT_LENGTH) { |
855 | | - // If we pulled a buffer larger than the supported packet size, we can slice it up and iteratively, |
856 | | - // encrypting multiple packets into a single larger buffer. This substantially saves on allocations |
857 | | - // for large responses. Here we estimate how large of a buffer we need. If we overestimate a bit, |
858 | | - // that's fine. If we underestimate, we'll simply re-enqueue the remaining buffer and get it on the |
859 | | - // next outer loop. |
860 | | - int readableBytes = buf.readableBytes(); |
861 | | - int numPackets = readableBytes / MAX_PLAINTEXT_LENGTH; |
862 | | - if (readableBytes % MAX_PLAINTEXT_LENGTH != 0) { |
863 | | - numPackets += 1; |
864 | | - } |
| 854 | + try { |
| 855 | + if (buf.readableBytes() > MAX_PLAINTEXT_LENGTH) { |
| 856 | + // If we pulled a buffer larger than the supported packet size, we can slice it up and |
| 857 | + // iteratively, encrypting multiple packets into a single larger buffer. This substantially |
| 858 | + // saves on allocations for large responses. Here we estimate how large of a buffer we need. |
| 859 | + // If we overestimate a bit, that's fine. If we underestimate, we'll simply re-enqueue the |
| 860 | + // remaining buffer and get it on the next outer loop. |
| 861 | + int readableBytes = buf.readableBytes(); |
| 862 | + int numPackets = readableBytes / MAX_PLAINTEXT_LENGTH; |
| 863 | + if (readableBytes % MAX_PLAINTEXT_LENGTH != 0) { |
| 864 | + numPackets += 1; |
| 865 | + } |
865 | 866 |
|
866 | | - if (out == null) { |
867 | | - out = allocateOutNetBuf(ctx, readableBytes, buf.nioBufferCount() + numPackets); |
868 | | - } |
869 | | - result = wrapMultiple(alloc, engine, buf, out); |
870 | | - } else { |
871 | | - if (out == null) { |
872 | | - out = allocateOutNetBuf(ctx, buf.readableBytes(), buf.nioBufferCount()); |
| 867 | + if (out == null) { |
| 868 | + out = allocateOutNetBuf(ctx, readableBytes, buf.nioBufferCount() + numPackets); |
| 869 | + } |
| 870 | + result = wrapMultiple(alloc, engine, buf, out); |
| 871 | + } else { |
| 872 | + if (out == null) { |
| 873 | + out = allocateOutNetBuf(ctx, buf.readableBytes(), buf.nioBufferCount()); |
| 874 | + } |
| 875 | + result = wrap(alloc, engine, buf, out); |
873 | 876 | } |
874 | | - result = wrap(alloc, engine, buf, out); |
| 877 | + } catch (SSLException e) { |
| 878 | + // Either wrapMultiple(...) or wrap(...) did throw. In this case we need to release the buffer |
| 879 | + // that we removed from pendingUnencryptedWrites before failing the promise and rethrowing it. |
| 880 | + // Failing to do so would result in a buffer leak. |
| 881 | + // See https://github.com/netty/netty/issues/14644 |
| 882 | + // |
| 883 | + // We don't need to release out here as this is done in a finally block already. |
| 884 | + buf.release(); |
| 885 | + promise.setFailure(e); |
| 886 | + throw e; |
875 | 887 | } |
876 | 888 |
|
877 | 889 | if (buf.isReadable()) { |
|