Skip to content

[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

Closed
carlosgalvezp opened this issue Sep 2, 2024 · 1 comment · Fixed by #108083
Closed

[clang-tidy] Check request: detect incorrect usage of std::bit_cast #106987

carlosgalvezp opened this issue Sep 2, 2024 · 1 comment · Fixed by #108083
Labels
check-request Request for a new check in clang-tidy clang-tidy

Comments

@carlosgalvezp
Copy link
Contributor

carlosgalvezp commented Sep 2, 2024

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 std::bit_cast<PointerType> are incorrect.

@carlosgalvezp carlosgalvezp added clang-tidy check-request Request for a new check in clang-tidy labels Sep 2, 2024
@llvmbot
Copy link
Member

llvmbot commented Sep 2, 2024

@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&lt;int*&gt;(&amp;x);
+int* y = std::bit_cast&lt;int*&gt;(&amp;x);

This usage is incorrect. Instead, the code should do:

int y = std::bit_cast&lt;int&gt;(x);

Most likely all usages of std::bit_cast&lt;PointerType&gt; are incorrect.

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
Labels
check-request Request for a new check in clang-tidy clang-tidy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants