From 88a695f251b20273c9d31a9377a81da5a5fbbeff Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Sat, 21 Dec 2019 14:35:08 +0400 Subject: [PATCH] Add safety checks for door state --- apps/openmw/mwclass/door.cpp | 4 +--- components/esm/doorstate.cpp | 10 ++++++++++ components/esm/doorstate.hpp | 2 +- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/apps/openmw/mwclass/door.cpp b/apps/openmw/mwclass/door.cpp index f2d79a0a3..cd2039869 100644 --- a/apps/openmw/mwclass/door.cpp +++ b/apps/openmw/mwclass/door.cpp @@ -34,7 +34,7 @@ namespace MWClass class DoorCustomData : public MWWorld::CustomData { public: - MWWorld::DoorState mDoorState; + MWWorld::DoorState mDoorState = MWWorld::DoorState::Idle; virtual MWWorld::CustomData *clone() const; @@ -344,8 +344,6 @@ namespace MWClass if (!ptr.getRefData().getCustomData()) { std::unique_ptr data(new DoorCustomData); - - data->mDoorState = MWWorld::DoorState::Idle; ptr.getRefData().setCustomData(data.release()); } } diff --git a/components/esm/doorstate.cpp b/components/esm/doorstate.cpp index 0487be7a4..4378bb219 100644 --- a/components/esm/doorstate.cpp +++ b/components/esm/doorstate.cpp @@ -3,6 +3,8 @@ #include "esmreader.hpp" #include "esmwriter.hpp" +#include + namespace ESM { @@ -12,12 +14,20 @@ namespace ESM mDoorState = 0; esm.getHNOT (mDoorState, "ANIM"); + if (mDoorState < 0 || mDoorState > 2) + Log(Debug::Warning) << "Dropping invalid door state (" << mDoorState << ") for door \"" << mRef.mRefID << "\""; } void DoorState::save(ESMWriter &esm, bool inInventory) const { ObjectState::save(esm, inInventory); + if (mDoorState < 0 || mDoorState > 2) + { + Log(Debug::Warning) << "Dropping invalid door state (" << mDoorState << ") for door \"" << mRef.mRefID << "\""; + return; + } + if (mDoorState != 0) esm.writeHNT ("ANIM", mDoorState); } diff --git a/components/esm/doorstate.hpp b/components/esm/doorstate.hpp index 0df30afb0..c2e765880 100644 --- a/components/esm/doorstate.hpp +++ b/components/esm/doorstate.hpp @@ -9,7 +9,7 @@ namespace ESM struct DoorState : public ObjectState { - int mDoorState; + int mDoorState = 0; virtual void load (ESMReader &esm); virtual void save (ESMWriter &esm, bool inInventory = false) const;