Skip to content

Commit 327481a

Browse files
authored
Fix insufficient pool-growing logic in bli_pool.c. (#559)
Details: - The current mechanism for growing a pool_t doubles the length of the block_ptrs array every time the array length needs to be increased due to new blocks being added. However, that logic did not take in account the new total number of blocks, and the fact that the caller may be requesting more blocks that would fit even after doubling the current length of block_ptrs. The code comments now contain two illustrating examples that show why, even after doubling, we must always have at least enough room to fit all of the old blocks plus the newly requested blocks. - This commit also happens to fix a memory corruption issue that stems from growing any pool_t that is initialized with a block_ptrs length of 0. (Previously, the memory pool for packed buffers of C was initialized with a block_ptrs length of 0, but because it is unused this bug did not manifest by default.) - Co-authored-by: Minh Quan Ho <[email protected]>
1 parent 32a6d93 commit 327481a

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

frame/base/bli_pool.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,15 @@ void bli_pool_grow
373373
{
374374
// To prevent this from happening often, we double the current
375375
// length of the block_ptrs array.
376-
const siz_t block_ptrs_len_new = 2 * block_ptrs_len_cur;
376+
// Sanity: make sure that the block_ptrs_len_new will be at least
377+
// num_blocks_new, in case doubling the block_ptrs_len_cur is not enough.
378+
// Example 1:
379+
// - block_ptrs_len_cur == num_blocks_cur == 0 and num_blocks_add = 1
380+
// - So doubling: 2 * block_ptrs_len_cur = 0, whereas 1 is expected
381+
// Example 2:
382+
// - block_ptrs_len_cur == num_blocks_cur == 10 and num_blocks_add = 30
383+
// - So doubling: 2 * block_ptrs_len_cur = 20, whereas 40 is expected
384+
const siz_t block_ptrs_len_new = bli_max( (2 * block_ptrs_len_cur), num_blocks_new );
377385

378386
#ifdef BLIS_ENABLE_MEM_TRACING
379387
printf( "bli_pool_grow(): growing block_ptrs_len (%d -> %d): ",

0 commit comments

Comments
 (0)