-
Notifications
You must be signed in to change notification settings - Fork 78
Make sure memcpy/memmove/memset with size 0 behave correctly #516
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
Comments
@antoyo Can I try this one? Also any pointers on how to solve this issue will be helpful |
There are proposals in progress to allow that: https://www.open-std.org/JTC1/SC22/WG14/www/docs/n3261.pdf Fingers crossed it gets accepted |
After a bit of research, I have found an example where GCC optimizes things based on the assumption that the arguments to memcpy are not null. Here is a sample I found on the internet: https://godbolt.org/z/aPcr1bfPe #include <string.h>
int do_thing1();
int do_thing2();
void test(char *dest, const char *src, size_t len) {
memcpy(dest, src, len);
if (dest == NULL) {
// This branch will be removed by GCC due to undefined behavior.
do_thing1();
} else {
do_thing2();
}
} Here, the memory copy of This does not seem to break Rust code today, at least on x86_64(is |
I don't think we disable that. |
From what I heard, C26 will also permit |
Zero-sized memory accesses are now always permitted, even if the pointer is NULL or dangling (but it must be aligned still). For codegen this means in particular that memcpy/memmove/memset must be lowered to operations that are never UB when the size is 0 (and the pointer is sufficiently aligned). In LLVM that's easy as LLVM's corresponding intrinsics explicitly allow size 0. However, in C, memcpy/memmove/memset with size 0 is UB on NULL (and dangling pointers are impossible to even mention in C), so GCC may use a different semantics for its builtins. For Rust's GCC backend, it's crucial that we use GCC builtins that allow size 0 with any pointer.
The text was updated successfully, but these errors were encountered: