From 20c388a410770c15be2ac6046911be302fea5da2 Mon Sep 17 00:00:00 2001 From: elsid Date: Fri, 19 Sep 2025 20:00:31 +0200 Subject: [PATCH] Replace asserts by exceptions in RecordPtrT It's possible to fail when reading nif file. --- components/nif/recordptr.hpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/components/nif/recordptr.hpp b/components/nif/recordptr.hpp index a721d1ddb2..410059f415 100644 --- a/components/nif/recordptr.hpp +++ b/components/nif/recordptr.hpp @@ -1,6 +1,9 @@ #ifndef OPENMW_COMPONENTS_NIF_RECORDPTR_HPP #define OPENMW_COMPONENTS_NIF_RECORDPTR_HPP +#include +#include +#include #include #include "niffile.hpp" @@ -40,8 +43,11 @@ namespace Nif assert(mIndex == -2); // Store the index for later - mIndex = nif->get(); - assert(mIndex >= -1); + const int32_t index = nif->get(); + if (index < -1) + throw std::runtime_error(std::format("Invalid index: {}", index)); + + mIndex = index; } /// Resolve index to pointer @@ -51,10 +57,14 @@ namespace Nif mPtr = nullptr; else { - Record* r = nif.getRecord(mIndex); + Record* const r = nif.getRecord(mIndex); + if (r == nullptr) + throw std::runtime_error(std::format("Record at {} is nullptr", mIndex)); + // And cast it mPtr = dynamic_cast(r); - assert(mPtr != nullptr); + if (mPtr == nullptr) + throw std::runtime_error(std::format("Failed to cast record pointer to {}", typeid(X).name())); } }