From 665e3c1f45a6ae44d4a25534b1d67816f2d1962b Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sat, 29 May 2021 15:13:11 +0200 Subject: Fix: ScriptObject::DoCommand could modify "text" while defined "const" This could have unwanted side-effects, as it could change the source for ever and ever. --- src/script/api/script_object.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/script/api/script_object.cpp b/src/script/api/script_object.cpp index 1f72eaa22..d61a76d9f 100644 --- a/src/script/api/script_object.cpp +++ b/src/script/api/script_object.cpp @@ -309,10 +309,11 @@ ScriptObject::ActiveInstance::~ActiveInstance() return false; } - if (!StrEmpty(text) && (GetCommandFlags(cmd) & CMD_STR_CTRL) == 0) { + std::string command_text = text == nullptr ? std::string{} : text; + if (!command_text.empty() && (GetCommandFlags(cmd) & CMD_STR_CTRL) == 0) { /* The string must be valid, i.e. not contain special codes. Since some * can be made with GSText, make sure the control codes are removed. */ - ::StrMakeValidInPlace(text, SVS_NONE); + command_text = ::StrMakeValid(command_text, SVS_NONE); } /* Set the default callback to return a true/false result of the DoCommand */ @@ -328,7 +329,7 @@ ScriptObject::ActiveInstance::~ActiveInstance() if (!estimate_only && _networking && !_generating_world) SetLastCommand(tile, p1, p2, cmd); /* Try to perform the command. */ - CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, (_networking && !_generating_world) ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : nullptr, text, false, estimate_only); + CommandCost res = ::DoCommandPInternal(tile, p1, p2, cmd, (_networking && !_generating_world) ? ScriptObject::GetActiveInstance()->GetDoCommandCallback() : nullptr, command_text.c_str(), false, estimate_only); /* We failed; set the error and bail out */ if (res.Failed()) { -- cgit v1.2.3-70-g09d2