Skip to content

Commit 4c28d2a

Browse files
fix: prevent integer overflow in provide_at_least()
When available_clones.size() > n, the subtraction (n - available_clones.size()) caused unsigned arithmetic underflow, wrapping to INT_MAX (2^31-1). This made the code try to allocate 2 billion clones, causing catastrophic memory growth. The fix checks if we already have enough available clones before performing the arithmetic. This prevents the underflow and keeps memory usage reasonable. Before: decay_of_LD_highd.py would hit 12GB after 5 generations After: Completes all 250 generations with <1GB memory 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent 450eb08 commit 4c28d2a

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/haploid_highd.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ int haploid_highd::free_mem() {
212212
int haploid_highd::provide_at_least(int n) {
213213
//calculate the number of clones that need to be newly allocated. Allow for some slack
214214
//to avoid calling this too often
215+
// BUG FIX: Check if we already have enough clones to avoid unsigned arithmetic underflow
216+
if (available_clones.size() >= n) {
217+
return 0; // We already have enough clones available
218+
}
215219
int needed_gts = n - available_clones.size() + 100 + 0.1 * population.size();
216220

217221
//allocate at the necessary memory

0 commit comments

Comments
 (0)