Skip to content

Commit 8471a35

Browse files
committed
fixed rare fatal racing condition in SSLSocketChannel2 ( TooTallNate#190 )
1 parent ea2b570 commit 8471a35

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/main/java/org/java_websocket/SSLSocketChannel2.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ private synchronized ByteBuffer unwrap() throws SSLException {
158158
rem = inData.remaining();
159159
readEngineResult = sslEngine.unwrap( inCrypt, inData );
160160
} while ( readEngineResult.getStatus() == SSLEngineResult.Status.OK && ( rem != inData.remaining() || sslEngine.getHandshakeStatus() == HandshakeStatus.NEED_UNWRAP ) );
161-
162161
inData.flip();
163162
return inData;
164163
}
@@ -226,10 +225,16 @@ public int read( ByteBuffer dst ) throws IOException {
226225
}
227226
}
228227
}
228+
/* 1. When "dst" is smaller than "inData" readRemaining will fill "dst" with data decoded in a previous read call.
229+
* 2. When "inCrypt" contains more data than "inData" has remaining space, unwrap has to be called on more time(readRemaining)
230+
*/
229231
int purged = readRemaining( dst );
230232
if( purged != 0 )
231233
return purged;
232234

235+
/* We only continue when we really need more data from the network.
236+
* Thats the case if inData is empty or inCrypt holds to less data than necessary for decryption
237+
*/
233238
assert ( inData.position() == 0 );
234239
inData.clear();
235240

@@ -238,7 +243,7 @@ public int read( ByteBuffer dst ) throws IOException {
238243
else
239244
inCrypt.compact();
240245

241-
if( ( isBlocking() && inCrypt.position() == 0 ) || readEngineResult.getStatus() == Status.BUFFER_UNDERFLOW )
246+
if( isBlocking() || readEngineResult.getStatus() == Status.BUFFER_UNDERFLOW )
242247
if( socketChannel.read( inCrypt ) == -1 ) {
243248
return -1;
244249
}

0 commit comments

Comments
 (0)