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.
This commit is contained in:
Patric Stout 2021-05-29 15:13:11 +02:00 committed by Patric Stout
parent b0f44d7eb1
commit 665e3c1f45
1 changed files with 4 additions and 3 deletions

View File

@ -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()) {