From 09bc3aed4f8267126d144e379c4fc14e4954113d Mon Sep 17 00:00:00 2001 From: Duncan Ogilvie Date: Mon, 10 Oct 2022 14:34:17 +0200 Subject: [PATCH] Remove footguns from a few APIs --- src/dbg/_dbgfunctions.cpp | 7 +++++++ src/dbg/_exports.cpp | 15 +++++++-------- src/dbg/stackinfo.cpp | 8 ++++++++ src/dbg/thread.cpp | 6 ++++-- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/dbg/_dbgfunctions.cpp b/src/dbg/_dbgfunctions.cpp index 71603122..8f2fd420 100644 --- a/src/dbg/_dbgfunctions.cpp +++ b/src/dbg/_dbgfunctions.cpp @@ -127,6 +127,10 @@ static void _getsehchain(DBGSEHCHAIN* sehchain) MemRead(SEHList[i] + 4, &sehchain->records[i].handler, sizeof(duint)); } } + else + { + sehchain->records = nullptr; + } } static bool _getjitauto(bool* jit_auto) @@ -183,7 +187,10 @@ static bool _getprocesslist(DBGPROCESSINFO** entries, int* count) return false; *count = (int)infoList.size(); if(!*count) + { + *entries = nullptr; return false; + } *entries = (DBGPROCESSINFO*)BridgeAlloc(*count * sizeof(DBGPROCESSINFO)); for(int i = 0; i < *count; i++) { diff --git a/src/dbg/_exports.cpp b/src/dbg/_exports.cpp index e3cbf6e5..9c34f25d 100644 --- a/src/dbg/_exports.cpp +++ b/src/dbg/_exports.cpp @@ -61,8 +61,8 @@ extern "C" DLL_EXPORT bool _dbg_memmap(MEMMAP* memmap) SHARED_ACQUIRE(LockMemoryPages); int pagecount = (int)memoryPages.size(); - memset(memmap, 0, sizeof(MEMMAP)); memmap->count = pagecount; + memmap->page = nullptr; if(!pagecount) return true; @@ -802,13 +802,14 @@ extern "C" DLL_EXPORT int _dbg_getbplist(BPXTYPE type, BPMAP* bpmap) { if(!bpmap) return 0; + + bpmap->count = 0; + bpmap->bp = nullptr; + std::vector list; int bpcount = BpGetList(&list); if(bpcount == 0) - { - bpmap->count = 0; return 0; - } int retcount = 0; std::vector bridgeList; @@ -847,10 +848,7 @@ extern "C" DLL_EXPORT int _dbg_getbplist(BPXTYPE type, BPMAP* bpmap) retcount++; } if(!retcount) - { - bpmap->count = retcount; - return retcount; - } + return 0; bpmap->count = retcount; bpmap->bp = (BRIDGEBP*)BridgeAlloc(sizeof(BRIDGEBP) * retcount); for(int i = 0; i < retcount; i++) @@ -1395,6 +1393,7 @@ extern "C" DLL_EXPORT duint _dbg_sendmessage(DBGMSG type, void* param1, void* pa if(info->refcount == 0) { + info->references = nullptr; return false; } else diff --git a/src/dbg/stackinfo.cpp b/src/dbg/stackinfo.cpp index 7434dc4c..90872962 100644 --- a/src/dbg/stackinfo.cpp +++ b/src/dbg/stackinfo.cpp @@ -452,6 +452,10 @@ void stackgetcallstackbythread(HANDLE thread, CALLSTACK* callstack) // Copy data directly from the vector memcpy(callstack->entries, callstackVector.data(), callstack->total * sizeof(CALLSTACKENTRY)); } + else + { + callstack->entries = nullptr; + } } void stackgetcallstack(duint csp, CALLSTACK* callstack) @@ -469,6 +473,10 @@ void stackgetcallstack(duint csp, CALLSTACK* callstack) // Copy data directly from the vector memcpy(callstack->entries, callstackVector.data(), callstack->total * sizeof(CALLSTACKENTRY)); } + else + { + callstack->entries = nullptr; + } } void stackupdatesettings() diff --git a/src/dbg/thread.cpp b/src/dbg/thread.cpp index 9109e3ea..00b753e2 100644 --- a/src/dbg/thread.cpp +++ b/src/dbg/thread.cpp @@ -101,10 +101,12 @@ void ThreadGetList(THREADLIST* List) // Also assume BridgeAlloc zeros the returned buffer. // List->count = (int)threadList.size(); - List->list = nullptr; - if(List->count <= 0) + if(List->count == 0) + { + List->list = nullptr; return; + } // Allocate C-style array List->list = (THREADALLINFO*)BridgeAlloc(List->count * sizeof(THREADALLINFO));