<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/123317>123317</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang wrongly catches FE_INVALID exception
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
thomasbangels
</td>
</tr>
</table>
<pre>
When the `FE_INVALID` exception trap is enabled, an exception is caught but there is no reason why this code should produce such an exception.
Script:
```c++
#include <cfenv>
#include <iostream>
#include <Eigen/Eigen>
int main()
{
feenableexcept(FE_INVALID);
// Ask number from terminal so the compiler cannot optimize at compile time.
// Enter 0.0
double a;
std::cout << "input number: " << std::flush;
std::cin >> a;
Eigen::Vector3d x1(1.0 + a, 2.0 + a, 3.0 + a);
Eigen::Vector3d x2(1.0, 2.0, 3.0);
Eigen::Vector3d v = x2 - x1;
double dist = v.norm();
std::cout << "dist = " << dist << std::endl;
if (dist == 0) {
// std::cout << "if-branch" << std::endl;
v = Eigen::Vector3d(1, 0, 0);
} else {
// std::cout << "else-branch" << std::endl;
v /= dist;
}
std::cout << "v = " << v << std::endl;
return 0;
}
```
How to reproduce the error:
- Compile using clang++ with -O3 optimizations
- When the "input number" is asked, enter `0.0`
I found out that no exception is caught (which is correct) in the following scenarios:
- When the prints, commented out in the branches, are re-enabled
- When compiling with clang++ without the -O3 optimizations
- When compiling with g++ (with and without optimizations)
System details:
Operating system: Ubuntu 24.04
Clang version: 18.1.3
GCC version: 13.3.0
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUVU1v4zYQ_TX0ZWCBIh1bOuigOHEboOgeFt0eC0oaWexSpMAPO-mvL6gPx856d7GAEUUczps3j5wn4Zw8asSCPDySh6eVCL4ztvCd6YWrhD6icqvKNG_F3x1q8B0C2dLD8z8vf34p_3h5IlsK-Frj4KXR4K0YQDpALSqFDWF7EPoqLh3UIhw7D1XwEc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