1
0
mirror of https://github.com/OpenTTD/OpenTTD.git synced 2025-08-31 18:39:10 +00:00

(svn r10212) -Fix [FS#723]: money overflow bugs in many locations.

This commit is contained in:
rubidium
2007-06-19 00:05:26 +00:00
parent 23af871615
commit 16ce2192e4
6 changed files with 31 additions and 9 deletions

View File

@@ -594,7 +594,7 @@ callb_err:
CommandCost CommandCost::AddCost(CommandCost ret)
{
this->cost += ret.cost;
this->AddCost(ret.cost);
if (this->success && !ret.success) {
this->message = ret.message;
this->success = false;
@@ -604,13 +604,25 @@ CommandCost CommandCost::AddCost(CommandCost ret)
CommandCost CommandCost::AddCost(Money cost)
{
this->cost += cost;
/* Overflow protection */
if (cost < 0 && (this->cost + cost) > this->cost) {
this->cost = INT64_MIN;
} else if (cost > 0 && (this->cost + cost) < this->cost) {
this->cost = INT64_MAX;
} else {
this->cost += cost;
}
return *this;
}
CommandCost CommandCost::MultiplyCost(int factor)
{
this->cost *= factor;
/* Overflow protection */
if (factor != 0 && (INT64_MAX / myabs(factor)) < myabs(this->cost)) {
this->cost = (this->cost < 0 == factor < 0) ? INT64_MAX : INT64_MIN;
} else {
this->cost *= factor;
}
return *this;
}