Describe the Code Quality Issue
After a very long time debugging, @jinzx10, @caic99 and I find the bug reported by issue #4540.
See the following code piece:
|
std::vector<double> ibz2bz(this->nkstot); |
what will happen if:
|
for (int ik = 0; ik < nkstot_ibz; ik++) |
|
{ |
|
table += FmtCore::format("%8d%12.8f%12.8f%12.8f%8.4f%8d\n", |
|
ik + 1, |
|
kvec_d_ibz[ik].x, |
|
kvec_d_ibz[ik].y, |
|
kvec_d_ibz[ik].z, |
|
wk_ibz[ik], |
|
ibz2bz[ik]); |
|
} |
?
It will trigger an undefined behavior because the double is parsed like int by
formatter (the ABACUS in-built library for formatting strings, implemented in
module_base/formatter.h), that cases recent failure in integrated test.
The implementation of function FmtCore::format is quite simple:
|
template<typename... Ts> |
|
static inline std::string format(const char* fmt, const Ts&... args) |
|
{ |
|
const int size = snprintf(nullptr, 0, fmt, FmtCore::filter(args)...) + 1; |
|
std::string dst(size, ' '); |
|
const int size_filled = snprintf(&dst[0], size, fmt, FmtCore::filter(args)...); |
|
dst.resize(size_filled); |
|
return dst; |
|
} |
But the c-style function
snprintf itself cannot identify the datatype. So I would suggest to use something like
static_assert before using this function.
Additional Context
No response
Task list for Issue attackers (only for developers)
Describe the Code Quality Issue
After a very long time debugging, @jinzx10, @caic99 and I find the bug reported by issue #4540.
See the following code piece:
abacus-develop/source/module_cell/klist.cpp
Line 776 in db90f92
what will happen if:
abacus-develop/source/module_cell/klist.cpp
Lines 944 to 953 in db90f92
?
It will trigger an undefined behavior because the double is parsed like int by
formatter(the ABACUS in-built library for formatting strings, implemented inmodule_base/formatter.h), that cases recent failure in integrated test.The implementation of function
FmtCore::formatis quite simple:abacus-develop/source/module_base/formatter.h
Lines 40 to 48 in db90f92
But the c-style function
snprintfitself cannot identify the datatype. So I would suggest to use something likestatic_assertbefore using this function.Additional Context
No response
Task list for Issue attackers (only for developers)