]> https://gitweb.dealii.org/ - dealii.git/commit
distributed/tria.cc: avoid FPE due to undefined behavior 16820/head
authorMatthias Maier <tamiko@43-1.org>
Sat, 30 Mar 2024 00:49:16 +0000 (19:49 -0500)
committerMatthias Maier <tamiko@43-1.org>
Sat, 30 Mar 2024 00:49:16 +0000 (19:49 -0500)
commitd55bf9d5bf7a19dcfb6689f368a19c1b7ab942dc
tree99bbac91fef5b944515f2db4c3738614a989c048
parentd27057778855a4e4794ae5d6eed569bbc7694626
distributed/tria.cc: avoid FPE due to undefined behavior

The following construct
```
static_cast<types::subdomain_id>(this_sc_point[dim])
```
triggers a floating point exception when the stored double is -1.0 (at
least with gcc-11 and avx512 instructions enabled).

Judging from cppreference [1] this is undefined behavior:
"""
A prvalue of floating-point type can be converted to a prvalue of any
integer type. The fractional part is truncated, that is, the fractional
part is discarded.

If the truncated value cannot fit into the destination type, the
behavior is undefined (even when the destination type is unsigned,
modulo arithmetic does not apply).
"""

Thus, work around the issue by explicitly setting negative values to
`numbers::invalid_subdomain_id` if we encounter -1.

[1] https://en.cppreference.com/w/cpp/language/implicit_conversion
source/distributed/tria.cc

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.