Issue 123317
Summary Clang wrongly catches FE_INVALID exception
Labels clang
Assignees
Reporter thomasbangels
    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
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to