From ba17305a2fff12ca9160fb29eb3b8decbe86ce4b Mon Sep 17 00:00:00 2001 From: Ludovic Orban Date: Wed, 15 Jun 2022 09:26:57 +0200 Subject: [PATCH] #8161 introduce releaseEmpty*Buffer vs release*Buffer helpers Signed-off-by: Ludovic Orban --- .../eclipse/jetty/io/ssl/SslConnection.java | 36 +++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java b/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java index 0aeb72f7e320..99bcc2a0b777 100644 --- a/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java +++ b/jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java @@ -442,6 +442,12 @@ private void releaseDecryptedInputBuffer() } private void releaseInputBuffers() + { + clearInputBuffers(); + releaseEmptyInputBuffers(); + } + + private void releaseEmptyInputBuffers() { releaseEncryptedInputBuffer(); releaseDecryptedInputBuffer(); @@ -457,6 +463,14 @@ private void clearInputBuffers() } private void releaseEncryptedOutputBuffer() + { + if (!_lock.isHeldByCurrentThread()) + throw new IllegalStateException(); + BufferUtil.clear(_encryptedOutput); + releaseEmptyEncryptedOutputBuffer(); + } + + private void releaseEmptyEncryptedOutputBuffer() { if (!_lock.isHeldByCurrentThread()) throw new IllegalStateException(); @@ -809,9 +823,7 @@ public int fill(ByteBuffer buffer) throws IOException } catch (Throwable x) { - // Clear the input buffers so that releaseInputBuffers() - // in the finally block will not leak them. - clearInputBuffers(); + releaseInputBuffers(); Throwable f = handleException(x, "fill"); Throwable failure = handshakeFailed(f); if (_flushState == FlushState.WAIT_FOR_FILL) @@ -823,7 +835,7 @@ public int fill(ByteBuffer buffer) throws IOException } finally { - releaseInputBuffers(); + releaseEmptyInputBuffers(); if (_flushState == FlushState.WAIT_FOR_FILL) { @@ -1142,7 +1154,7 @@ public boolean flush(ByteBuffer... appOuts) throws IOException // See also system property "jsse.SSLEngine.acceptLargeFragments". if (packetBufferSize < getPacketBufferSize()) { - releaseEncryptedOutputBuffer(); + releaseEmptyEncryptedOutputBuffer(); continue; } throw new IllegalStateException("Unexpected wrap result " + wrap); @@ -1180,16 +1192,13 @@ public boolean flush(ByteBuffer... appOuts) throws IOException } catch (Throwable x) { - // Clear the encrypted output buffer so that - // releaseEncryptedOutputBuffer() in the finally block - // will not leak it. - BufferUtil.clear(_encryptedOutput); + releaseEncryptedOutputBuffer(); Throwable failure = handleException(x, "flush"); throw handshakeFailed(failure); } finally { - releaseEncryptedOutputBuffer(); + releaseEmptyEncryptedOutputBuffer(); if (LOG.isDebugEnabled()) LOG.debug(" disconnect()), write); } @@ -1375,7 +1384,6 @@ private void disconnect() { try (AutoLock l = _lock.lock()) { - BufferUtil.clear(_encryptedOutput); releaseEncryptedOutputBuffer(); } getEndPoint().close(); @@ -1425,7 +1433,6 @@ public void doClose() { try (AutoLock l = _lock.lock()) { - clearInputBuffers(); releaseInputBuffers(); } // First send the TLS Close Alert, then the FIN. @@ -1582,7 +1589,7 @@ public void succeeded() { if (LOG.isDebugEnabled()) LOG.debug("IncompleteWriteCB succeeded {}", SslConnection.this); - releaseEncryptedOutputBuffer(); + releaseEmptyEncryptedOutputBuffer(); _flushState = FlushState.IDLE; interested = _fillState == FillState.INTERESTED; @@ -1608,7 +1615,6 @@ public void failed(final Throwable x) if (LOG.isDebugEnabled()) LOG.debug("IncompleteWriteCB failed {}", SslConnection.this, x); - BufferUtil.clear(_encryptedOutput); releaseEncryptedOutputBuffer(); _flushState = FlushState.IDLE;