Skip to content

Commit 3235996

Browse files
becastmarci4
authored andcommitted
Fix for NPE on WSS Portscan. Fixes TooTallNate#388 & TooTallNate#349 (TooTallNate#389)
* Fix for NPE on WSS Portscan. Fixes TooTallNate#388 & TooTallNate#349
1 parent 2f85abb commit 3235996

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/main/java/org/java_websocket/server/WebSocketServer.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ public void run() {
295295

296296
while ( i.hasNext() ) {
297297
key = i.next();
298-
298+
conn = null;
299+
299300
if( !key.isValid() ) {
300301
// Object o = key.attachment();
301302
continue;
@@ -308,18 +309,34 @@ public void run() {
308309
}
309310

310311
SocketChannel channel = server.accept();
312+
if(channel==null){
313+
continue;
314+
}
311315
channel.configureBlocking( false );
312316
WebSocketImpl w = wsf.createWebSocket( this, drafts, channel.socket() );
313317
w.key = channel.register( selector, SelectionKey.OP_READ, w );
314-
w.channel = wsf.wrapChannel( channel, w.key );
315-
i.remove();
316-
allocateBuffers( w );
318+
try {
319+
w.channel = wsf.wrapChannel( channel, w.key );
320+
i.remove();
321+
allocateBuffers( w );
322+
continue;
323+
} catch (IOException ex) {
324+
if (w.key != null)
325+
w.key.cancel();
326+
}
317327
continue;
318328
}
319329

320330
if( key.isReadable() ) {
321331
conn = (WebSocketImpl) key.attachment();
322332
ByteBuffer buf = takeBuffer();
333+
if(conn.channel == null){
334+
if( key != null )
335+
key.cancel();
336+
337+
handleIOException( key, conn, new IOException() );
338+
continue;
339+
}
323340
try {
324341
if( SocketChannelIOHelper.read( buf, conn, conn.channel ) ) {
325342
if( buf.hasRemaining() ) {

0 commit comments

Comments
 (0)