-
Notifications
You must be signed in to change notification settings - Fork 13.5k
[clang-tidy] Check request: detect incorrect usage of std::bit_cast
#106987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Comments
@llvm/issue-subscribers-clang-tidy Author: Carlos Galvez (carlosgalvezp)
`std::bit_cast` is often presented as the correct alternative to type punning in C++. However, we see people use it as a drop-in replacement to `reinterpret_cast` , which only obfuscates the code and still has UB:
float x{};
-int* y = reinterpret_cast<int*>(&x);
+int* y = std::bit_cast<int*>(&x); This usage is incorrect. Instead, the code should do: int y = std::bit_cast<int>(x); Most likely all usages of |
carlosgalvezp
pushed a commit
to carlosgalvezp/llvm-project
that referenced
this issue
Sep 10, 2024
To detect unsafe use of bit_cast that should be reinterpret_cast instead. Otherwise, bit_cast bypasses all checks done by compilers and linters. Fixes llvm#106987
carlosgalvezp
pushed a commit
to carlosgalvezp/llvm-project
that referenced
this issue
Sep 11, 2024
To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all checks done by compilers and linters. Fixes llvm#106987
carlosgalvezp
pushed a commit
to carlosgalvezp/llvm-project
that referenced
this issue
Sep 15, 2024
To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all checks done by compilers and linters. Fixes llvm#106987
carlosgalvezp
pushed a commit
to carlosgalvezp/llvm-project
that referenced
this issue
Sep 17, 2024
To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all checks done by compilers and linters. Fixes llvm#106987
carlosgalvezp
pushed a commit
to carlosgalvezp/llvm-project
that referenced
this issue
Sep 27, 2024
To detect unsafe use of bit_cast. Otherwise, bit_cast bypasses all checks done by compilers and linters. Fixes llvm#106987
carlosgalvezp
pushed a commit
to carlosgalvezp/llvm-project
that referenced
this issue
Oct 2, 2024
To detect unsafe usages of casting a pointer to another via copying the bytes from one into the other, either via std::bit_cast or via memcpy.use of bit_cast. This is currently not caught by any other means. Fixes llvm#106987
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
std::bit_cast
is often presented as the correct alternative to type punning in C++. However, we see people use it as a drop-in replacement toreinterpret_cast
, which only obfuscates the code and still has UB:This usage is incorrect. Instead, the code should do:
Most likely all usages of
std::bit_cast<PointerType>
are incorrect.The text was updated successfully, but these errors were encountered: