Skip to content

Commit 6d06a54

Browse files
authored
feat(ledger/babbage): Integer underflow issue in babbage.UtxoValidateCollateralEqBalance (#1024)
* feat(ledger/babbage): Prevented uint underflow in UtxoValidateCollateralEqBalance when collateral UTxOs are missing Signed-off-by: Akhil Repala <[email protected]> * feat(ledger/babbage): Refactored collateral validation to use only structured errors and return at function end Signed-off-by: Akhil Repala <[email protected]> --------- Signed-off-by: Akhil Repala <[email protected]>
1 parent 93c1740 commit 6d06a54

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

ledger/babbage/rules.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,19 @@ func UtxoValidateCollateralEqBalance(
184184
}
185185
collBalance += utxo.Output.Amount()
186186
}
187-
// Subtract collateral return amount
187+
188+
// Skip validation if no valid collateral UTxOs were found
189+
// This avoids subtracting from zero and prevents uint underflow
190+
if collBalance == 0 {
191+
return nil
192+
}
193+
194+
// Subtract collateral return amount with underflow protection
188195
collReturn := tx.CollateralReturn()
189-
if collReturn != nil {
196+
if collReturn != nil && collBalance >= collReturn.Amount() {
190197
collBalance -= collReturn.Amount()
191198
}
199+
192200
if totalCollateral == collBalance {
193201
return nil
194202
}

ledger/babbage/rules_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1449,6 +1449,27 @@ func TestUtxoValidateCollateralEqBalance(t *testing.T) {
14491449
}
14501450
},
14511451
)
1452+
// no valid collateral UTxO, should skip and not underflow
1453+
t.Run("no valid collateral UTxO, should skip and not underflow", func(t *testing.T) {
1454+
// Ledger state with NO matching UTxO
1455+
missingUtxoLedgerState := test.MockLedgerState{
1456+
MockUtxos: []common.Utxo{}, // empty
1457+
}
1458+
testTx.Body.TxCollateralReturn = &babbage.BabbageTransactionOutput{
1459+
OutputAmount: mary.MaryTransactionOutputValue{
1460+
Amount: testCollateralReturnAmountBad,
1461+
},
1462+
}
1463+
err := babbage.UtxoValidateCollateralEqBalance(
1464+
testTx,
1465+
testSlot,
1466+
missingUtxoLedgerState,
1467+
testProtocolParams,
1468+
)
1469+
if err != nil {
1470+
t.Errorf("Should skip collateral return validation if collBalance == 0. Got error: %v", err)
1471+
}
1472+
})
14521473
}
14531474

14541475
func TestUtxoValidateTooManyCollateralInputs(t *testing.T) {

0 commit comments

Comments
 (0)