Fix FileCache::ChangeNumBlocks using memory after realloc shrink.
This commit is contained in:
parent
1830ab64ee
commit
8b3e25ce7f
|
@ -448,6 +448,14 @@ bool FileCache::ChangeNumBlocks(size_t new_numblocks, bool exact)
|
||||||
if ( new_numblocks == blocks_used && !exact )
|
if ( new_numblocks == blocks_used && !exact )
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
|
// Release blocks if the file has decreased in size.
|
||||||
|
if ( new_numblocks < blocks_used )
|
||||||
|
{
|
||||||
|
for ( size_t i = new_numblocks; i < blocks_used; i++ )
|
||||||
|
kernel_block_cache->ReleaseBlock(blocks[i]);
|
||||||
|
blocks_used = new_numblocks;
|
||||||
|
}
|
||||||
|
|
||||||
// If needed, adapt the length of the array containing block pointers.
|
// If needed, adapt the length of the array containing block pointers.
|
||||||
if ( new_numblocks < blocks_length )
|
if ( new_numblocks < blocks_length )
|
||||||
exact = true;
|
exact = true;
|
||||||
|
@ -458,26 +466,18 @@ bool FileCache::ChangeNumBlocks(size_t new_numblocks, bool exact)
|
||||||
BlockCacheBlock** new_blocks = (BlockCacheBlock**) realloc(blocks, size);
|
BlockCacheBlock** new_blocks = (BlockCacheBlock**) realloc(blocks, size);
|
||||||
if ( !new_blocks )
|
if ( !new_blocks )
|
||||||
{
|
{
|
||||||
if ( new_blocks_length < blocks_length )
|
if ( blocks_length < new_blocks_length )
|
||||||
new_blocks = blocks;
|
|
||||||
else
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
blocks = new_blocks,
|
|
||||||
blocks_length = new_blocks_length;
|
|
||||||
|
|
||||||
assert(!blocks_length || blocks);
|
|
||||||
|
|
||||||
// Release blocks if the file has decreased in size.
|
|
||||||
if ( new_numblocks < blocks_used )
|
|
||||||
{
|
{
|
||||||
for ( size_t i = new_numblocks; i < blocks_used; i++ )
|
blocks = new_blocks;
|
||||||
kernel_block_cache->ReleaseBlock(blocks[i]);
|
blocks_length = new_blocks_length;
|
||||||
blocks_used = new_numblocks;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(new_numblocks <= blocks_length);
|
||||||
|
assert(!blocks_length || blocks);
|
||||||
|
|
||||||
// Acquire more blocks if the file has increased in size.
|
// Acquire more blocks if the file has increased in size.
|
||||||
for ( size_t i = blocks_used; i < new_numblocks; i++ )
|
for ( size_t i = blocks_used; i < new_numblocks; i++ )
|
||||||
{
|
{
|
||||||
|
@ -487,9 +487,9 @@ bool FileCache::ChangeNumBlocks(size_t new_numblocks, bool exact)
|
||||||
kernel_block_cache->ReleaseBlock(blocks[n]);
|
kernel_block_cache->ReleaseBlock(blocks[n]);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
blocks_used++;
|
||||||
}
|
}
|
||||||
|
|
||||||
blocks_used = new_numblocks;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue