From 16ce2192e45cbc4d2ba01267578ea5c3943caf0f Mon Sep 17 00:00:00 2001 From: rubidium Date: Tue, 19 Jun 2007 00:05:26 +0000 Subject: (svn r10212) -Fix [FS#723]: money overflow bugs in many locations. --- src/command.cpp | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/command.cpp') diff --git a/src/command.cpp b/src/command.cpp index 31c0d417e..8b814d034 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -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; } -- cgit v1.2.3-54-g00ecf