`round2neg_inf_c` invokes UB, Clang refuses to compile
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 35/100
Research direction
Start with include/boost/numeric/conversion/detail/converter.hpp at line 29 and reproduce the issue by compiling the supplied round2neg_inf_c MWE with Clang. Trace the std::float_round_style instantiation that produces the invalid next value. Done means the MWE compiles without undefined behavior while preserving the intended conversion behavior.
Written by the indexing model from the issue text.
Description
https://github.com/boostorg/numeric_conversion/blob/d1b479f7a4aa54d8ffb93d8dc4ee0c24670210d8/include/boost/numeric/conversion/detail/converter.hpp#L29
This invokes undefined behavior. Clang correctly identifies it and refuses to compile if you try to instantiate round2neg_inf_c. Here is an MWE (verified with Boost 1.89-1.90)
$ clang++ --version
clang version 21.1.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
$ clang++ -c -x c++ - <<EOF
#include <boost/numeric/conversion/converter.hpp>
int main (int argc, char *argv[]) {
boost::numeric::convdetail::round2neg_inf_c x;
}
EOF
In file included from <stdin>:1:
In file included from /usr/include/boost/numeric/conversion/converter.hpp:13:
In file included from /usr/include/boost/numeric/conversion/conversion_traits.hpp:13:
In file included from /usr/include/boost/numeric/conversion/detail/conversion_traits.hpp:21:
In file included from /usr/include/boost/numeric/conversion/detail/is_subranged.hpp:17:
In file included from /usr/include/boost/mpl/multiplies.hpp:17:
In file included from /usr/include/boost/mpl/times.hpp:19:
In file included from /usr/include/boost/mpl/aux_/arithmetic_op.hpp:17:
In file included from /usr/include/boost/mpl/integral_c.hpp:32:
/usr/include/boost/mpl/aux_/integral_wrapper.hpp:62:51: error: in-class initializer for static data member is not a
constant expression
62 | BOOST_STATIC_CONSTANT(AUX_WRAPPER_VALUE_TYPE, next_value = BOOST_MPL_AUX_STATIC_CAST(AUX_WRAPPER_VALUE_TYPE, (N + 1)));
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/boost/config/detail/suffix.hpp:410:72: note: expanded from macro 'BOOST_STATIC_CONSTANT'
410 | # define BOOST_STATIC_CONSTANT(type, assignment) static const type assignment
| ^~~~~~~~~~
<stdin>:4:49: note: in instantiation of template class 'mpl_::integral_c<std::float_round_style,
std::round_toward_neg_infinity>' requested here
4 | boost::numeric::convdetail::round2neg_inf_c x;
| ^
/usr/include/boost/mpl/aux_/integral_wrapper.hpp:62:64: note: integer value 4 is outside the valid range of values
[-4, 3] for the enumeration type 'float_round_style'
62 | BOOST_STATIC_CONSTANT(AUX_WRAPPER_VALUE_TYPE, next_value = BOOST_MPL_AUX_STATIC_CAST(AUX_WRAPPER_VALUE_TYPE, (N + 1)));
| ^
/usr/include/boost/mpl/aux_/static_cast.hpp:24:47: note: expanded from macro 'BOOST_MPL_AUX_STATIC_CAST'
24 | # define BOOST_MPL_AUX_STATIC_CAST(T, expr) static_cast<T>(expr)
| ^
1 error generated.
It appears that Clang reserves 3 bits for std::float_round_style. This makes std::round_toward_neg_infinity + 1 to overflow.
- Dominant language
- C++
- Stars
- 13
- Forks
- 42
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from boostorg/numeric_conversion
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
All issues in boostorg/numeric_conversion
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
flutter-webrtc/flutter-webrtc#2206 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
Component: GLib
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
brave/brave-browser#59300 ·
-
Mute ydb/tests/functional/dstool/test_canonical_requests.py.Test.test_group_take_snapshot in main Openai_reviewed
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
ydb-platform/ydb#53974 · 3 comments ·