提交 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 {
throws IOException {
Socket socket = NetUtils.createSocket(server,
Constants.DEFAULT_TCP_PORT, ci.isSSL());
Transfer trans = new Transfer(this);
trans.setSocket(socket);
Transfer trans = new Transfer(this, socket);
trans.setSSL(ci.isSSL());
trans.init();
trans.writeInt(Constants.TCP_PROTOCOL_VERSION_6);
......
......@@ -62,8 +62,7 @@ public class TcpServerThread implements Runnable {
TcpServerThread(Socket socket, TcpServer server, int id) {
this.server = server;
this.threadId = id;
transfer = new Transfer(null);
transfer.setSocket(socket);
transfer = new Transfer(null, socket);
}
private void trace(String s) {
......@@ -78,6 +77,11 @@ public class TcpServerThread implements Runnable {
// TODO server: should support a list of allowed databases
// and a list of allowed clients
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())) {
throw DbException.get(ErrorCode.REMOTE_CONNECTION_NOT_ALLOWED);
}
......
......@@ -234,8 +234,7 @@ public class FileLock implements Runnable {
try {
Socket socket = NetUtils.createSocket(server,
Constants.DEFAULT_TCP_PORT, false);
Transfer transfer = new Transfer(null);
transfer.setSocket(socket);
Transfer transfer = new Transfer(null, socket);
transfer.init();
transfer.writeInt(Constants.TCP_PROTOCOL_VERSION_6);
transfer.writeInt(Constants.TCP_PROTOCOL_VERSION_16);
......@@ -523,10 +522,15 @@ public class FileLock implements Runnable {
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 {
trace.debug("watchdog accept");
Socket s = serverSocket.accept();
Socket s = local.accept();
s.close();
} catch (Exception e) {
trace.debug(e, "watchdog");
......
......@@ -58,17 +58,9 @@ public class Transfer {
*
* @param session the session
*/
public Transfer(SessionInterface session) {
public Transfer(SessionInterface session, Socket s) {
this.session = session;
}
/**
* Set the socket this object uses.
*
* @param s the socket
*/
public void setSocket(Socket s) {
socket = s;
this.socket = s;
}
/**
......@@ -764,8 +756,7 @@ public class Transfer {
InetAddress address = socket.getInetAddress();
int port = socket.getPort();
Socket s2 = NetUtils.createSocket(address, port, ssl);
Transfer trans = new Transfer(null);
trans.setSocket(s2);
Transfer trans = new Transfer(null, s2);
trans.setSSL(ssl);
return trans;
}
......
......@@ -158,7 +158,9 @@ public class TestNetUtils extends TestBase {
*/
void closeSilently(Socket socket) {
try {
if (socket != null) {
socket.close();
}
} catch (Exception e) {
// ignore
}
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论