Skip to content

Commit

Permalink
fix #361, no good error on overflow of mutation buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaller committed Jul 25, 2023
1 parent bf7bcf9 commit 4a36a26
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ development head (in the master branch):
fix a bug with the Species methods mutationCounts() / mutationFrequences(), only for multispecies models; counts/frequencies would be zero for some species
this did not occur when running under SLiMgui (different code path), but always happened at the command line
add rank() function to Eidos (https://github.com/MesserLab/SLiM/issues/379); identical to R except that logical and string are not supported, and "random" is not supported; can be added later as needed
bug fix: #361, more than 2 billion mutations overflows without a good error message
bug fix in pyslim to announce: https://github.com/tskit-dev/pyslim/issues/307


Expand Down
12 changes: 12 additions & 0 deletions core/mutation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,25 @@ void SLiM_IncreaseMutationBlockCapacity(void)
std::uintptr_t old_mutation_block = reinterpret_cast<std::uintptr_t>(gSLiM_Mutation_Block);
MutationIndex old_block_capacity = gSLiM_Mutation_Block_Capacity;

//std::cout << "old capacity: " << old_block_capacity << std::endl;

// BCH 25 July 2023: check for increasing our block beyond the maximum size of 2^31 mutations.
// See https://github.com/MesserLab/SLiM/issues/361. Note that the initial size should be
// a power of 2, so that we actually reach the maximum; see SLIM_MUTATION_BLOCK_INITIAL_SIZE.
// In other words, we expect to be at exactly 0x0000000040000000UL here, and thus to double
// to 0x0000000080000000UL, which is a capacity of 2^31, which is the limit of int32_t.
if ((size_t)old_block_capacity > 0x0000000040000000UL) // >2^30 means >2^31 when doubled
EIDOS_TERMINATION << "ERROR (SLiM_IncreaseMutationBlockCapacity): too many mutations; there is a limit of 2^31 (2147483648) segregating mutations in SLiM." << EidosTerminate(nullptr);

gSLiM_Mutation_Block_Capacity *= 2;
gSLiM_Mutation_Block = (Mutation *)realloc((void*)gSLiM_Mutation_Block, gSLiM_Mutation_Block_Capacity * sizeof(Mutation));
gSLiM_Mutation_Refcounts = (slim_refcount_t *)realloc(gSLiM_Mutation_Refcounts, gSLiM_Mutation_Block_Capacity * sizeof(slim_refcount_t));

if (!gSLiM_Mutation_Block || !gSLiM_Mutation_Refcounts)
EIDOS_TERMINATION << "ERROR (SLiM_IncreaseMutationBlockCapacity): allocation failed; you may need to raise the memory limit for SLiM." << EidosTerminate(nullptr);

//std::cout << "new capacity: " << gSLiM_Mutation_Block_Capacity << std::endl;

std::uintptr_t new_mutation_block = reinterpret_cast<std::uintptr_t>(gSLiM_Mutation_Block);

// Set up the free list to extend into the new portion of the buffer. If we are called when
Expand Down

0 comments on commit 4a36a26

Please sign in to comment.