diff options
author | Patric Stout <truebrain@openttd.org> | 2021-05-29 15:13:11 +0200 |
---|---|---|
committer | Patric Stout <github@truebrain.nl> | 2021-05-29 16:23:59 +0200 |
commit | 665e3c1f45a6ae44d4a25534b1d67816f2d1962b (patch) | |
tree | ab2a6f4643d5b071f954a9896a26cb79008b85dc /src | |
parent | b0f44d7eb1cfac13a5ede8b7154664f5fb455775 (diff) | |
download | openttd-665e3c1f45a6ae44d4a25534b1d67816f2d1962b.tar.xz |
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.
Diffstat (limited to 'src')
-rw-r--r-- | src/script/api/script_object.cpp | 7 |
1 files changed, 4 insertions, 3 deletions
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()) { |