Skip to content

Commit

Permalink
#8161 introduce releaseEmpty*Buffer vs release*Buffer helpers
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovic Orban <lorban@bitronix.be>
  • Loading branch information
lorban committed Jun 15, 2022
1 parent 7a0b80c commit ba17305
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java
Expand Up @@ -442,6 +442,12 @@ private void releaseDecryptedInputBuffer()
}

private void releaseInputBuffers()
{
clearInputBuffers();
releaseEmptyInputBuffers();
}

private void releaseEmptyInputBuffers()
{
releaseEncryptedInputBuffer();
releaseDecryptedInputBuffer();
Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand All @@ -823,7 +835,7 @@ public int fill(ByteBuffer buffer) throws IOException
}
finally
{
releaseInputBuffers();
releaseEmptyInputBuffers();

if (_flushState == FlushState.WAIT_FOR_FILL)
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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("<flush {} {}", result, SslConnection.this);
}
Expand Down Expand Up @@ -1351,7 +1360,7 @@ private void doShutdownOutput(boolean close)
try (AutoLock l = _lock.lock())
{
_flushState = FlushState.IDLE;
releaseEncryptedOutputBuffer();
releaseEmptyEncryptedOutputBuffer();
}
}, t -> disconnect()), write);
}
Expand All @@ -1375,7 +1384,6 @@ private void disconnect()
{
try (AutoLock l = _lock.lock())
{
BufferUtil.clear(_encryptedOutput);
releaseEncryptedOutputBuffer();
}
getEndPoint().close();
Expand Down Expand Up @@ -1425,7 +1433,6 @@ public void doClose()
{
try (AutoLock l = _lock.lock())
{
clearInputBuffers();
releaseInputBuffers();
}
// First send the TLS Close Alert, then the FIN.
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit ba17305

Please sign in to comment.