Skip to content

Commit 2aa8583

Browse files
authored
Fix(io): check nx*ny*nz before glo_order allocation in read_wfc_pw (#7180)
- Use size_t product and reject non-positive or INT_MAX overflow before new int[]
1 parent a0840ed commit 2aa8583

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

source/source_io/module_wf/read_wfc_pw.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "source_base/timer.h"
1010
#include "source_base/vector3.h"
1111

12+
#include <limits>
13+
1214
void ModuleIO::read_wfc_pw(const std::string& filename,
1315
const ModulePW::PW_Basis_K* pw_wfc,
1416
const int rank_in_pool,
@@ -195,9 +197,18 @@ void ModuleIO::read_wfc_pw(const std::string& filename,
195197
rfs >> size;
196198
}
197199

200+
const size_t nxyz_sz = static_cast<size_t>(nx) * static_cast<size_t>(ny) * static_cast<size_t>(nz);
201+
if (nx <= 0 || ny <= 0 || nz <= 0
202+
|| nxyz_sz > static_cast<size_t>(std::numeric_limits<int>::max()))
203+
{
204+
ModuleBase::WARNING_QUIT("ModuleIO::read_wfc_pw",
205+
"Invalid FFT grid or nx*ny*nz overflow for glo_order allocation.");
206+
}
207+
const int nxyz = static_cast<int>(nxyz_sz);
208+
198209
// map global index to read ordering for plane waves
199-
glo_order = new int[nx * ny * nz];
200-
for (int i = 0; i < nx * ny * nz; i++)
210+
glo_order = new int[nxyz];
211+
for (int i = 0; i < nxyz; i++)
201212
{
202213
glo_order[i] = -1;
203214
}

0 commit comments

Comments
 (0)