Changeset - r25557:82b9dc003739
[Not reviewed]
master
0 1 0
Patric Stout - 3 years ago 2021-05-29 13:13:11
truebrain@openttd.org
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.
1 file changed with 4 insertions and 3 deletions:
0 comments (0 inline, 0 general)
src/script/api/script_object.cpp
Show inline comments
 
@@ -309,10 +309,11 @@ ScriptObject::ActiveInstance::~ActiveIns
 
		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::~ActiveIns
 
	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()) {
0 comments (0 inline, 0 general)