Skip to content

Commit fe59123

Browse files
Merge pull request tronprotocol#1288 from tronprotocol/fix_long_overflow
handle the overflow
2 parents 719eb99 + 962a3ae commit fe59123

1 file changed

Lines changed: 20 additions & 16 deletions

File tree

‎src/main/java/org/tron/common/runtime/Runtime.java‎

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,12 +340,14 @@ private long getGasLimit(AccountCapsule account, long feeLimit) {
340340
cpuGasFromFeeLimit = feeLimit / Constant.SUN_PER_GAS;
341341
} else {
342342
long totalCpuGasFromFreeze = cpuProcessor.calculateGlobalCpuLimit(balanceForCpuFreeze);
343-
long leftBalanceForCpuFreeze =
344-
Math.multiplyExact(cpuGasFromFreeze, balanceForCpuFreeze) / totalCpuGasFromFreeze;
343+
long leftBalanceForCpuFreeze = BigInteger.valueOf(cpuGasFromFreeze)
344+
.multiply(BigInteger.valueOf(balanceForCpuFreeze))
345+
.divide(BigInteger.valueOf(totalCpuGasFromFreeze)).longValue();
345346

346347
if (leftBalanceForCpuFreeze >= feeLimit) {
347-
cpuGasFromFeeLimit =
348-
Math.multiplyExact(totalCpuGasFromFreeze, feeLimit) / balanceForCpuFreeze;
348+
cpuGasFromFeeLimit = BigInteger.valueOf(totalCpuGasFromFreeze)
349+
.multiply(BigInteger.valueOf(feeLimit))
350+
.divide(BigInteger.valueOf(balanceForCpuFreeze)).longValue();
349351
} else {
350352
cpuGasFromFeeLimit = Math
351353
.addExact(cpuGasFromFreeze,
@@ -371,12 +373,7 @@ private long getGasLimit(AccountCapsule creator, AccountCapsule caller,
371373
.getContract(contract.getContractAddress().toByteArray()).getInstance();
372374
long consumeUserResourcePercent = smartContract.getConsumeUserResourcePercent();
373375

374-
if (consumeUserResourcePercent >= 100) {
375-
consumeUserResourcePercent = 100;
376-
}
377-
if (consumeUserResourcePercent <= 0) {
378-
consumeUserResourcePercent = 0;
379-
}
376+
consumeUserResourcePercent = max(0, min(consumeUserResourcePercent, 100));
380377

381378
if (consumeUserResourcePercent <= 0) {
382379
return creatorGasLimit;
@@ -414,9 +411,9 @@ private void create()
414411

415412
// insure the new contract address haven't exist
416413
if (deposit.getAccount(contractAddress) != null) {
417-
logger.error("Trying to create a contract with existing contract address: " + Wallet
418-
.encode58Check(contractAddress));
419-
return;
414+
throw new ContractExeException(
415+
"Trying to create a contract with existing contract address: " + Wallet
416+
.encode58Check(contractAddress));
420417
}
421418

422419
newSmartContract = newSmartContract.toBuilder()
@@ -457,7 +454,7 @@ private void create()
457454
this.program = new Program(ops, programInvoke, internalTransaction, config);
458455
} catch (Exception e) {
459456
logger.error(e.getMessage());
460-
return;
457+
throw new ContractExeException(e.getMessage());
461458
}
462459

463460
program.getResult().setContractAddress(contractAddress);
@@ -481,8 +478,9 @@ private void create()
481478
/**
482479
* **
483480
*/
481+
484482
private void call()
485-
throws ContractValidateException {
483+
throws ContractExeException, ContractValidateException {
486484
Contract.TriggerSmartContract contract = ContractCapsule.getTriggerContractFromTransaction(trx);
487485
if (contract == null) {
488486
return;
@@ -510,7 +508,13 @@ private void call()
510508
long vmShouldEndInUs = vmStartInUs + thisTxCPULimitInUs;
511509

512510
long feeLimit = trx.getRawData().getFeeLimit();
513-
long gasLimit = getGasLimit(creator, caller, contract, feeLimit);
511+
long gasLimit;
512+
try {
513+
gasLimit = getGasLimit(creator, caller, contract, feeLimit);
514+
} catch (Exception e) {
515+
logger.error(e.getMessage());
516+
throw new ContractExeException(e.getMessage());
517+
}
514518

515519
if (isCallConstant(contractAddress)) {
516520
gasLimit = Constant.MAX_GAS_IN_TX;

0 commit comments

Comments
 (0)