From 19abe80449c5cacc2ff7bb3c925f8e14289bfa1e Mon Sep 17 00:00:00 2001 From: Chris Marsh Date: Thu, 7 Sep 2017 09:23:35 -0700 Subject: [PATCH] Add a few more null checks. --- src/connection_win.cpp | 9 +++++++++ src/discord-rpc.cpp | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/src/connection_win.cpp b/src/connection_win.cpp index b750f05..3159c7e 100644 --- a/src/connection_win.cpp +++ b/src/connection_win.cpp @@ -71,13 +71,22 @@ bool BaseConnection::Close() bool BaseConnection::Write(const void* data, size_t length) { + if (length == 0) { + return true; + } auto self = reinterpret_cast(this); + if (self->pipe == INVALID_HANDLE_VALUE) { + return false; + } return ::WriteFile(self->pipe, data, (DWORD)length, nullptr, nullptr) == TRUE; } bool BaseConnection::Read(void* data, size_t length) { auto self = reinterpret_cast(this); + if (self->pipe == INVALID_HANDLE_VALUE) { + return false; + } DWORD bytesAvailable = 0; if (::PeekNamedPipe(self->pipe, nullptr, 0, nullptr, &bytesAvailable, nullptr)) { if (bytesAvailable >= length) { diff --git a/src/discord-rpc.cpp b/src/discord-rpc.cpp index 97a78de..86b66c5 100644 --- a/src/discord-rpc.cpp +++ b/src/discord-rpc.cpp @@ -95,6 +95,10 @@ static void SendQueueCommitMessage() extern "C" void Discord_UpdateConnection() { + if (!Connection) { + return; + } + if (!Connection->IsOpen()) { if (std::chrono::system_clock::now() >= NextConnect) { UpdateReconnectTime(); @@ -292,6 +296,10 @@ extern "C" void Discord_RunCallbacks() // of times inbetween calls here. Externally, we want the sequence to seem sane, so any other // signals are book-ended by calls to ready and disconnect. + if (!Connection) { + return; + } + bool wasDisconnected = WasJustDisconnected.exchange(false); bool isConnected = Connection->IsOpen();