Skip to content

Commit

Permalink
Fixed issue with corner case of reading empty fbcsr matrix
Browse files Browse the repository at this point in the history
The issue was flagged by SonarCloud - we would be accessing invalid
memory in case the input was empty. This is now fixed.
Also added a test for the empty input case.
  • Loading branch information
Slaedr committed Apr 8, 2021
1 parent 94e2361 commit fabf4dd
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/matrix/fbcsr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,11 @@ void Fbcsr<ValueType, IndexType>::read(const mat_data &data)
blocks.size() * bs * bs, bs);

tmp->row_ptrs_.get_data()[0] = 0;
if (data.nonzeros.size() == 0) {
tmp->move_to(this);
return;
}

index_type cur_brow = 0;
index_type cur_bnz = 0;
index_type cur_bcol = blocks.begin()->first.block_column;
Expand Down
18 changes: 18 additions & 0 deletions core/test/matrix/fbcsr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,24 @@ TYPED_TEST(Fbcsr, CanBeReadFromMatrixData)
}


TYPED_TEST(Fbcsr, CanBeReadFromEmptyMatrixData)
{
using Mtx = typename TestFixture::Mtx;
using MtxData = typename TestFixture::MtxData;
auto m = Mtx::create(this->exec);
m->set_block_size(this->fbsample.bs);
MtxData mdata;
mdata.size = gko::dim<2>{0, 0};

m->read(mdata);

ASSERT_EQ(m->get_size(), (gko::dim<2>{0, 0}));
ASSERT_EQ(m->get_const_row_ptrs()[0], 0);
ASSERT_EQ(m->get_const_col_idxs(), nullptr);
ASSERT_EQ(m->get_const_values(), nullptr);
}


TYPED_TEST(Fbcsr, GeneratesCorrectMatrixData)
{
using value_type = typename TestFixture::value_type;
Expand Down

0 comments on commit fabf4dd

Please sign in to comment.