zscilib
zscilib copied to clipboard
OOB write in zsl_mtx_eigenvectors
I'm not sure I'm understanding this block of code, but there's definitely an unchecked out-of-bounds write here which is crashing my code:
int
zsl_mtx_eigenvectors(struct zsl_mtx *m, struct zsl_mtx *mev, size_t iter,
bool orthonormal)
{
<...>
/* Since 'b' is the number of eigenvectors, reduce 'mev' (of size
* m->sz_rows times b) to erase columns of zeros. */
mev->sz_cols = b;
for (size_t s = 0; s < b; s++) {
zsl_mtx_get_col(&mev2, s, f.data);
zsl_mtx_set_col(mev, s, f.data);
}
<...>
}
I'm unclear why the number of eigenvectors would be greater than the number of columns. However, when it is, this code manipulates memory well outside the allocated space.
zsl_mtx_eigenvectors
is used as I would expect by zsl_mtx_svd
- allocating only enough space for eigenvectors matching the number of columns - but if that's not true, then there's an OOB write even within the same file.
int
zsl_mtx_svd(struct zsl_mtx *m, struct zsl_mtx *u, struct zsl_mtx *e,
struct zsl_mtx *v, size_t iter)
{
ZSL_MATRIX_DEF(aat, m->sz_rows, m->sz_rows);
ZSL_MATRIX_DEF(upri, m->sz_rows, m->sz_rows);
<...>
zsl_mtx_eigenvectors(&aat, &upri, iter, true);
Here, aat
and upri
are allocated the same size, but if it's true that there can be more eigenvectors than rows, then upri
doesn't have enough memory allocated.
Most other math libraries I work with tend to return orthogonal eigenvectors, and therefore limit themselves to the number of columns.
What am I missing?