Fix a bug in CompressedRowSparseMatrix::AppendRows

The test for CompressedRowSparseMatrix::AppendRows tries to add
a matrix of size zero, which results in an invalid pointer deferencing
even though that pointer is never written to.

Change-Id: I97dba37082bd5dad242ae1af0447a9178cd92027
This commit is contained in:
Sameer Agarwal
2015-09-05 15:30:01 -07:00
parent 67622b080c
commit 1ac3dd223c
2 changed files with 15 additions and 4 deletions
+3
View File
@@ -27,6 +27,9 @@ New Features
Bug Fixes & Minor Changes
-------------------------
#. Fix invalid memory access bug in
``CompressedRowSparseMatrix::AppendRows`` when it was called with a
matrix of size zero.
#. Build position independent code when compiling Ceres statically
(Alexander Alekhin).
#. Fix a bug in DetectStructure (Johannes Schonberger).
+12 -4
View File
@@ -241,16 +241,24 @@ void CompressedRowSparseMatrix::AppendRows(const CompressedRowSparseMatrix& m) {
<< "The matrix being appended has: " << m.row_blocks().size()
<< " row blocks.";
if (m.num_rows() == 0) {
return;
}
if (cols_.size() < num_nonzeros() + m.num_nonzeros()) {
cols_.resize(num_nonzeros() + m.num_nonzeros());
values_.resize(num_nonzeros() + m.num_nonzeros());
}
// Copy the contents of m into this matrix.
std::copy(m.cols(), m.cols() + m.num_nonzeros(), &cols_[num_nonzeros()]);
std::copy(m.values(),
m.values() + m.num_nonzeros(),
&values_[num_nonzeros()]);
DCHECK_LT(num_nonzeros(), cols_.size());
if (m.num_nonzeros() > 0) {
std::copy(m.cols(), m.cols() + m.num_nonzeros(), &cols_[num_nonzeros()]);
std::copy(m.values(),
m.values() + m.num_nonzeros(),
&values_[num_nonzeros()]);
}
rows_.resize(num_rows_ + m.num_rows() + 1);
// new_rows = [rows_, m.row() + rows_[num_rows_]]
std::fill(rows_.begin() + num_rows_,