CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
53 lines
1.9 KiB
ReStructuredText
53 lines
1.9 KiB
ReStructuredText
.. title:: clang-tidy - bugprone-bitwise-pointer-cast
|
|
|
|
bugprone-bitwise-pointer-cast
|
|
=============================
|
|
|
|
Warns about code that tries to cast between pointers by means of
|
|
``std::bit_cast`` or ``memcpy``.
|
|
|
|
The motivation is that ``std::bit_cast`` is advertised as the safe alternative
|
|
to type punning via ``reinterpret_cast`` in modern C++. However, one should not
|
|
blindly replace ``reinterpret_cast`` with ``std::bit_cast``, as follows:
|
|
|
|
.. code-block:: c++
|
|
|
|
int x{};
|
|
-float y = *reinterpret_cast<float*>(&x);
|
|
+float y = *std::bit_cast<float*>(&x);
|
|
|
|
The drop-in replacement behaves exactly the same as ``reinterpret_cast``, and
|
|
Undefined Behavior is still invoked. ``std::bit_cast`` is copying the bytes of
|
|
the input pointer, not the pointee, into an output pointer of a different type,
|
|
which may violate the strict aliasing rules. However, simply looking at the
|
|
code, it looks "safe", because it uses ``std::bit_cast`` which is advertised as
|
|
safe.
|
|
|
|
The solution to safe type punning is to apply ``std::bit_cast`` on value types,
|
|
not on pointer types:
|
|
|
|
.. code-block:: c++
|
|
|
|
int x{};
|
|
float y = std::bit_cast<float>(x);
|
|
|
|
This way, the bytes of the input object are copied into the output object,
|
|
which is much safer. Do note that Undefined Behavior can still occur, if there
|
|
is no value of type ``To`` corresponding to the value representation produced.
|
|
Compilers may be able to optimize this copy and generate identical assembly to
|
|
the original ``reinterpret_cast`` version.
|
|
|
|
Code before C++20 may backport ``std::bit_cast`` by means of ``memcpy``, or
|
|
simply call ``memcpy`` directly, which is equally problematic. This is also
|
|
detected by this check:
|
|
|
|
.. code-block:: c++
|
|
|
|
int* x{};
|
|
float* y{};
|
|
std::memcpy(&y, &x, sizeof(x));
|
|
|
|
Alternatively, if a cast between pointers is truly wanted, ``reinterpret_cast``
|
|
should be used, to clearly convey the intent and enable warnings from compilers
|
|
and linters, which should be addressed accordingly.
|