Change: Limit total script ops that can be consumed by a list valuate

(cherry picked from commit 1d0b40b2b2f2998d96221b18ae65a4d1524c61a7)
This commit is contained in:
Jonathan G Rennison
2024-01-01 22:51:54 +00:00
parent 42c8f50551
commit be4f8b91c6
5 changed files with 36 additions and 0 deletions

View File

@@ -11,7 +11,9 @@
#include "script_list.hpp"
#include "script_controller.hpp"
#include "../../debug.h"
#include "../../core/backup_type.hpp"
#include "../../script/squirrel.hpp"
#include <../squirrel/sqvm.h>
#include "../../safeguards.h"
@@ -1026,6 +1028,14 @@ SQInteger ScriptList::Valuate(HSQUIRRELVM vm)
bool backup_allow = ScriptObject::GetAllowDoCommand();
ScriptObject::SetAllowDoCommand(false);
/* Limit the total number of ops that can be consumed by a valuate operation */
SQInteger new_ops_error_threshold = vm->_ops_till_suspend_error_threshold;
if (vm->_ops_till_suspend_error_threshold == INT64_MIN) {
new_ops_error_threshold = vm->_ops_till_suspend - MAX_VALUATE_OPS;
vm->_ops_till_suspend_error_label = "valuator function";
}
AutoRestoreBackup ops_error_threshold_backup(vm->_ops_till_suspend_error_threshold, new_ops_error_threshold);
/* Push the function to call */
sq_push(vm, 2);

View File

@@ -15,6 +15,9 @@
#include "../../3rdparty/cpp-btree/safe_btree_set.h"
#include "../../3rdparty/cpp-btree/safe_btree_map.h"
/** Maximum number of operations allowed for valuating a list. */
static const int MAX_VALUATE_OPS = 500000;
class ScriptListSorter;
/**

View File

@@ -15,6 +15,8 @@
#include "../../depot_map.h"
#include "../../vehicle_base.h"
#include "../../train.h"
#include "../../core/backup_type.hpp"
#include <../squirrel/sqvm.h>
#include "../../safeguards.h"
@@ -41,6 +43,14 @@ ScriptVehicleList::ScriptVehicleList(HSQUIRRELVM vm)
bool backup_allow = ScriptObject::GetAllowDoCommand();
ScriptObject::SetAllowDoCommand(false);
/* Limit the total number of ops that can be consumed by a filter operation, if a filter function is present */
SQInteger new_ops_error_threshold = vm->_ops_till_suspend_error_threshold;
if (nparam >= 1 && vm->_ops_till_suspend_error_threshold == INT64_MIN) {
new_ops_error_threshold = vm->_ops_till_suspend - MAX_VALUATE_OPS;
vm->_ops_till_suspend_error_label = "vehicle filter function";
}
AutoRestoreBackup ops_error_threshold_backup(vm->_ops_till_suspend_error_threshold, new_ops_error_threshold);
for (const Vehicle *v : Vehicle::Iterate()) {
if (v->owner != ScriptObject::GetCompany() && !ScriptCompanyMode::IsDeity()) continue;
if (!v->IsPrimaryVehicle() && !(v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon())) continue;