You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In liballoc/collections/btree/node.rs the constant EMPTY_ROOT_NODE is defined as a LeafNode<(), ()>. A comment explains that while the key/value can be set to () and (), this constant can be used in any BTreeMap of any key/value type. The reason being that this is only used as a placeholder in an empty tree, and the rest of the source ensures that neither the keys nor vals field are ever accessed.
However, LeafNode::as_leaf casts this pointer to a &LeafNode<K, V>. As promised, the invalid fields are never accessed, but my understanding is that the mere existence of the returned reference constitutes UB because we have an &-pointer to an invalid object.
I don't believe the current compiler will exploit this in any way to actually produce unexpected behaviour (and I can't really think of how it would), but the problem can be seen in MIRI with the following program:
use std::collections::BTreeMap;fnmain(){letmut map = BTreeMap::<u8,u8>::new();let _ = map.entry(10);}
which produces the following output under cargo miri:
In liballoc/collections/btree/node.rs the constant
EMPTY_ROOT_NODE
is defined as aLeafNode<(), ()>
. A comment explains that while the key/value can be set to()
and()
, this constant can be used in anyBTreeMap
of any key/value type. The reason being that this is only used as a placeholder in an empty tree, and the rest of the source ensures that neither thekeys
norvals
field are ever accessed.However,
LeafNode::as_leaf
casts this pointer to a&LeafNode<K, V>
. As promised, the invalid fields are never accessed, but my understanding is that the mere existence of the returned reference constitutes UB because we have an &-pointer to an invalid object.I don't believe the current compiler will exploit this in any way to actually produce unexpected behaviour (and I can't really think of how it would), but the problem can be seen in MIRI with the following program:
which produces the following output under
cargo miri
:The text was updated successfully, but these errors were encountered: