Skip to content

Commit 5b2ee71

Browse files
committed
Make not_alloc use max address
1 parent c956b5e commit 5b2ee71

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub use segfault::segfault;
2121
pub use transmute::transmute;
2222
pub use use_after_free::use_after_free;
2323

24-
pub use references::{null, null_mut};
24+
pub use references::{not_alloc, null, null_mut};
2525

2626
/// Construct a [`String`] from a pointer, capacity and length, in a completely safe manner.
2727
///

src/references.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ pub fn null_mut<'a, T: 'static>() -> &'a mut T {
1111
}
1212

1313
/// Not allocate an object. The returned reference is always invalid.
14-
/// This is equivalent to [`crate::null_mut()`].
14+
///
15+
/// **Note:** It turns out that `null` is a valid memory address in WASM.
16+
/// So here we use the maximum address instead.
1517
pub fn not_alloc<'a, T: 'static>() -> &'a mut T {
16-
null_mut()
18+
crate::transmute(usize::MAX)
1719
}

src/segfault.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn segfault() -> ! {
1212

1313
// If null doesn't work, try max. Surely that'll stop it.
1414
// Confirmed to be effective on WASM.
15-
let max = crate::transmute::<usize, &'static mut u8>(usize::MAX);
15+
let max = crate::not_alloc::<u8>();
1616
*max = 69;
1717

1818
unreachable!("Sorry, your platform is too strong.")

0 commit comments

Comments
 (0)