Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

check integer overflow in Matrix::setSize #296

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/linalg/Matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ Matrix::Matrix(
memcpy(d_mat, mat, d_alloc_size*sizeof(double));
}
else {
// Check integer multiplication overflow
if (num_rows > INT_MAX / num_cols)
CAROM_ERROR("Matrix::Matrix- new size exceeds maximum integer value!\n");

d_mat = mat;
d_alloc_size = num_rows*num_cols;
d_num_cols = num_cols;
Expand Down
5 changes: 5 additions & 0 deletions lib/linalg/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <complex>
#include <memory>
#include <string>
#include <climits>

namespace CAROM {

Expand Down Expand Up @@ -149,6 +150,10 @@ class Matrix
int num_rows,
int num_cols)
{
// Check integer multiplication overflow
if (num_rows > INT_MAX / num_cols)
CAROM_ERROR("Matrix::setSize- new size exceeds maximum integer value!\n");

int new_size = num_rows*num_cols;
if (new_size > d_alloc_size) {
if (!d_owns_data) {
Expand Down
Loading