Skip to content

Commit a6d5c90

Browse files
authored
Merge pull request #1700 from adamdecaf/fix-offset-cleanup-panic
fix: fix offset slice cleanup
2 parents edf90d2 + e9e1714 commit a6d5c90

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

batch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1215,7 +1215,7 @@ func (b *Batch) upsertOffsets() error {
12151215
}
12161216
// remove the EntryDetail
12171217
b.Control.EntryAddendaCount -= 1
1218-
b.Entries = append(b.Entries[:i], b.Entries[i+i:]...)
1218+
b.Entries = append(b.Entries[:i], b.Entries[i+1:]...)
12191219
i--
12201220
}
12211221
}

batch_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,3 +1774,49 @@ func TestBatch_DeleteADVEntries(t *testing.T) {
17741774
require.Len(t, b.ADVEntries, 1)
17751775
require.Equal(t, "1", b.ADVEntries[0].ID)
17761776
}
1777+
1778+
func TestBatch_upsertOffsets_PanicRegression(t *testing.T) {
1779+
batch := NewBatchWEB(&BatchHeader{
1780+
ServiceClassCode: MixedDebitsAndCredits,
1781+
StandardEntryClassCode: "WEB",
1782+
CompanyName: "Test",
1783+
CompanyIdentification: "123456789",
1784+
})
1785+
1786+
// Add 3 normal entries
1787+
for i := 0; i < 3; i++ {
1788+
batch.AddEntry(&EntryDetail{
1789+
TransactionCode: CheckingDebit,
1790+
RDFIIdentification: "12345678",
1791+
Amount: 100,
1792+
IndividualName: "User",
1793+
})
1794+
}
1795+
1796+
// Add offset as last entry (this triggered the i+i bug)
1797+
batch.AddEntry(&EntryDetail{
1798+
TransactionCode: CheckingCredit,
1799+
RDFIIdentification: "98765432",
1800+
Amount: 300,
1801+
IndividualName: offsetIndividualName,
1802+
})
1803+
1804+
// Setup control
1805+
batch.Control = &BatchControl{
1806+
TotalDebitEntryDollarAmount: 300,
1807+
TotalCreditEntryDollarAmount: 300,
1808+
EntryAddendaCount: 4,
1809+
}
1810+
1811+
batch.offset = &Offset{
1812+
RoutingNumber: "987654320",
1813+
AccountNumber: "123456",
1814+
AccountType: OffsetChecking,
1815+
}
1816+
1817+
require.NotPanics(t, func() {
1818+
batch.upsertOffsets()
1819+
})
1820+
1821+
require.Len(t, batch.Entries, 4)
1822+
}

0 commit comments

Comments
 (0)