diff --git a/source/source_hsolver/module_genelpa/utils.cpp b/source/source_hsolver/module_genelpa/utils.cpp index ed79a5790a..bceb782b7f 100644 --- a/source/source_hsolver/module_genelpa/utils.cpp +++ b/source/source_hsolver/module_genelpa/utils.cpp @@ -91,8 +91,20 @@ void loadMatrix(const char FileName[], int nFull, double* a, int* desca, int bla const int ROOT_PROC = 0; std::ifstream matrixFile; - if (myid == ROOT_PROC) + if (myid == ROOT_PROC){ matrixFile.open(FileName); + if (!matrixFile.is_open()) + { + #ifdef __MPI + std::cerr << "LoadError in opening '" << FileName << "'" << std::endl; + MPI_Abort(MPI_COMM_WORLD, 1); + #else + throw std::runtime_error(std::string("LoadError in opening '") + FileName + "'"); + #endif + } + + } + double* b = nullptr; // buffer const int MAX_BUFFER_SIZE = 1e9; // max buffer size is 1GB diff --git a/source/source_relax/bfgs.cpp b/source/source_relax/bfgs.cpp index 078a5122f9..7bf9b33912 100644 --- a/source/source_relax/bfgs.cpp +++ b/source/source_relax/bfgs.cpp @@ -3,12 +3,18 @@ #include "source_io/module_parameter/parameter.h" #include "ions_move_basic.h" #include "source_cell/update_cell.h" -#include "source_cell/print_cell.h" // lanshuyue add 2025-06-19 +#include "source_cell/print_cell.h" // lanshuyue add 2025-06-19 +#include +#include //! initialize H0、H、pos0、force0、force void BFGS::allocate(const int _size) { assert(_size > 0); + // make sure 3*size doesn't overflow + if (_size > std::numeric_limits::max() / 3) { + throw std::overflow_error("3*size overflow"); + } alpha=70;//default value in ase is 70 maxstep=PARAM.inp.relax_bfgs_rmax; size=_size; @@ -126,9 +132,15 @@ void BFGS::PrepareStep(std::vector>& force, this->Update(changedpos, changedforce,H,ucell); //! call dysev - std::vector omega(3*size); - std::vector work(3*size*3*size); - int lwork=3*size*3*size; + int three_size = 3 * size; + // ensure three_size*three_size doesn't overflow + if (three_size > 0 && three_size > std::numeric_limits::max() / three_size) { + throw std::overflow_error("Dimension is too large"); + } + int total_size = three_size * three_size; + std::vector omega(three_size); + std::vector work(total_size); + int lwork = total_size; int info=0; std::vector H_flat; @@ -137,15 +149,15 @@ void BFGS::PrepareStep(std::vector>& force, H_flat.insert(H_flat.end(), row.begin(), row.end()); } - int value=3*size; - int* ptr=&value; + int value = three_size; + int* ptr = &value; dsyev_("V","U",ptr,H_flat.data(),ptr,omega.data(),work.data(),&lwork,&info); - std::vector> V(3*size, std::vector(3*size, 0.0)); - for(int i = 0; i < 3*size; i++) + std::vector> V(three_size, std::vector(three_size, 0.0)); + for(int i = 0; i < three_size; i++) { - for(int j = 0; j < 3*size; j++) + for(int j = 0; j < three_size; j++) { - V[j][i] = H_flat[3*size*i + j]; + V[j][i] = H_flat[three_size*i + j]; } } std::vector a=DotInMAndV2(V, changedforce); @@ -247,6 +259,13 @@ void BFGS::Update(std::vector& pos, H = MSubM(H, term4); } +inline int calcsize_first(int size) { + if (size > std::numeric_limits::max() / 3) { + throw std::overflow_error("3*size overflow"); + } + return 3 * size; +} + void BFGS::DetermineStep(std::vector& steplength, std::vector>& dpos, double& maxstep) @@ -269,14 +288,15 @@ void BFGS::DetermineStep(std::vector& steplength, void BFGS::UpdatePos(UnitCell& ucell) { - double a[3*size]; + int three_size = calcsize_first(size); + std::vector a(three_size); for(int i=0;i grad= std::vector(3*size, 0.0); + int three_size = calcsize_first(size); + std::vector grad(three_size, 0.0); int iat = 0; for (int it = 0; it < ucell.ntype; it++) { @@ -338,7 +359,7 @@ void BFGS::CalculateLargestGrad(const ModuleBase::matrix& _force,UnitCell& ucell } } largest_grad = 0.0; - for (int i = 0; i < 3*size; i++) + for (int i = 0; i < three_size; i++) { if (largest_grad < std::abs(grad[i])) {