From a61749cbf0a142b49987ce686506ab0ad942a709 Mon Sep 17 00:00:00 2001 From: llde Date: Sun, 18 Aug 2024 02:37:49 +0200 Subject: [PATCH] Protect GetNextRef to avoid crashing if no scan context is initialized (either no GerFirstRef or GenNextRef returned null once) Should fix #232 --- obse/obse/Commands_MiscReference.cpp | 13 ++++++++++--- obse_whatsnew.txt | 1 + 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/obse/obse/Commands_MiscReference.cpp b/obse/obse/Commands_MiscReference.cpp index febe11c..721f9f7 100644 --- a/obse/obse/Commands_MiscReference.cpp +++ b/obse/obse/Commands_MiscReference.cpp @@ -940,7 +940,10 @@ static const TESObjectCELL::ObjectListEntry* GetCellRefEntry(CellListVisitor vis return entry; } - +/* +TODO document and rework cell scans when it make sense. +Add additional protections for bad callers. +*/ static TESObjectREFR* CellScan(Script* scriptObj, TESObjectCELL* cellToScan = NULL, UInt32 formType = 0, UInt32 cellDepth = 0, bool getFirst = false, bool includeTaken = false, bool includeDeleted = false ,ProjectileFinder* projFinder = NULL) { static std::map scanScripts; @@ -948,12 +951,16 @@ static TESObjectREFR* CellScan(Script* scriptObj, TESObjectCELL* cellToScan = NU if (getFirst) scanScripts.erase(idx); - - if (scanScripts.find(idx) == scanScripts.end()) + bool isScriptUnRegistered = scanScripts.find(idx) == scanScripts.end(); + if (isScriptUnRegistered && cellToScan) { scanScripts[idx] = CellScanInfo(cellDepth, formType, includeTaken, includeDeleted , cellToScan); scanScripts[idx].FirstCell(); } + else if (isScriptUnRegistered && cellToScan == nullptr) { + Console_Print("[ERROR], attempted to call GetNextRef after a Null reference, without reinit the cell scan with GetFirstRef. In Script %08X", scriptObj->refID); + return nullptr; + } CellScanInfo* info = &(scanScripts[idx]); diff --git a/obse_whatsnew.txt b/obse_whatsnew.txt index 176a5a3..677a369 100644 --- a/obse_whatsnew.txt +++ b/obse_whatsnew.txt @@ -16,6 +16,7 @@ xOBSE 22.10 - Avoid rare null pointer access in ShowRuntimeError (thanks @Fox2Code) - Fix Menu{Tap/Hold/Release}Key commands not properly registering the button event for the UI. - Fix xOBSE 22.7 regression for Useful House, now unmodified IR won't reset the stack extraDataList, possible if it was modified by another IR or directly by the engine. + - Fix CTD when GetNextRef is called in a script after returning a null reference, without a GetFirstRef call to reinitialize the scan. Changes: - Force C++20 Standard, modified some places for compliance API: