Skip to content

Commit

Permalink
Fix six Potential null pointer access
Browse files Browse the repository at this point in the history
Fixes openhab#11167

Signed-off-by: Jacob Laursen <[email protected]>
  • Loading branch information
jlaur committed Aug 28, 2021
1 parent 4f7b669 commit 540021b
Showing 1 changed file with 14 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ public synchronized void connect() throws IOException {
if (connected) {
return;
}
socket = new Socket(inetAddr, port);
oStream = socket.getOutputStream();
iStream = socket.getInputStream();
Socket newSocket = new Socket(inetAddr, port);
oStream = newSocket.getOutputStream();
iStream = newSocket.getInputStream();
socket = newSocket;
connected = true;
}

Expand All @@ -64,8 +65,9 @@ public synchronized void disconnect() {
return;
}
try {
if (socket != null) {
socket.close();
Socket connectedSocket = socket;
if (connectedSocket != null) {
connectedSocket.close();
}
} catch (IOException ioe) {
logger.debug("Connection to air unit could not be closed gracefully. {}", ioe.getMessage());
Expand Down Expand Up @@ -98,21 +100,23 @@ public synchronized byte[] sendRobustRequest(byte[] operation, byte[] register,
}

private synchronized byte[] sendRequestInternal(byte[] request) throws IOException {
OutputStream localOutputStream = oStream;

if (oStream == null) {
if (localOutputStream == null) {
throw new IOException(
String.format("Output stream is null while sending request: %s", Arrays.toString(request)));
}
oStream.write(request);
oStream.flush();
localOutputStream.write(request);
localOutputStream.flush();

byte[] result = new byte[63];
if (iStream == null) {
InputStream localInputStream = iStream;
if (localInputStream == null) {
throw new IOException(
String.format("Input stream is null while sending request: %s", Arrays.toString(request)));
}
// noinspection ResultOfMethodCallIgnored
iStream.read(result, 0, 63);
localInputStream.read(result, 0, 63);

return result;
}
Expand Down

0 comments on commit 540021b

Please sign in to comment.