Add nnz argument for size of allocated memory to mju_dense2sparse and check if this number is too small for number of non-zeros.
PiperOrigin-RevId: 690545825 Change-Id: I0e31cb907a1151cb2e4766d5f0cdfecb5629a2a6
This commit is contained in:
committed by
Copybara-Service
parent
1c424644dd
commit
a36f2cccb6
@@ -148,8 +148,13 @@ mjtNum mju_dotSparse2(const mjtNum* vec1, const mjtNum* vec2, int nnz1, const in
|
||||
|
||||
|
||||
// convert matrix from dense to sparse
|
||||
void mju_dense2sparse(mjtNum* res, const mjtNum* mat, int nr, int nc,
|
||||
int* rownnz, int* rowadr, int* colind) {
|
||||
// nnz is size of res and colind, return 1 if too small, 0 otherwise
|
||||
int mju_dense2sparse(mjtNum* res, const mjtNum* mat, int nr, int nc,
|
||||
int* rownnz, int* rowadr, int* colind, int nnz) {
|
||||
if (nnz <= 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int adr = 0;
|
||||
|
||||
// find non-zeros and construct sparse
|
||||
@@ -161,6 +166,11 @@ void mju_dense2sparse(mjtNum* res, const mjtNum* mat, int nr, int nc,
|
||||
// find non-zeros
|
||||
for (int c=0; c < nc; c++) {
|
||||
if (mat[r*nc+c]) {
|
||||
// check for out of bounds
|
||||
if (adr >= nnz) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// record index and count
|
||||
colind[adr] = c;
|
||||
rownnz[r]++;
|
||||
@@ -170,6 +180,7 @@ void mju_dense2sparse(mjtNum* res, const mjtNum* mat, int nr, int nc,
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user