Open Quuxplusone opened 3 years ago
__builtin_memcpy does give undefined behavior with gcc when passed null pointer arguments.
__builtin_memcpy(null, null, C) where C is > 0 is UB.
If we know that C is value > 0, LLVM infers that dst and src ptrs must be nonnull.
__builtin_memcpy(dst, src, dynamicsize) - LLVM in general case does nothing fancy here, in same cases LLVM can prove that dynamicsize is > 0, so it is a same story as C > 0 case.
The C function memcpy cannot be given null pointers as arguments, even if the size is 0. This is checked by UBSan.
It is unclear whether __builtin_memcpy has this same restriction. UBSan accepts calls to __builtin_memcpy with null pointer arguments as long as the size is 0. The documentation does not say whether this is guaranteed behavior.
I hope that it is defined to call it with null pointer arguments, since the entire reason I'm using it is to avoid that check.