Skip to content

Commit c64bfc5

Browse files
committed
Auto merge of #3183 - ChrisDenton:align-stack-buffer, r=JohnTitor
Use aligned `cmsghdr` structs `test_cmsg_nxthdr` Fixes #3181. I could find no reason for using unaligned structs in this test.
2 parents e0d66cd + 9102bfb commit c64bfc5

File tree

1 file changed

+11
-13
lines changed

1 file changed

+11
-13
lines changed

libc-test/test/cmsg.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,34 +55,32 @@ mod t {
5555
// https://github.com/rust-lang/libc/issues/1239
5656
#[cfg(not(target_arch = "sparc64"))]
5757
#[test]
58-
// FIXME: This triggers alignment checks for pointer dereferences:
59-
// https://github.com/rust-lang/libc/issues/3181
60-
#[ignore]
6158
fn test_cmsg_nxthdr() {
6259
use std::ptr;
60+
// Helps to align the buffer on the stack.
61+
#[repr(align(8))]
62+
struct Align8<T>(T);
6363

64-
let mut buffer = [0u8; 256];
64+
const CAPACITY: usize = 512;
65+
let mut buffer = Align8([0_u8; CAPACITY]);
6566
let mut mhdr: msghdr = unsafe { mem::zeroed() };
66-
let pmhdr = &mhdr as *const msghdr;
6767
for start_ofs in 0..64 {
68-
let pcmsghdr = &mut buffer[start_ofs] as *mut u8 as *mut cmsghdr;
68+
let pcmsghdr = buffer.0.as_mut_ptr().cast::<cmsghdr>();
6969
mhdr.msg_control = pcmsghdr as *mut c_void;
7070
mhdr.msg_controllen = (160 - start_ofs) as _;
7171
for cmsg_len in 0..64 {
7272
for next_cmsg_len in 0..32 {
73-
for i in buffer[start_ofs..].iter_mut() {
74-
*i = 0;
75-
}
7673
unsafe {
74+
pcmsghdr.cast::<u8>().write_bytes(0, CAPACITY);
7775
(*pcmsghdr).cmsg_len = cmsg_len;
78-
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr);
79-
let next = cmsg_nxthdr(pmhdr, pcmsghdr);
76+
let libc_next = libc::CMSG_NXTHDR(&mhdr, pcmsghdr);
77+
let next = cmsg_nxthdr(&mhdr, pcmsghdr);
8078
assert_eq!(libc_next, next);
8179

8280
if libc_next != ptr::null_mut() {
8381
(*libc_next).cmsg_len = next_cmsg_len;
84-
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr);
85-
let next = cmsg_nxthdr(pmhdr, pcmsghdr);
82+
let libc_next = libc::CMSG_NXTHDR(&mhdr, pcmsghdr);
83+
let next = cmsg_nxthdr(&mhdr, pcmsghdr);
8684
assert_eq!(libc_next, next);
8785
}
8886
}

0 commit comments

Comments
 (0)