From eede526b7c4be526deca424ba9d3ac729098a3e7 Mon Sep 17 00:00:00 2001 From: Andreas Forster Date: Mon, 22 Jan 2024 00:35:31 +0000 Subject: [PATCH] [Compressed Storage] Use smaller type of Index & StorageIndex for determining maximum size during resize. --- Eigen/src/SparseCore/CompressedStorage.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Eigen/src/SparseCore/CompressedStorage.h b/Eigen/src/SparseCore/CompressedStorage.h index 123c89c0d..8f8a6963a 100644 --- a/Eigen/src/SparseCore/CompressedStorage.h +++ b/Eigen/src/SparseCore/CompressedStorage.h @@ -71,8 +71,13 @@ class CompressedStorage { void resize(Index size, double reserveSizeFactor = 0) { if (m_allocatedSize < size) { + // Avoid underflow on the std::min call by choosing the smaller index type. + using SmallerIndexType = + typename std::conditional((std::numeric_limits::max)()) < + static_cast((std::numeric_limits::max)()), + Index, StorageIndex>::type; Index realloc_size = - (std::min)(NumTraits::highest(), size + Index(reserveSizeFactor * double(size))); + (std::min)(NumTraits::highest(), size + Index(reserveSizeFactor * double(size))); if (realloc_size < size) internal::throw_std_bad_alloc(); reallocate(realloc_size); }