[eigen] Possible undefined behavior in insert() in SparseMatrix |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
Hi,
I justed checked the new insert() method for the SparseMatrix. There
seems to be a problem, if the actual size is 0. So assuming you created
a new sparse matrix and ran setZero() but no reserve(). And then you
start inserting. In that case line 251 in SparseMatrix results in a nan
(due to division by zero). And the following std::max and std::min
operations have undefined behavior when passing a nan. In my case I get
a nan as value for reallocRatio. And this may result in
CompressedStorage's resize to allocate really much memory.
For me I even get a std::bad_alloc, if my Scalar type is an unsigned
char (checked attached code). For some reason I don't get it for int.
This is strange to me. But another issue I don not understand yet.
Regards,
Jens
#include <Eigen/Sparse>
namespace Eigen {
template<> struct NumTraits<unsigned char>
{
typedef unsigned char Real;
typedef double FloatingPoint;
enum {
IsComplex = 0,
HasFloatingPoint = 0,
ReadCost = 1,
AddCost = 1,
MulCost = 1
};
};
}
int main() {
// for int it works
Eigen::SparseMatrix<unsigned char> foo(10,10);
foo.setZero();
//foo.reserve(1);
// next line results in std::bad_alloc on line 211 of CompressedStorage.h
// but not for int as Scalar?
foo.insert(2,3) = 4;
foo.finalize();
}