This issue is a result of a Codex global repository scan.
The WFC_NAO writer warns when Binstream::open() fails, but then continues writing to the stream. Binstream::operator<< and Binstream::write() call fwrite(..., this->fileptr) without validating that fileptr is non-null or that the full write succeeded.
Binstream write helpers:
|
// write a data into file |
|
template<class T> |
|
Binstream& Binstream:: operator<<(const T& data) |
|
{ |
|
const int size=sizeof(T); |
|
fwrite(&data,size,1,this->fileptr); |
|
return *this; |
|
} |
|
|
|
//read an array of data |
|
template<class T> |
|
Binstream& Binstream::read(T* data, const int n) |
|
{ |
|
const int size=sizeof(T); |
|
size_t ch = fread(data,size,n,this->fileptr); |
|
if(ch<n) |
|
{ |
|
std::cout<<"Error in Binstream: Some dynamic memory didn't be read."<<std::endl; |
|
std::cout<<"Please make you are using op: \"r\""<<std::endl; |
|
exit(0); |
|
} |
|
return *this; |
|
} |
|
|
|
//write an array of data |
|
template<class T> |
|
Binstream& Binstream::write(const T* data, const int n) |
|
{ |
|
const int size=sizeof(T); |
|
fwrite(data,size,n,this->fileptr); |
|
return *this; |
WFC_NAO write paths:
|
{ |
|
ofs.open(name, "a"); |
|
} |
|
else |
|
{ |
|
ofs.open(name, "w"); |
|
} |
|
if (!ofs) |
|
{ |
|
ModuleBase::WARNING("ModuleIO::wfc_nao_write2file", "Can't write local orbital wave functions."); |
|
} |
|
|
|
ofs << nbands; |
|
ofs << nlocal; |
|
} |
|
else |
|
{ |
|
ofs.open(name, "w"); |
|
} |
|
if (!ofs) |
|
{ |
|
ModuleBase::WARNING("ModuleIO::wfc_nao_write2file_complex", "Can't write local orbital wave functions."); |
|
} |
|
ofs << ik + 1; |
Relevant code:
if (!ofs)
{
ModuleBase::WARNING("ModuleIO::wfc_nao_write2file", "Can't write local orbital wave functions.");
}
ofs << nbands;
and:
fwrite(&data, size, 1, this->fileptr);
Suggested fix:
Return or abort immediately after a failed open. Also make Binstream write operations validate fileptr and check that fwrite wrote the requested number of records.
This issue is a result of a Codex global repository scan.
The WFC_NAO writer warns when
Binstream::open()fails, but then continues writing to the stream.Binstream::operator<<andBinstream::write()callfwrite(..., this->fileptr)without validating thatfileptris non-null or that the full write succeeded.Binstream write helpers:
abacus-develop/source/source_io/module_output/binstream.h
Lines 57 to 87 in 84ca04b
WFC_NAO write paths:
abacus-develop/source/source_io/module_wf/write_wfc_nao.cpp
Lines 35 to 48 in 84ca04b
abacus-develop/source/source_io/module_wf/write_wfc_nao.cpp
Lines 128 to 137 in 84ca04b
Relevant code:
and:
Suggested fix:
Return or abort immediately after a failed open. Also make
Binstreamwrite operations validatefileptrand check thatfwritewrote the requested number of records.