From 12a880921dcf39a72ae43645f0188f8bec6c3fb4 Mon Sep 17 00:00:00 2001 From: "Wu, Xin-Chuan" Date: Wed, 5 Aug 2026 10:49:02 -0700 Subject: [PATCH 1/2] Fix matrix index bounds validation --- include/tinymatrix.hpp | 13 +++++++++---- pybind11/intelqs_py.cpp | 21 ++++++++++----------- unit_test/import_iqs.py | 19 +++++++++++++++++++ unit_test/include/tinymatrix_test.hpp | 13 +++++++++++++ 4 files changed, 51 insertions(+), 15 deletions(-) diff --git a/include/tinymatrix.hpp b/include/tinymatrix.hpp index b49e5c4d..e5d068fa 100644 --- a/include/tinymatrix.hpp +++ b/include/tinymatrix.hpp @@ -18,6 +18,7 @@ #include #include #include +#include /// \addtogroup util /// @{ @@ -141,8 +142,10 @@ class TinyMatrix /// \pre inumRows() && "Row index out of range"); - assert(j < this->numCols() && "Column index out of range"); + if (i >= this->numRows()) + throw std::out_of_range("TinyMatrix row index out of range"); + if (j >= this->numCols()) + throw std::out_of_range("TinyMatrix column index out of range"); return data_[i][j]; } @@ -154,8 +157,10 @@ class TinyMatrix /// \pre inumRows() && "Row index out of range"); - assert(j < this->numCols() && "Column index out of range"); + if (i >= this->numRows()) + throw std::out_of_range("TinyMatrix row index out of range"); + if (j >= this->numCols()) + throw std::out_of_range("TinyMatrix column index out of range"); return data_[i][j]; } diff --git a/pybind11/intelqs_py.cpp b/pybind11/intelqs_py.cpp index aa025a18..6889bc6d 100644 --- a/pybind11/intelqs_py.cpp +++ b/pybind11/intelqs_py.cpp @@ -96,16 +96,15 @@ PYBIND11_MODULE(intelqs_py, m) .def(py::init<>()) .def(py::init<>()) // Access element: - .def("__getitem__", [](const iqs::ChiMatrix &a, std::pair i, int column) { - if (i.first > 4) throw py::index_error(); - if (i.second > 4) throw py::index_error(); -std::cout << "ciao\n"; + .def("__getitem__", [](const iqs::ChiMatrix &a, std::pair i) { + if (i.first < 0 || i.first >= 4) throw py::index_error(); + if (i.second < 0 || i.second >= 4) throw py::index_error(); return a(i.first, i.second); }, py::is_operator()) // Set element: .def("__setitem__", [](iqs::ChiMatrix &a, std::pair i, ComplexDP value) { - if (i.first > 4) throw py::index_error(); - if (i.second > 4) throw py::index_error(); + if (i.first < 0 || i.first >= 4) throw py::index_error(); + if (i.second < 0 || i.second >= 4) throw py::index_error(); a(i.first, i.second) = value; }, py::is_operator()) #if 0 @@ -147,15 +146,15 @@ std::cout << "ciao\n"; .def(py::init<>()) .def(py::init<>()) // Access element: - .def("__getitem__", [](const iqs::ChiMatrix &a, std::pair i, int column) { - if (i.first > 16) throw py::index_error(); - if (i.second > 16) throw py::index_error(); + .def("__getitem__", [](const iqs::ChiMatrix &a, std::pair i) { + if (i.first < 0 || i.first >= 16) throw py::index_error(); + if (i.second < 0 || i.second >= 16) throw py::index_error(); return a(i.first, i.second); }, py::is_operator()) // Set element: .def("__setitem__", [](iqs::ChiMatrix &a, std::pair i, ComplexDP value) { - if (i.first > 16) throw py::index_error(); - if (i.second > 16) throw py::index_error(); + if (i.first < 0 || i.first >= 16) throw py::index_error(); + if (i.second < 0 || i.second >= 16) throw py::index_error(); a(i.first, i.second) = value; }, py::is_operator()) .def("SolveEigenSystem", &iqs::ChiMatrix::SolveEigenSystem) diff --git a/unit_test/import_iqs.py b/unit_test/import_iqs.py index c8c43069..4fb4f3a2 100644 --- a/unit_test/import_iqs.py +++ b/unit_test/import_iqs.py @@ -4,6 +4,25 @@ sys.path.insert(0, "../build/lib/") import intelqs_py as iqs + +def assert_index_error(operation): + try: + operation() + except IndexError: + return + raise AssertionError("Invalid matrix index did not raise IndexError") + + +for matrix_type, dimension in ((iqs.CM4x4, 4), (iqs.CM16x16, 16)): + matrix = matrix_type() + matrix[dimension - 1, dimension - 1] = 1 + 2j + assert matrix[dimension - 1, dimension - 1] == 1 + 2j + + for invalid_index in ((-1, 0), (0, -1), (dimension, 0), (0, dimension)): + assert_index_error(lambda index=invalid_index: matrix[index]) + assert_index_error(lambda index=invalid_index: matrix.__setitem__(index, 0j)) + + iqs.EnvInit() rank = iqs.MPIEnvironment.GetRank() diff --git a/unit_test/include/tinymatrix_test.hpp b/unit_test/include/tinymatrix_test.hpp index a47ef2c4..95379df2 100644 --- a/unit_test/include/tinymatrix_test.hpp +++ b/unit_test/include/tinymatrix_test.hpp @@ -162,4 +162,17 @@ TEST_F(TinyMatrixTest, ComplexDP) ////////////////////////////////////////////////////////////////////////////// +TEST_F(TinyMatrixTest, OutOfRangeAccess) +{ + iqs::TinyMatrix mat; + const iqs::TinyMatrix& const_mat = mat; + + ASSERT_THROW(mat(2, 0), std::out_of_range); + ASSERT_THROW(mat(0, 3), std::out_of_range); + ASSERT_THROW(const_mat(2, 0), std::out_of_range); + ASSERT_THROW(const_mat(0, 3), std::out_of_range); +} + +////////////////////////////////////////////////////////////////////////////// + #endif // header guard TINYMATRIX_TEST_HPP From 1eab81fc2be2f9e50a71f89d1c90d5f4b562b554 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 5 Aug 2026 18:09:20 +0000 Subject: [PATCH 2/2] Fix Dockerfile: replace deprecated apt-key with gpg for Intel MKL key Co-authored-by: ryanxw <16125496+ryanxw@users.noreply.github.com> --- Dockerfile | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8482fef6..f1269d99 100644 --- a/Dockerfile +++ b/Dockerfile @@ -41,10 +41,9 @@ RUN tar -xzf cmake-3.15.2-Linux-x86_64.tar.gz -C /usr/local/ --strip-components= # Fetch and install the Intel MKL libraries required for building the Intel-QS simulator. WORKDIR swpkgs/mkl -RUN wget "https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB" -RUN apt-key add GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB -RUN rm GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB -RUN sh -c 'echo deb https://apt.repos.intel.com/mkl all main > /etc/apt/sources.list.d/intel-mkl.list' +RUN apt-get install -y gpg +RUN wget -qO - "https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB" | gpg --dearmor -o /usr/share/keyrings/intel-sw-products.gpg +RUN sh -c 'echo "deb [signed-by=/usr/share/keyrings/intel-sw-products.gpg] https://apt.repos.intel.com/mkl all main" > /etc/apt/sources.list.d/intel-mkl.list' RUN apt-get update RUN apt-get install -y intel-mkl-64bit-2019.2-057 # Set the (global) environment variable MKLROOT to facilitate the build process.