From d310a04276d52ccebc46648df8c56bf29a7780e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guilloux?= Date: Tue, 20 Feb 2024 18:55:18 +0100 Subject: [PATCH] Fix #12133: [Script] Don't crash when emergency saving (#12138) (cherry picked from commit 25d1238907390a67ffc877b7a712865d32d6248f) --- src/ai/ai_core.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/ai/ai_core.cpp b/src/ai/ai_core.cpp index c5813e4148..5c466c81ab 100644 --- a/src/ai/ai_core.cpp +++ b/src/ai/ai_core.cpp @@ -283,14 +283,18 @@ { if (!_networking || _network_server) { Company *c = Company::GetIfValid(company); - assert(c != nullptr && c->ai_instance != nullptr); + assert(c != nullptr); - Backup cur_company(_current_company, company, FILE_LINE); - c->ai_instance->Save(); - cur_company.Restore(); - } else { - AIInstance::SaveEmpty(); + /* When doing emergency saving, an AI can be not fully initialised. */ + if (c->ai_instance != nullptr) { + Backup cur_company(_current_company, company, FILE_LINE); + c->ai_instance->Save(); + cur_company.Restore(); + return; + } } + + AIInstance::SaveEmpty(); } /* static */ std::string AI::GetConsoleList(bool newest_only)