Repository navigation
forward_declare: give slices a Name that no array can have - #3026
Open
dhivadhiva wants to merge 1 commit into
Open
dhivadhiva wants to merge 1 commit into
dhivadhiva wants to merge 1 commit into
Conversation
`[T]` used `([(); usize::MAX], T::Name)` as its `CppType::Name`, relying on `[Incomplete<..>; usize::MAX]` not being a valid type. Since `Unsized` became `()`, `Incomplete` is zero-sized, so that array type exists and has the same `Name` as the slice. `&[T]` (a fat pointer) and `&[Incomplete<..>; usize::MAX]` (a thin pointer) therefore ended up in the same transmutability class. The `Vec<&T>` -> `Vec<&U>` `CppCoerce` impl reinterprets the buffer without a size check, so safe code could turn `Vec<&[T]>` into `Vec<&[Fwd; usize::MAX]>`, push a copy of the thin pointer, and coerce back to get a `&[T]` whose length is a pointer value. Indexing it reads and writes out of bounds. Use a dedicated `internal::SliceName<Name>` for slices, and check that `&T` and `&U` have the same size in the `Vec` impl like the blanket impl already does. Add a test for `Vec<&[T]>` coercion.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[T]used([(); usize::MAX], T::Name)as itsCppType::Name. That relied on[Incomplete<..>; usize::MAX]not being a valid type, but sinceUnsizedbecame(),Incompleteis zero-sized and that array type exists. ItsNameis the same as the slice's, so&[T](a fat pointer) and&[Incomplete<..>; usize::MAX](a thin pointer) ended up in the same transmutability class.The
Vec<&T>->Vec<&U>CppCoerceimpl reinterprets the buffer without checking that&Tand&Uhave the same size, so this is reachable from safe code:This change:
internal::SliceName<Name>, so no array shares a slice'sName;size_of::<&T>() == size_of::<&U>()in theVecimpl, like the blanketT -> Uimpl already does;Vec<&[T]><->Vec<&[Incomplete]>round trip toforward_declare_macros_test.rs.Tested outside Bazel with
cargoon nightly-2026-11-11, buildingforward_declare,forward_declare_proc_macrosandcreffrom this tree (with#[gtest]swapped for#[test], since the public googletest crate lacksdeath_tests). The snippet above now fails to compile with E0271.forward_declare_macros_test.rspasses, including the new case. A hand-writtenCppTypeimpl that puts a thin-pointer type into a slice's class now panics on the new size check instead of reading memory.