Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions source/source_pw/module_pwdft/soc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ void Fcoef::create(const int i1, const int i2, const int i3)
}
else
{
std::cout << "not allowed!" << std::endl;
std::cout << "Fcoef::create: not allowed!" << std::endl;
if (!(i1 * i2 * i3 > 0)){
std::cout << "i1*i2*i3 must be positive! i1*i2*i3 is " << i1 * i2 * i3 << std::endl;
unsigned long long product = static_cast<unsigned long long>(i1) * static_cast<unsigned long long>(i2) * static_cast<unsigned long long>(i3);
std::cout << "Fcoef::create: possible integer overflow in product i1*i2*i3! i1 is " << i1 << ", i2 is " << i2 << ", i3 is " << i3 << ", product is " << product << std::endl;
}
Comment on lines 31 to 34
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition i1 * i2 * i3 > 0 can itself trigger signed integer overflow (undefined behavior) before you get to the diagnostic, which defeats the purpose of detecting overflow. Also, casting negative int values to unsigned long long will produce very large values, so the printed product can be misleading when the real problem is simply a negative input. A safer approach is to check i1/i2/i3 positivity without multiplying, and only compute a product using a wide signed type (e.g., long long/__int128) after confirming all are positive (or perform overflow-checked multiplication).

Copilot uses AI. Check for mistakes.
if (!(i1 > 0)){
std::cout << "i1 must be positive! i1 is " << i1 << std::endl;
std::cout << "Fcoef::create: i1 must be positive! i1 is " << i1 << std::endl;
}
if (!(i2 > 0)){
std::cout << "i2 must be positive! i2 is " << i2 << std::endl;
std::cout << "Fcoef::create: i2 must be positive! i2 is " << i2 << std::endl;
}
if (!(i3 > 0)){
std::cout << "i3 must be positive! i3 is " << i3 << std::endl;
std::cout << "Fcoef::create: i3 must be positive! i3 is " << i3 << std::endl;
Comment on lines +30 to +42
Copy link

Copilot AI Mar 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are error diagnostics; using std::cerr (instead of std::cout) is generally more appropriate and makes it easier to separate normal output from error output in logs/pipes. Consider switching these streams (and keeping the prefixing you added).

Copilot uses AI. Check for mistakes.
}

}
Expand Down
Loading