diff --git a/source/source_io/module_wf/read_wfc_pw.cpp b/source/source_io/module_wf/read_wfc_pw.cpp index b322a4a9788..a985707fbd6 100644 --- a/source/source_io/module_wf/read_wfc_pw.cpp +++ b/source/source_io/module_wf/read_wfc_pw.cpp @@ -9,6 +9,8 @@ #include "source_base/timer.h" #include "source_base/vector3.h" +#include + void ModuleIO::read_wfc_pw(const std::string& filename, const ModulePW::PW_Basis_K* pw_wfc, const int rank_in_pool, @@ -195,9 +197,18 @@ void ModuleIO::read_wfc_pw(const std::string& filename, rfs >> size; } + const size_t nxyz_sz = static_cast(nx) * static_cast(ny) * static_cast(nz); + if (nx <= 0 || ny <= 0 || nz <= 0 + || nxyz_sz > static_cast(std::numeric_limits::max())) + { + ModuleBase::WARNING_QUIT("ModuleIO::read_wfc_pw", + "Invalid FFT grid or nx*ny*nz overflow for glo_order allocation."); + } + const int nxyz = static_cast(nxyz_sz); + // map global index to read ordering for plane waves - glo_order = new int[nx * ny * nz]; - for (int i = 0; i < nx * ny * nz; i++) + glo_order = new int[nxyz]; + for (int i = 0; i < nxyz; i++) { glo_order[i] = -1; }