From ee24dffae2f31f38c3b1a48314f4bf25a92820c7 Mon Sep 17 00:00:00 2001 From: Oleksii Matiiasevych Date: Wed, 26 Oct 2016 12:41:56 +0300 Subject: [PATCH] Prevent re-entrancy, fix balance check logic --- swarm/services/chequebook/contract/chequebook.sol | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/swarm/services/chequebook/contract/chequebook.sol b/swarm/services/chequebook/contract/chequebook.sol index cb19d0b27b..6443bd18a1 100644 --- a/swarm/services/chequebook/contract/chequebook.sol +++ b/swarm/services/chequebook/contract/chequebook.sol @@ -27,10 +27,12 @@ contract chequebook is mortal { if(owner != ecrecover(hash, sig_v, sig_r, sig_s)) return; // Attempt sending the difference between the cumulative amount on the cheque // and the cumulative amount on the last cashed cheque to beneficiary. - if (amount - sent[beneficiary] >= this.balance) { - if (beneficiary.send(amount - sent[beneficiary])) { - // Upon success, update the cumulative amount. - sent[beneficiary] = amount; + if (amount - sent[beneficiary] <= this.balance) { + // Update the cumulative amount upfront to prevent re-entrancy. + sent[beneficiary] = amount; + if (!beneficiary.send(amount - sent[beneficiary])) { + // Upon failure, rollback. + throw; } } else { // Upon failure, punish owner for writing a bounced cheque.