Skip to content

Commit 1e1cc5f

Browse files
committed
Fixed issue with corner case of reading empty fbcsr matrix
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.
1 parent edc18f8 commit 1e1cc5f

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

core/matrix/fbcsr.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,11 @@ void Fbcsr<ValueType, IndexType>::read(const mat_data &data)
336336
blocks.size() * bs * bs, bs);
337337

338338
tmp->row_ptrs_.get_data()[0] = 0;
339+
if (data.nonzeros.size() == 0) {
340+
tmp->move_to(this);
341+
return;
342+
}
343+
339344
index_type cur_brow = 0;
340345
index_type cur_bnz = 0;
341346
index_type cur_bcol = blocks.begin()->first.block_column;

core/test/matrix/fbcsr.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,24 @@ TYPED_TEST(Fbcsr, CanBeReadFromMatrixData)
460460
}
461461

462462

463+
TYPED_TEST(Fbcsr, CanBeReadFromEmptyMatrixData)
464+
{
465+
using Mtx = typename TestFixture::Mtx;
466+
using MtxData = typename TestFixture::MtxData;
467+
auto m = Mtx::create(this->exec);
468+
m->set_block_size(this->fbsample.bs);
469+
MtxData mdata;
470+
mdata.size = gko::dim<2>{0, 0};
471+
472+
m->read(mdata);
473+
474+
ASSERT_EQ(m->get_size(), (gko::dim<2>{0, 0}));
475+
ASSERT_EQ(m->get_const_row_ptrs()[0], 0);
476+
ASSERT_EQ(m->get_const_col_idxs(), nullptr);
477+
ASSERT_EQ(m->get_const_values(), nullptr);
478+
}
479+
480+
463481
TYPED_TEST(Fbcsr, GeneratesCorrectMatrixData)
464482
{
465483
using value_type = typename TestFixture::value_type;

0 commit comments

Comments
 (0)