Bound pre-allocation to the maximal size representable by StorageIndex and throw bad_alloc if that's not possible.

This commit is contained in:
Gael Guennebaud 2015-03-13 20:57:33 +01:00
parent 2f6f8bf31c
commit 5ffe29cb9f

View File

@ -86,7 +86,12 @@ class CompressedStorage
void resize(Index size, double reserveSizeFactor = 0) void resize(Index size, double reserveSizeFactor = 0)
{ {
if (m_allocatedSize<size) if (m_allocatedSize<size)
reallocate(size + Index(reserveSizeFactor*double(size))); {
Index realloc_size = (std::min<Index>)(NumTraits<StorageIndex>::highest(), size + Index(reserveSizeFactor*double(size)));
if(realloc_size<size)
internal::throw_std_bad_alloc();
reallocate(realloc_size);
}
m_size = size; m_size = size;
} }