Skip to content

Commit

Permalink
Use new tatami::multiply() to safeguard against overflow.
Browse files Browse the repository at this point in the history
The values being multiplied were already size_t's so it isn't strictly
necessary but it should be odd enough to be self-documenting.

Also did some linting to distinguish between multiplication and pointers.
  • Loading branch information
LTLA committed Apr 30, 2024
1 parent e6cdf1b commit dbfa22c
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions include/tatami_mtx/load_matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,17 @@ std::shared_ptr<tatami::Matrix<Data_, Index_> > load_sparse_matrix_index(Parser_
template<bool row_, typename Data_, typename Index_, typename StoredData_, typename Parser_>
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_dense_matrix_basic(Parser_& parser, eminem::Field field, size_t NR, size_t NC) {
std::vector<StoredData_> values;
auto total_size = tatami::multiply(NR, NC);
if constexpr(row_) {
values.resize(NR * NC);
values.resize(total_size);
} else {
values.reserve(NR * NC);
values.reserve(total_size);
}

if (field == eminem::Field::INTEGER) {
parser.scan_integer([&](size_t r, size_t c, int v) -> void {
if constexpr(row_) {
values[(r - 1) * NC + (c - 1)] = v;
values[tatami::multiply_and_add(r - 1, NC, c - 1)] = v;
} else {
values.push_back(v); // Matrix Market ARRAY format is already column-major
}
Expand All @@ -125,7 +126,7 @@ std::shared_ptr<tatami::Matrix<Data_, Index_> > load_dense_matrix_basic(Parser_&
} else if (field == eminem::Field::REAL || field == eminem::Field::DOUBLE) {
parser.scan_real([&](size_t r, size_t c, double v) -> void {
if constexpr(row_) {
values[(r - 1) * NC + (c - 1)] = v;
values[tatami::multiply_and_add(r - 1, NC, c - 1)] = v;
} else {
values.push_back(v);
}
Expand Down Expand Up @@ -216,7 +217,7 @@ std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix(byteme::Reader& read
* @return Pointer to a `tatami::Matrix` instance containing data from the Matrix Market file.
*/
template<bool row_, typename Data_, typename Index_, bool parallel_ = false, typename StoredData_ = Automatic, typename StoredIndex_ = Automatic>
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_text_file(const char * filepath, size_t bufsize = 65536) {
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_text_file(const char* filepath, size_t bufsize = 65536) {
byteme::RawFileReader reader(filepath, bufsize);
return load_matrix<row_, Data_, Index_, StoredData_, StoredIndex_, parallel_>(reader);
}
Expand All @@ -239,7 +240,7 @@ std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_text_file(const
* @return Pointer to a `tatami::Matrix` instance containing data from the Matrix Market file.
*/
template<bool row_, typename Data_, typename Index_, bool parallel_ = false, typename StoredData_ = Automatic, typename StoredIndex_ = Automatic>
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_gzip_file(const char * filepath, size_t bufsize = 65536) {
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_gzip_file(const char* filepath, size_t bufsize = 65536) {
byteme::GzipFileReader reader(filepath, bufsize);
return load_matrix<row_, Data_, Index_, StoredData_, StoredIndex_, parallel_>(reader);
}
Expand All @@ -260,7 +261,7 @@ std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_gzip_file(const
* @return Pointer to a `tatami::Matrix` instance containing data from the Matrix Market file.
*/
template<bool row_, typename Data_, typename Index_, bool parallel_ = false, typename StoredData_ = Automatic, typename StoredIndex_ = Automatic>
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_some_file(const char * filepath, size_t bufsize = 65536) {
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_some_file(const char* filepath, size_t bufsize = 65536) {
byteme::SomeFileReader reader(filepath, bufsize);
return load_matrix<row_, Data_, Index_, StoredData_, StoredIndex_, parallel_>(reader);
}
Expand All @@ -283,7 +284,7 @@ std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_some_file(const
* @return Pointer to a `tatami::Matrix` instance containing data from the Matrix Market file.
*/
template<bool row_, typename Data_, typename Index_, typename StoredData_ = Automatic, typename StoredIndex_ = Automatic, bool parallel_ = false>
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_text_buffer(const unsigned char * buffer, size_t n) {
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_text_buffer(const unsigned char* buffer, size_t n) {
byteme::RawBufferReader reader(buffer, n);
return load_matrix<row_, Data_, Index_, StoredData_, StoredIndex_, parallel_>(reader);
}
Expand All @@ -309,7 +310,7 @@ std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_text_buffer(con
* @return Pointer to a `tatami::Matrix` instance containing data from the Matrix Market file.
*/
template<bool row_, typename Data_, typename Index_, typename StoredData_ = Automatic, typename StoredIndex_ = Automatic, bool parallel_ = false>
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_zlib_buffer(const unsigned char * buffer, size_t n, int compression = 3, size_t bufsize = 65536) {
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_zlib_buffer(const unsigned char* buffer, size_t n, int compression = 3, size_t bufsize = 65536) {
byteme::ZlibBufferReader reader(buffer, n, compression, bufsize);
return load_matrix<row_, Data_, Index_, StoredData_, StoredIndex_, parallel_>(reader);
}
Expand All @@ -331,7 +332,7 @@ std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_zlib_buffer(con
* @return Pointer to a `tatami::Matrix` instance containing data from the Matrix Market file.
*/
template<bool row_, typename Data_, typename Index_, typename StoredData_ = Automatic, typename StoredIndex_ = Automatic, bool parallel_ = false>
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_some_buffer(const unsigned char * buffer, size_t n, size_t bufsize = 65536) {
std::shared_ptr<tatami::Matrix<Data_, Index_> > load_matrix_from_some_buffer(const unsigned char* buffer, size_t n, size_t bufsize = 65536) {
byteme::SomeBufferReader reader(buffer, n, bufsize);
return load_matrix<row_, Data_, Index_, StoredData_, StoredIndex_, parallel_>(reader);
}
Expand Down

0 comments on commit dbfa22c

Please sign in to comment.