summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2007-05-19 22:37:24 +0000
committerpeter1138 <peter1138@openttd.org>2007-05-19 22:37:24 +0000
commit1ce633b894d0c56c42b0ad696378f1236bcfddb1 (patch)
treee3165e93d13eb34f548c1c8be0b0b6ca539deaeb /src
parent707bc97c0317a99fd4c6259f073251ae5b6ac49b (diff)
downloadopenttd-1ce633b894d0c56c42b0ad696378f1236bcfddb1.tar.xz
(svn r9883) -Fix (r4965): [NewGRF] Check for overflow and carry over when adding to the existing value.
Diffstat (limited to 'src')
-rw-r--r--src/newgrf.cpp9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/newgrf.cpp b/src/newgrf.cpp
index ee8e35526..55f64f461 100644
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -3094,11 +3094,18 @@ static void CfgApply(byte *buf, int len)
grfmsg(8, "CfgApply: Applying %u bytes from parameter 0x%02X at offset 0x%04X", param_size, param_num, offset);
+ bool carry = false;
for (i = 0; i < param_size; i++) {
uint32 value = GetParamVal(param_num + i / 4, NULL);
+ /* Reset carry flag for each iteration of the variable (only really
+ * matters if param_size is greater than 4) */
+ if (i == 0) carry = false;
if (add_value) {
- _preload_sprite[offset + i] += GB(value, (i % 4) * 8, 8);
+ uint new_value = _preload_sprite[offset + i] + GB(value, (i % 4) * 8, 8) + (carry ? 1 : 0);
+ _preload_sprite[offset + i] = GB(new_value, 0, 8);
+ /* Check if the addition overflowed */
+ carry = new_value >= 256;
} else {
_preload_sprite[offset + i] = GB(value, (i % 4) * 8, 8);
}