From d1509d6096e6a0959b40f420b4023a09c9b3d25f Mon Sep 17 00:00:00 2001 From: Scott Giese Date: Sat, 13 Feb 2021 22:25:31 -0600 Subject: [PATCH] NULL check after malloc --- 3rdparty/udns/rblcheck.c | 10 ++++++---- simgear/debug/BufferedLogCallback.cxx | 6 ++++-- simgear/io/raw_socket.cxx | 6 ++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/3rdparty/udns/rblcheck.c b/3rdparty/udns/rblcheck.c index 82d29dee..a58bf8c7 100644 --- a/3rdparty/udns/rblcheck.c +++ b/3rdparty/udns/rblcheck.c @@ -339,10 +339,12 @@ int main(int argc, char **argv) { char *home = getenv("HOME"); if (!home) home = "."; path = malloc(strlen(home) + 1 + sizeof(".rblcheckrc")); - sprintf(path, "%s/.rblcheckrc", home); - if (!addzonefile(path)) - addzonefile("/etc/rblcheckrc"); - free(path); + if (path) { + sprintf(path, "%s/.rblcheckrc", home); + if (!addzonefile(path)) + addzonefile("/etc/rblcheckrc"); + free(path); + } } } if (!nzones) diff --git a/simgear/debug/BufferedLogCallback.cxx b/simgear/debug/BufferedLogCallback.cxx index 21c8e972..5d094fa4 100644 --- a/simgear/debug/BufferedLogCallback.cxx +++ b/simgear/debug/BufferedLogCallback.cxx @@ -67,8 +67,10 @@ void BufferedLogCallback::operator()(sgDebugClass c, sgDebugPriority p, vector_cstring::value_type msg; if (aMessage.size() >= d->m_maxLength) { msg = (vector_cstring::value_type) malloc(d->m_maxLength); - strncpy((char*) msg, aMessage.c_str(), d->m_maxLength - 1); - msg[d->m_maxLength - 1] = 0; // add final NULL byte + if (msg) { + strncpy((char*) msg, aMessage.c_str(), d->m_maxLength - 1); + msg[d->m_maxLength - 1] = 0; // add final NULL byte + } } else { msg = (vector_cstring::value_type) strdup(aMessage.c_str()); } diff --git a/simgear/io/raw_socket.cxx b/simgear/io/raw_socket.cxx index 94b8a441..5b9308f5 100644 --- a/simgear/io/raw_socket.cxx +++ b/simgear/io/raw_socket.cxx @@ -239,7 +239,8 @@ IPAddress::IPAddress( const IPAddress& other ) : { if (other.addr) { addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in)); - memcpy(addr, other.addr, sizeof(struct sockaddr_in)); + if (addr) + memcpy(addr, other.addr, sizeof(struct sockaddr_in)); } } @@ -252,7 +253,8 @@ const IPAddress& IPAddress::operator=(const IPAddress& other) if (other.addr) { addr = (struct sockaddr_in*) malloc(sizeof(struct sockaddr_in)); - memcpy(addr, other.addr, sizeof(struct sockaddr_in)); + if (addr) + memcpy(addr, other.addr, sizeof(struct sockaddr_in)); } return *this;