From 7271e5558c8707d3ffdf1003b2d2c81811f7c42e Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Mon, 12 Jul 2021 03:25:59 +0300 Subject: [PATCH] Get rid of unchecked calls to malloc --- Source/dthread.cpp | 2 ++ Source/msg.cpp | 2 ++ Source/tmsg.cpp | 2 ++ Source/utils/thread.cpp | 4 ++-- 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Source/dthread.cpp b/Source/dthread.cpp index 22783506a..587472445 100644 --- a/Source/dthread.cpp +++ b/Source/dthread.cpp @@ -81,6 +81,8 @@ void dthread_send_delta(int pnum, _cmd_id cmd, byte *pbSrc, int dwLen) } pkt = static_cast(std::malloc(dwLen + 20)); + if (pkt == nullptr) + app_fatal("Failed to allocate memory"); pkt->pNext = nullptr; pkt->dwSpaceLeft = pnum; pkt->data[0] = static_cast(cmd); diff --git a/Source/msg.cpp b/Source/msg.cpp index c99d685f7..42ba813de 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -60,6 +60,8 @@ void GetNextPacket() TMegaPkt *result; sgpCurrPkt = static_cast(std::malloc(sizeof(TMegaPkt))); + if (sgpCurrPkt == nullptr) + app_fatal("Failed to allocate memory"); sgpCurrPkt->pNext = nullptr; sgpCurrPkt->dwSpaceLeft = sizeof(result->data); diff --git a/Source/tmsg.cpp b/Source/tmsg.cpp index c7046f0dd..d3f938696 100644 --- a/Source/tmsg.cpp +++ b/Source/tmsg.cpp @@ -38,6 +38,8 @@ void tmsg_add(byte *pbMsg, uint8_t bLen) TMsg **tail; TMsg *msg = static_cast(std::malloc(bLen + sizeof(*msg))); + if (msg == nullptr) + app_fatal("Failed to allocate memory"); msg->hdr.pNext = nullptr; msg->hdr.dwTime = SDL_GetTicks() + gnTickDelay * 10; msg->hdr.bLen = bLen; diff --git a/Source/utils/thread.cpp b/Source/utils/thread.cpp index f7666701a..311e08317 100644 --- a/Source/utils/thread.cpp +++ b/Source/utils/thread.cpp @@ -35,7 +35,7 @@ SDL_Thread *CreateThread(void (*handler)(), SDL_threadID *threadId) event_emul *StartEvent() { event_emul *ret; - ret = (event_emul *)malloc(sizeof(event_emul)); + ret = new event_emul(); ret->mutex = SDL_CreateMutex(); if (ret->mutex == nullptr) { ErrSdl(); @@ -51,7 +51,7 @@ void EndEvent(event_emul *event) { SDL_DestroyCond(event->cond); SDL_DestroyMutex(event->mutex); - free(event); + delete event; } void SetEvent(event_emul *e)