about summary refs log tree commit diff
path: root/src/test/ui/iterators/iter-sum-overflow-overflow-checks.rs
diff options
context:
space:
mode:
authorTyler Mandry <tmandry@gmail.com>2019-09-30 14:38:25 -0700
committerGitHub <noreply@github.com>2019-09-30 14:38:25 -0700
commit1cd9b4bc8535da94c8e9288f0eaf69ad7027e539 (patch)
treebfd9691a233e37c19520bd16ea298e109c05f4e1 /src/test/ui/iterators/iter-sum-overflow-overflow-checks.rs
parent3add9790abe9225c34bbf2c9177519af063b1966 (diff)
parentce60da497b664bae7b1476ae4502a073dbb12127 (diff)
downloadrust-1cd9b4bc8535da94c8e9288f0eaf69ad7027e539.tar.gz
rust-1cd9b4bc8535da94c8e9288f0eaf69ad7027e539.zip
Rollup merge of #64891 - SimonSapin:vec-of-fat-raw-ptr, r=sfackler
Fix `vec![x; n]` with null raw fat pointer zeroing the pointer metadata

https://github.com/rust-lang/rust/pull/49496 introduced specialization based on:

```rust
unsafe impl<T: ?Sized> IsZero for *mut T {
    fn is_zero(&self) -> bool {
        (*self).is_null()
    }
}
```

… to call `RawVec::with_capacity_zeroed` for creating `Vec<*mut T>`, which is incorrect for fat pointers since `<*mut T>::is_null` only looks at the data component. That is, a fat pointer can be “null” without being made entirely of zero bits.

This commit fixes it by removing the `?Sized` bound on this impl (and the corresponding `*const T` one). This regresses `vec![x; n]` with `x` a null raw slice of length zero, but that seems exceptionally uncommon. (Vtable pointers are never null, so raw trait objects would not take the fast path anyway.)

An alternative to keep the `?Sized` bound (or even generalize to `impl<U: Copy> IsZero for U`) would be to cast to `&[u8]` of length `size_of::<U>()`, but the optimizer seems not to be able to propagate alignment information and sticks with comparing one byte at a time:

https://rust.godbolt.org/z/xQFkwL

----

Without the library change, the new test fails as follows:

```rust
---- vec::vec_macro_repeating_null_raw_fat_pointer stdout ----
[src/liballoc/tests/vec.rs:1301] ptr_metadata(raw_dyn) = 0x00005596ef95f9a8
[src/liballoc/tests/vec.rs:1306] ptr_metadata(vec[0]) = 0x0000000000000000
thread 'vec::vec_macro_repeating_null_raw_fat_pointer' panicked at 'assertion failed: vec[0] == null_raw_dyn', src/liballoc/tests/vec.rs:1307:5
```
Diffstat (limited to 'src/test/ui/iterators/iter-sum-overflow-overflow-checks.rs')
0 files changed, 0 insertions, 0 deletions