From bb9d1c82655af26f13cbf5dc094a8bd7f0f351a9 Mon Sep 17 00:00:00 2001 From: krzys-h Date: Sun, 2 Oct 2016 21:36:59 +0200 Subject: [PATCH] Remove "this == nullptr" checks in CBOT, fixes #828 --- src/CBot/CBotClass.cpp | 6 +++--- src/CBot/CBotProgram.cpp | 21 ++++++++++++++------- src/CBot/CBotStack.cpp | 14 +++++++------- src/CBot/CBotVar/CBotVarClass.cpp | 5 ++--- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/CBot/CBotClass.cpp b/src/CBot/CBotClass.cpp index 460ab9fa..8efc4af8 100644 --- a/src/CBot/CBotClass.cpp +++ b/src/CBot/CBotClass.cpp @@ -92,7 +92,7 @@ void CBotClass::ClearPublic() //////////////////////////////////////////////////////////////////////////////// void CBotClass::Purge() { - if ( this == nullptr ) return; + assert ( this != nullptr ); delete m_pVar; m_pVar = nullptr; @@ -104,7 +104,7 @@ void CBotClass::Purge() m_nbVar = m_parent == nullptr ? 0 : m_parent->m_nbVar; - m_next->Purge(); + if (m_next != nullptr) m_next->Purge(); m_next = nullptr; // no longer belongs to this chain } @@ -205,7 +205,7 @@ std::string CBotClass::GetName() //////////////////////////////////////////////////////////////////////////////// CBotClass* CBotClass::GetParent() { - if ( this == nullptr ) return nullptr; + assert ( this != nullptr ); return m_parent; } diff --git a/src/CBot/CBotProgram.cpp b/src/CBot/CBotProgram.cpp index 81008297..17c5ec0a 100644 --- a/src/CBot/CBotProgram.cpp +++ b/src/CBot/CBotProgram.cpp @@ -47,13 +47,13 @@ CBotProgram::CBotProgram(CBotVar* thisVar) CBotProgram::~CBotProgram() { // delete m_classes; - m_classes->Purge(); + if (m_classes != nullptr) m_classes->Purge(); m_classes = nullptr; CBotClass::FreeLock(this); delete m_functions; - m_stack->Delete(); + if (m_stack != nullptr) m_stack->Delete(); } bool CBotProgram::Compile(const std::string& program, std::vector& functions, void* pUser) @@ -62,7 +62,7 @@ bool CBotProgram::Compile(const std::string& program, std::vector& Stop(); // delete m_classes; - m_classes->Purge(); // purge the old definitions of classes + if (m_classes != nullptr) m_classes->Purge(); // purge the old definitions of classes // but without destroying the object m_classes = nullptr; delete m_functions; m_functions = nullptr; @@ -227,8 +227,11 @@ bool CBotProgram::Run(void* pUser, int timer) void CBotProgram::Stop() { - m_stack->Delete(); - m_stack = nullptr; + if (m_stack != nullptr) + { + m_stack->Delete(); + m_stack = nullptr; + } m_entryPoint = nullptr; CBotClass::FreeLock(this); } @@ -365,11 +368,15 @@ bool CBotProgram::RestoreState(FILE* pf) if (!ReadString( pf, s )) return false; Start(s); // point de reprise - m_stack->Delete(); - m_stack = nullptr; + if (m_stack != nullptr) + { + m_stack->Delete(); + m_stack = nullptr; + } // retrieves the stack from the memory // uses a nullptr pointer (m_stack) but it's ok like that + // TODO: no it's not okay like that! but it looks like it doesn't get optimized out at least ~krzys_h if (!m_stack->RestoreState(pf, m_stack)) return false; m_stack->SetProgram(this); // bases for routines diff --git a/src/CBot/CBotStack.cpp b/src/CBot/CBotStack.cpp index 295e6063..94697b4a 100644 --- a/src/CBot/CBotStack.cpp +++ b/src/CBot/CBotStack.cpp @@ -82,10 +82,10 @@ CBotStack* CBotStack::AllocateStack() //////////////////////////////////////////////////////////////////////////////// void CBotStack::Delete() { - if ( this == nullptr ) return; + assert ( this != nullptr ); - m_next->Delete(); - m_next2->Delete(); + if (m_next != nullptr) m_next->Delete(); + if (m_next2 != nullptr) m_next2->Delete(); if (m_prev != nullptr) { @@ -192,8 +192,8 @@ bool CBotStack::Return(CBotStack* pfils) m_var = pfils->m_var; // result transmitted pfils->m_var = nullptr; // not to destroy the variable - m_next->Delete();m_next = nullptr; // releases the stack above - m_next2->Delete();m_next2 = nullptr; // also the second stack (catch) + if (m_next != nullptr) m_next->Delete();m_next = nullptr; // releases the stack above + if (m_next2 != nullptr) m_next2->Delete();m_next2 = nullptr; // also the second stack (catch) return IsOk(); // interrupted if error } @@ -281,7 +281,7 @@ bool CBotStack::IfContinue(int state, const std::string& name) m_state = state; // where again? m_error = CBotNoErr; m_labelBreak.clear(); - m_next->Delete(); // purge above stack + if (m_next != nullptr) m_next->Delete(); // purge above stack return true; } @@ -478,7 +478,7 @@ bool CBotStack::Execute() if (!instr->Run(nullptr, pile)) return false; // resume interrupted execution - pile->m_next->Delete(); + if (pile->m_next != nullptr) pile->m_next->Delete(); pile->m_callFinished = true; return true; diff --git a/src/CBot/CBotVar/CBotVarClass.cpp b/src/CBot/CBotVar/CBotVarClass.cpp index 08acffbe..4da99b2d 100644 --- a/src/CBot/CBotVar/CBotVarClass.cpp +++ b/src/CBot/CBotVar/CBotVarClass.cpp @@ -69,11 +69,10 @@ CBotVarClass::CBotVarClass(const CBotToken& name, const CBotTypResult& type) m_instances.insert(this); CBotClass* pClass = type.GetClass(); - CBotClass* pClass2 = pClass->GetParent(); - if ( pClass2 != nullptr ) + if ( pClass != nullptr && pClass->GetParent() != nullptr ) { // also creates an instance of the parent class - m_pParent = new CBotVarClass(name, CBotTypResult(type.GetType(),pClass2) ); //, nIdent); + m_pParent = new CBotVarClass(name, CBotTypResult(type.GetType(), pClass->GetParent()) ); //, nIdent); } SetClass( pClass );