@@ -734,27 +734,39 @@ private void wrap(ChannelHandlerContext ctx, boolean inUnwrap) throws SSLExcepti |
734 | 734 |
|
735 | 735 | SSLEngineResult result; |
736 | 736 |
|
737 | | - if (buf.readableBytes() > MAX_PLAINTEXT_LENGTH) { |
738 | | - // If we pulled a buffer larger than the supported packet size, we can slice it up and iteratively, |
739 | | - // encrypting multiple packets into a single larger buffer. This substantially saves on allocations |
740 | | - // for large responses. Here we estimate how large of a buffer we need. If we overestimate a bit, |
741 | | - // that's fine. If we underestimate, we'll simply re-enqueue the remaining buffer and get it on the |
742 | | - // next outer loop. |
743 | | - int readableBytes = buf.readableBytes(); |
744 | | - int numPackets = readableBytes / MAX_PLAINTEXT_LENGTH; |
745 | | - if (readableBytes % MAX_PLAINTEXT_LENGTH != 0) { |
746 | | - numPackets += 1; |
747 | | - } |
| 737 | + try { |
| 738 | + if (buf.readableBytes() > MAX_PLAINTEXT_LENGTH) { |
| 739 | + // If we pulled a buffer larger than the supported packet size, we can slice it up and |
| 740 | + // iteratively, encrypting multiple packets into a single larger buffer. This substantially |
| 741 | + // saves on allocations for large responses. Here we estimate how large of a buffer we need. |
| 742 | + // If we overestimate a bit, that's fine. If we underestimate, we'll simply re-enqueue the |
| 743 | + // remaining buffer and get it on the next outer loop. |
| 744 | + int readableBytes = buf.readableBytes(); |
| 745 | + int numPackets = readableBytes / MAX_PLAINTEXT_LENGTH; |
| 746 | + if (readableBytes % MAX_PLAINTEXT_LENGTH != 0) { |
| 747 | + numPackets += 1; |
| 748 | + } |
748 | 749 |
|
749 | | - if (out == null) { |
750 | | - out = allocateOutNetBuf(ctx, readableBytes, buf.countReadableComponents() + numPackets); |
751 | | - } |
752 | | - result = wrapMultiple(engine, buf, out); |
753 | | - } else { |
754 | | - if (out == null) { |
755 | | - out = allocateOutNetBuf(ctx, buf.readableBytes(), buf.countReadableComponents()); |
| 750 | + if (out == null) { |
| 751 | + out = allocateOutNetBuf(ctx, readableBytes, buf.countReadableComponents() + numPackets); |
| 752 | + } |
| 753 | + result = wrapMultiple(engine, buf, out); |
| 754 | + } else { |
| 755 | + if (out == null) { |
| 756 | + out = allocateOutNetBuf(ctx, buf.readableBytes(), buf.countReadableComponents()); |
| 757 | + } |
| 758 | + result = wrap(engine, buf, out); |
756 | 759 | } |
757 | | - result = wrap(engine, buf, out); |
| 760 | + } catch (SSLException e) { |
| 761 | + // Either wrapMultiple(...) or wrap(...) did throw. In this case we need to close the buffer |
| 762 | + // that we removed from pendingUnencryptedWrites before failing the promise and rethrowing it. |
| 763 | + // Failing to do so would result in a buffer leak. |
| 764 | + // See https://github.com/netty/netty/issues/14644 |
| 765 | + // |
| 766 | + // We don't need to close out here as this is done in a finally block already. |
| 767 | + buf.close(); |
| 768 | + promise.setFailure(e); |
| 769 | + throw e; |
758 | 770 | } |
759 | 771 |
|
760 | 772 | if (buf.readableBytes() > 0) { |
|