@@ -12,6 +12,20 @@ fn cast() {
12
12
assert_eq ! ( z, y % 256 ) ;
13
13
}
14
14
15
+ /// Test usize->ptr cast for dangling and OOB address.
16
+ /// That is safe, and thus has to work.
17
+ fn cast_dangling ( ) {
18
+ let b = Box :: new ( 0 ) ;
19
+ let x = & * b as * const i32 as usize ;
20
+ drop ( b) ;
21
+ let _val = x as * const i32 ;
22
+
23
+ let b = Box :: new ( 0 ) ;
24
+ let mut x = & * b as * const i32 as usize ;
25
+ x += 0x100 ;
26
+ let _val = x as * const i32 ;
27
+ }
28
+
15
29
fn format ( ) {
16
30
// Pointer string formatting! We can't check the output as it changes when libstd changes,
17
31
// but we can make sure Miri does not error.
@@ -47,8 +61,7 @@ fn ptr_eq_dangling() {
47
61
drop ( b) ;
48
62
let b = Box :: new ( 0 ) ;
49
63
let y = & * b as * const i32 ; // different allocation
50
- // We cannot compare these even though both are inbounds -- they *could* be
51
- // equal if memory was reused.
64
+ // They *could* be equal if memory was reused, but probably are not.
52
65
assert ! ( x != y) ;
53
66
}
54
67
@@ -57,27 +70,27 @@ fn ptr_eq_out_of_bounds() {
57
70
let x = ( & * b as * const i32 ) . wrapping_sub ( 0x800 ) ; // out-of-bounds
58
71
let b = Box :: new ( 0 ) ;
59
72
let y = & * b as * const i32 ; // different allocation
60
- // We cannot compare these even though both allocations are live -- they *could* be
61
- // equal (with the right base addresses).
73
+ // They *could* be equal (with the right base addresses), but probably are not.
62
74
assert ! ( x != y) ;
63
75
}
64
76
65
77
fn ptr_eq_out_of_bounds_null ( ) {
66
78
let b = Box :: new ( 0 ) ;
67
79
let x = ( & * b as * const i32 ) . wrapping_sub ( 0x800 ) ; // out-of-bounds
68
- // We cannot compare this with NULL. After all, this *could* be NULL (with the right base address).
80
+ // This *could* be NULL (with the right base address), but probably is not .
69
81
assert ! ( x != std:: ptr:: null( ) ) ;
70
82
}
71
83
72
84
fn ptr_eq_integer ( ) {
73
85
let b = Box :: new ( 0 ) ;
74
86
let x = & * b as * const i32 ;
75
- // We cannot compare this with a non-NULL integer. After all, these *could* be equal (with the right base address).
87
+ // These *could* be equal (with the right base address), but probably are not .
76
88
assert ! ( x != 64 as * const i32 ) ;
77
89
}
78
90
79
91
fn main ( ) {
80
92
cast ( ) ;
93
+ cast_dangling ( ) ;
81
94
format ( ) ;
82
95
transmute ( ) ;
83
96
ptr_bitops1 ( ) ;
0 commit comments