Skip to content

Commit a505bfd

Browse files
committed
Ensure proper heap alignment in tests
1 parent 8efb4b5 commit a505bfd

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/test.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,9 +502,10 @@ fn extend_fragmented_heap() {
502502
/// the hole write would result in an out of bounds write.
503503
#[test]
504504
fn small_heap_extension() {
505-
static mut HEAP: [u8; 33] = [0; 33];
505+
// define an array of `u64` instead of `u8` for alignment
506+
static mut HEAP: [u64; 5] = [0; 5];
506507
let result = unsafe {
507-
let mut heap = Heap::new(HEAP.as_mut_ptr(), 32);
508+
let mut heap = Heap::new(HEAP.as_mut_ptr().cast(), 32);
508509
heap.try_extend(1)
509510
};
510511
assert_eq!(result, Err(ExtendError::SizeTooSmall))
@@ -513,9 +514,10 @@ fn small_heap_extension() {
513514
/// Ensures that `Heap::extend` fails for sizes that are not a multiple of the hole size.
514515
#[test]
515516
fn oddly_sized_heap_extension() {
516-
static mut HEAP: [u8; 33] = [0; 33];
517+
// define an array of `u64` instead of `u8` for alignment
518+
static mut HEAP: [u64; 5] = [0; 5];
517519
let result = unsafe {
518-
let mut heap = Heap::new(HEAP.as_mut_ptr(), 16);
520+
let mut heap = Heap::new(HEAP.as_mut_ptr().cast(), 16);
519521
heap.try_extend(17)
520522
};
521523
assert_eq!(result, Err(ExtendError::OddSize))
@@ -539,9 +541,10 @@ fn extend_empty() {
539541
/// only works if the top pointer is sufficiently aligned.
540542
#[test]
541543
fn extend_odd_size() {
542-
static mut HEAP: [u8; 33] = [0; 33];
544+
// define an array of `u64` instead of `u8` for alignment
545+
static mut HEAP: [u64; 5] = [0; 5];
543546
let result = unsafe {
544-
let mut heap = Heap::new(HEAP.as_mut_ptr(), 17);
547+
let mut heap = Heap::new(HEAP.as_mut_ptr().cast(), 17);
545548
heap.try_extend(16)
546549
};
547550
assert_eq!(result, Err(ExtendError::OddHeapSize))

0 commit comments

Comments
 (0)