@@ -122,7 +122,10 @@ impl<T: 'static> Ptr<T> {
122122 pub ( crate ) fn borrow_in < ' a > ( & self , root : & ' a SliceRoot ) -> Ref < ' a , T > {
123123 let offset = self . offset ;
124124 match & self . kind {
125- PtrKind :: StackSingle ( _) | PtrKind :: HeapSingle ( _) => root. alloc :: < T > ( ) . borrow ( ) ,
125+ PtrKind :: StackSingle ( _) | PtrKind :: HeapSingle ( _) => {
126+ check_single ( offset) ;
127+ root. alloc :: < T > ( ) . borrow ( )
128+ }
126129 PtrKind :: StackVec ( _) | PtrKind :: HeapVec ( _) => {
127130 Ref :: map ( root. alloc :: < Vec < T > > ( ) . borrow ( ) , |v| & v[ offset] )
128131 }
@@ -144,7 +147,10 @@ impl<T: 'static> Ptr<T> {
144147 pub ( crate ) fn borrow_in_mut < ' a > ( & self , root : & ' a SliceRoot ) -> RefMut < ' a , T > {
145148 let offset = self . offset ;
146149 match & self . kind {
147- PtrKind :: StackSingle ( _) | PtrKind :: HeapSingle ( _) => root. alloc :: < T > ( ) . borrow_mut ( ) ,
150+ PtrKind :: StackSingle ( _) | PtrKind :: HeapSingle ( _) => {
151+ check_single ( offset) ;
152+ root. alloc :: < T > ( ) . borrow_mut ( )
153+ }
148154 PtrKind :: StackVec ( _) | PtrKind :: HeapVec ( _) => {
149155 RefMut :: map ( root. alloc :: < Vec < T > > ( ) . borrow_mut ( ) , |v| & mut v[ offset] )
150156 }
@@ -595,6 +601,7 @@ impl<T> Ptr<T> {
595601 match & self . kind {
596602 PtrKind :: Null => null_deref ( ) ,
597603 PtrKind :: StackSingle ( weak) | PtrKind :: HeapSingle ( weak) => {
604+ check_single ( self . offset ) ;
598605 StrongPtr :: StackSingle ( weak. upgrade ( ) . unwrap_or_else ( || dangling ( ) ) )
599606 }
600607 PtrKind :: StackVec ( weak) | PtrKind :: HeapVec ( weak) => StrongPtr :: Vec {
@@ -766,6 +773,7 @@ impl<T> Ptr<T> {
766773 {
767774 match & self . kind {
768775 PtrKind :: StackSingle ( weak) | PtrKind :: HeapSingle ( weak) => {
776+ check_single ( offset) ;
769777 let rc = weak. upgrade ( ) . unwrap_or_else ( || dangling ( ) ) ;
770778 let mut borrow = rc. borrow_mut ( ) ;
771779 f ( & mut * borrow)
@@ -887,6 +895,7 @@ impl<T> Ptr<T> {
887895 {
888896 match & self . kind {
889897 PtrKind :: StackSingle ( weak) | PtrKind :: HeapSingle ( weak) => {
898+ check_single ( offset) ;
890899 let rc = weak. upgrade ( ) . unwrap_or_else ( || dangling ( ) ) ;
891900 let borrow = rc. borrow ( ) ;
892901 f ( & * borrow)
@@ -957,6 +966,22 @@ pub(crate) fn dangling() -> ! {
957966 panic ! ( "ub: dangling pointer" )
958967}
959968
969+ // A pointer to a single object can only be dereferenced at offset 0, not
970+ // past the object.
971+ #[ inline( always) ]
972+ #[ track_caller]
973+ fn check_single ( offset : usize ) {
974+ #[ cold]
975+ #[ inline( never) ]
976+ #[ track_caller]
977+ fn out_of_bounds ( offset : usize ) -> ! {
978+ panic ! ( "ub: access at offset {offset} of a single object" )
979+ }
980+ if offset != 0 {
981+ out_of_bounds ( offset)
982+ }
983+ }
984+
960985impl < T : CChar > Ptr < T > {
961986 pub fn with_slice_mut < R > ( & self , len : usize , f : impl FnOnce ( & mut [ T ] ) -> R ) -> R {
962987 let off = self . offset ;
@@ -1537,6 +1562,13 @@ mod tests {
15371562 p. decay ( ) . delete ( ) ;
15381563 }
15391564
1565+ #[ test]
1566+ #[ should_panic( expected = "ub: access at offset 1 of a single object" ) ]
1567+ fn write_past_single_panics ( ) {
1568+ let v: Value < i32 > = Rc :: new ( RefCell :: new ( 1 ) ) ;
1569+ v. as_pointer ( ) . offset ( 1 ) . write ( 2 ) ;
1570+ }
1571+
15401572 #[ test]
15411573 #[ should_panic( expected = "ub: dangling pointer" ) ]
15421574 fn read_dangling_panics ( ) {
0 commit comments