aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkekzdealer <kekzdealer@gmail.com>2020-05-12 21:29:46 +0200
committerkekzdealer <kekzdealer@gmail.com>2020-05-12 21:29:46 +0200
commitdc21eafe10f52ce9cf55b651de72ba48667187c3 (patch)
treeba249a4cbf4d8f738224e009064ab7e8caab4ef3
parent2af60ac5f234e4df016158d84cf110a885fe958f (diff)
downloadGT5-Unofficial-dc21eafe10f52ce9cf55b651de72ba48667187c3.tar.gz
GT5-Unofficial-dc21eafe10f52ce9cf55b651de72ba48667187c3.tar.bz2
GT5-Unofficial-dc21eafe10f52ce9cf55b651de72ba48667187c3.zip
added overflow protection when dealing with TT laser hatches, as their throughput might exceed MAX_LONG
-rw-r--r--src/main/java/common/tileentities/GTMTE_LapotronicSuperCapacitor.java12
1 files changed, 10 insertions, 2 deletions
diff --git a/src/main/java/common/tileentities/GTMTE_LapotronicSuperCapacitor.java b/src/main/java/common/tileentities/GTMTE_LapotronicSuperCapacitor.java
index b528f7ac10..a595dbf40c 100644
--- a/src/main/java/common/tileentities/GTMTE_LapotronicSuperCapacitor.java
+++ b/src/main/java/common/tileentities/GTMTE_LapotronicSuperCapacitor.java
@@ -426,7 +426,11 @@ public class GTMTE_LapotronicSuperCapacitor extends GT_MetaTileEntity_MultiBlock
}
final BigInteger remcapActual = capacity.subtract(stored);
final BigInteger recampLimited = (MAX_LONG.compareTo(remcapActual) > 0) ? remcapActual : MAX_LONG;
- final long power = Math.min(eHatch.maxEUInput() * eHatch.maxAmperesIn(), recampLimited.longValue());
+ final long watts = eHatch.maxEUInput() * eHatch.maxAmperesIn();
+ long power = Math.min(
+ // Overflow protection (taken from Math.addExact())
+ ((eHatch.maxEUInput() ^ watts) & (eHatch.maxAmperesIn() ^ watts)) < 0 ? Long.MAX_VALUE : watts,
+ recampLimited.longValue());
if(power <= eHatch.getEUVar()) {
eHatch.setEUVar(eHatch.getEUVar() - power);
stored = stored.add(BigInteger.valueOf(power));
@@ -438,7 +442,11 @@ public class GTMTE_LapotronicSuperCapacitor extends GT_MetaTileEntity_MultiBlock
continue;
}
final BigInteger remStoredLimited = (MAX_LONG.compareTo(stored) > 0) ? stored : MAX_LONG;
- final long power = Math.min(eDynamo.maxEUOutput() * eDynamo.maxAmperesOut(), remStoredLimited.longValue());
+ final long watts = eDynamo.maxEUOutput() * eDynamo.maxAmperesOut();
+ long power = Math.min(
+ // Overflow protection (taken from Math.addExact())
+ ((eDynamo.maxEUOutput() ^ watts) & (eDynamo.maxAmperesOut() ^ watts)) < 0 ? Long.MAX_VALUE : watts,
+ remStoredLimited.longValue());
if(eDynamo.getEUVar() <= eDynamo.maxEUStore() - power) {
eDynamo.setEUVar(eDynamo.getEUVar() + power);
stored = stored.subtract(BigInteger.valueOf(power));