提交 39fdef7a authored 作者: Noel Grandin's avatar Noel Grandin

fix some NPE

found by setting breakpoint for NPE. And pass in the socket to Transfer in the constructor, since all of the usage sites need to immediately set it.
上级 40cd548a
...@@ -114,8 +114,7 @@ public class SessionRemote extends SessionWithState implements DataHandler { ...@@ -114,8 +114,7 @@ public class SessionRemote extends SessionWithState implements DataHandler {
throws IOException { throws IOException {
Socket socket = NetUtils.createSocket(server, Socket socket = NetUtils.createSocket(server,
Constants.DEFAULT_TCP_PORT, ci.isSSL()); Constants.DEFAULT_TCP_PORT, ci.isSSL());
Transfer trans = new Transfer(this); Transfer trans = new Transfer(this, socket);
trans.setSocket(socket);
trans.setSSL(ci.isSSL()); trans.setSSL(ci.isSSL());
trans.init(); trans.init();
trans.writeInt(Constants.TCP_PROTOCOL_VERSION_6); trans.writeInt(Constants.TCP_PROTOCOL_VERSION_6);
......
...@@ -62,8 +62,7 @@ public class TcpServerThread implements Runnable { ...@@ -62,8 +62,7 @@ public class TcpServerThread implements Runnable {
TcpServerThread(Socket socket, TcpServer server, int id) { TcpServerThread(Socket socket, TcpServer server, int id) {
this.server = server; this.server = server;
this.threadId = id; this.threadId = id;
transfer = new Transfer(null); transfer = new Transfer(null, socket);
transfer.setSocket(socket);
} }
private void trace(String s) { private void trace(String s) {
...@@ -78,6 +77,11 @@ public class TcpServerThread implements Runnable { ...@@ -78,6 +77,11 @@ public class TcpServerThread implements Runnable {
// TODO server: should support a list of allowed databases // TODO server: should support a list of allowed databases
// and a list of allowed clients // and a list of allowed clients
try { try {
Socket socket = transfer.getSocket();
if (socket == null) {
// the transfer is already closed, prevent NPE in TcpServer#allow(Socket)
return;
}
if (!server.allow(transfer.getSocket())) { if (!server.allow(transfer.getSocket())) {
throw DbException.get(ErrorCode.REMOTE_CONNECTION_NOT_ALLOWED); throw DbException.get(ErrorCode.REMOTE_CONNECTION_NOT_ALLOWED);
} }
......
...@@ -234,8 +234,7 @@ public class FileLock implements Runnable { ...@@ -234,8 +234,7 @@ public class FileLock implements Runnable {
try { try {
Socket socket = NetUtils.createSocket(server, Socket socket = NetUtils.createSocket(server,
Constants.DEFAULT_TCP_PORT, false); Constants.DEFAULT_TCP_PORT, false);
Transfer transfer = new Transfer(null); Transfer transfer = new Transfer(null, socket);
transfer.setSocket(socket);
transfer.init(); transfer.init();
transfer.writeInt(Constants.TCP_PROTOCOL_VERSION_6); transfer.writeInt(Constants.TCP_PROTOCOL_VERSION_6);
transfer.writeInt(Constants.TCP_PROTOCOL_VERSION_16); transfer.writeInt(Constants.TCP_PROTOCOL_VERSION_16);
...@@ -523,10 +522,15 @@ public class FileLock implements Runnable { ...@@ -523,10 +522,15 @@ public class FileLock implements Runnable {
trace.debug(e, "watchdog"); trace.debug(e, "watchdog");
} }
} }
while (serverSocket != null) { while (true) {
// take a copy so we don't get an NPE between checking it and using it
ServerSocket local = serverSocket;
if (local == null) {
break;
}
try { try {
trace.debug("watchdog accept"); trace.debug("watchdog accept");
Socket s = serverSocket.accept(); Socket s = local.accept();
s.close(); s.close();
} catch (Exception e) { } catch (Exception e) {
trace.debug(e, "watchdog"); trace.debug(e, "watchdog");
......
...@@ -58,17 +58,9 @@ public class Transfer { ...@@ -58,17 +58,9 @@ public class Transfer {
* *
* @param session the session * @param session the session
*/ */
public Transfer(SessionInterface session) { public Transfer(SessionInterface session, Socket s) {
this.session = session; this.session = session;
} this.socket = s;
/**
* Set the socket this object uses.
*
* @param s the socket
*/
public void setSocket(Socket s) {
socket = s;
} }
/** /**
...@@ -764,8 +756,7 @@ public class Transfer { ...@@ -764,8 +756,7 @@ public class Transfer {
InetAddress address = socket.getInetAddress(); InetAddress address = socket.getInetAddress();
int port = socket.getPort(); int port = socket.getPort();
Socket s2 = NetUtils.createSocket(address, port, ssl); Socket s2 = NetUtils.createSocket(address, port, ssl);
Transfer trans = new Transfer(null); Transfer trans = new Transfer(null, s2);
trans.setSocket(s2);
trans.setSSL(ssl); trans.setSSL(ssl);
return trans; return trans;
} }
......
...@@ -158,7 +158,9 @@ public class TestNetUtils extends TestBase { ...@@ -158,7 +158,9 @@ public class TestNetUtils extends TestBase {
*/ */
void closeSilently(Socket socket) { void closeSilently(Socket socket) {
try { try {
if (socket != null) {
socket.close(); socket.close();
}
} catch (Exception e) { } catch (Exception e) {
// ignore // ignore
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论