From 698f2dfa8c1b522d7359e1d93bd6f5efdf839408 Mon Sep 17 00:00:00 2001 From: chaomianpkp <2300011033@stu.pku.edu.cn> Date: Sat, 28 Mar 2026 12:49:34 +0000 Subject: [PATCH] Fix(io): check nx*ny*nz before glo_order allocation in read_wfc_pw - Use size_t product and reject non-positive or INT_MAX overflow before new int[] --- source/source_io/module_wf/read_wfc_pw.cpp | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) 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; }