From 15c27fddee8509614e9537297a3acadcedd0c4a8 Mon Sep 17 00:00:00 2001 From: Ben Haller Date: Fri, 19 Jul 2024 13:03:09 -0400 Subject: [PATCH] fix #458, occasional crash in sampleIndividuals() --- VERSIONS | 1 + core/subpopulation.cpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/VERSIONS b/VERSIONS index 1439d11d..7f92e513 100644 --- a/VERSIONS +++ b/VERSIONS @@ -16,6 +16,7 @@ development head (in the master branch): update Robin Hood Hashing to 3.11.5 to get some minor fixes update zlib from 1.2.13 to 1.3.1 to get some minor fixes some fixes under the hood to make filesystem paths on Windows, using \ instead of /, be better understood by Eidos + fix an occasional crash in sampleIndividuals() due to an off-by-one error in buffer sizing version 4.2.2 (Eidos version 3.2.2): diff --git a/core/subpopulation.cpp b/core/subpopulation.cpp index 3fdd9e6f..9e6da921 100644 --- a/core/subpopulation.cpp +++ b/core/subpopulation.cpp @@ -7734,9 +7734,9 @@ EidosValue_SP Subpopulation::ExecuteMethod_sampleIndividuals(EidosGlobalStringID static int *index_buffer = nullptr; static int buffer_capacity = 0; - if (last_candidate_index > buffer_capacity) // just make it big enough for last_candidate_index, not worth worrying + if (last_candidate_index + 1 > buffer_capacity) // just make it big enough for last_candidate_index, not worth worrying { - buffer_capacity = last_candidate_index * 2; // double whenever we go over capacity, to avoid reallocations + buffer_capacity = (last_candidate_index + 1) * 2; // double whenever we go over capacity, to avoid reallocations if (index_buffer) free(index_buffer); index_buffer = (int *)malloc(buffer_capacity * sizeof(int)); // no need to realloc, we don't need the old data