summaryrefslogtreecommitdiff
path: root/src/command.cpp
diff options
context:
space:
mode:
authorrubidium <rubidium@openttd.org>2007-06-19 00:05:26 +0000
committerrubidium <rubidium@openttd.org>2007-06-19 00:05:26 +0000
commit16ce2192e45cbc4d2ba01267578ea5c3943caf0f (patch)
treeb07b07a2f5f04c71f8608530c990ebe9bdf603e1 /src/command.cpp
parent23af871615557914fd36744afe46a93a360603ce (diff)
downloadopenttd-16ce2192e45cbc4d2ba01267578ea5c3943caf0f.tar.xz
(svn r10212) -Fix [FS#723]: money overflow bugs in many locations.
Diffstat (limited to 'src/command.cpp')
-rw-r--r--src/command.cpp18
1 files changed, 15 insertions, 3 deletions
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;
}