Skip to content

Commit 96b5826

Browse files
committed
Auto merge of #53804 - RalfJung:ptr-invalid, r=nagisa
fix some uses of pointer intrinsics with invalid pointers [Found by miri](rust-lang/miri#446): * `Vec::into_iter` calls `ptr::read` (and the underlying `copy_nonoverlapping`) with an unaligned pointer to a ZST. [According to LLVM devs](https://bugs.llvm.org/show_bug.cgi?id=38583), this is UB because it contradicts the metadata we are attaching to that pointer. * `HashMap` creation calls `ptr:.write_bytes` on a NULL pointer with a count of 0. This is likely not currently UB *currently*, but it violates the rules we are setting in rust-lang/rust#53783, and we might want to exploit those rules later (e.g. with more `nonnull` attributes for LLVM). Probably what `HashMap` really should do is use `NonNull::dangling()` instead of 0 for the empty case, but that would require a more careful analysis of the code. It seems like ideally, we should do a review of usage of such intrinsics all over libstd to ensure that they use valid pointers even when the size is 0. Is it worth opening an issue for that?
2 parents 170fa0b + 142b4db commit 96b5826

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

table.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,9 @@ impl<K, V> RawTable<K, V> {
742742
) -> Result<RawTable<K, V>, CollectionAllocErr> {
743743
unsafe {
744744
let ret = RawTable::new_uninitialized_internal(capacity, fallibility)?;
745-
ptr::write_bytes(ret.hashes.ptr(), 0, capacity);
745+
if capacity > 0 {
746+
ptr::write_bytes(ret.hashes.ptr(), 0, capacity);
747+
}
746748
Ok(ret)
747749
}
748750
}

0 commit comments

Comments
 (0)