Skip to content
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

Fix BTreeMap UB caused by using pointer identity on a static #63338

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/liballoc/collections/btree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ struct NodeHeader<K, V, K2 = ()> {
/// `parent_idx` into the same 32-bit word, reducing space overhead.
len: u16,

/// Whether this node is an `EMPTY_ROOT_NODE`.
shared_root: bool,

/// See `into_key_slice`.
keys_start: [K2; 0],
}
Expand All @@ -93,6 +96,9 @@ struct LeafNode<K, V> {
/// `parent_idx` into the same 32-bit word, reducing space overhead.
len: u16,

/// Whether this node is an `EMPTY_ROOT_NODE`.
shared_root: bool,

/// The arrays storing the actual data of the node. Only the first `len` elements of each
/// array are initialized and valid.
keys: [MaybeUninit<K>; CAPACITY],
Expand All @@ -110,14 +116,18 @@ impl<K, V> LeafNode<K, V> {
vals: uninit_array![_; CAPACITY],
parent: ptr::null(),
parent_idx: MaybeUninit::uninit(),
len: 0
len: 0,
shared_root: false,
}
}
}

impl<K, V> NodeHeader<K, V> {
fn is_shared_root(&self) -> bool {
ptr::eq(self, &EMPTY_ROOT_NODE as *const _ as *const _)
// This was previously implemented using pointer identity, but `EMPTY_ROOT_NODE`
// can have multiple addresses if multiple copies of `liballoc` are used together,
// i.e. when interoperating with a dynamic library.
self.shared_root
}
}

Expand All @@ -131,6 +141,7 @@ static EMPTY_ROOT_NODE: NodeHeader<(), ()> = NodeHeader {
parent: ptr::null(),
parent_idx: MaybeUninit::uninit(),
len: 0,
shared_root: true,
keys_start: [],
};

Expand Down