Skip to content

Commit

Permalink
Auto merge of #3183 - ChrisDenton:align-stack-buffer, r=JohnTitor
Browse files Browse the repository at this point in the history
Use aligned `cmsghdr` structs `test_cmsg_nxthdr`

Fixes #3181.

I could find no reason for using unaligned structs in this test.
  • Loading branch information
bors committed Apr 3, 2023
2 parents e0d66cd + 9102bfb commit c64bfc5
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions libc-test/test/cmsg.rs
Expand Up @@ -55,34 +55,32 @@ mod t {
// https://github.com/rust-lang/libc/issues/1239
#[cfg(not(target_arch = "sparc64"))]
#[test]
// FIXME: This triggers alignment checks for pointer dereferences:
// https://github.com/rust-lang/libc/issues/3181
#[ignore]
fn test_cmsg_nxthdr() {
use std::ptr;
// Helps to align the buffer on the stack.
#[repr(align(8))]
struct Align8<T>(T);

let mut buffer = [0u8; 256];
const CAPACITY: usize = 512;
let mut buffer = Align8([0_u8; CAPACITY]);
let mut mhdr: msghdr = unsafe { mem::zeroed() };
let pmhdr = &mhdr as *const msghdr;
for start_ofs in 0..64 {
let pcmsghdr = &mut buffer[start_ofs] as *mut u8 as *mut cmsghdr;
let pcmsghdr = buffer.0.as_mut_ptr().cast::<cmsghdr>();
mhdr.msg_control = pcmsghdr as *mut c_void;
mhdr.msg_controllen = (160 - start_ofs) as _;
for cmsg_len in 0..64 {
for next_cmsg_len in 0..32 {
for i in buffer[start_ofs..].iter_mut() {
*i = 0;
}
unsafe {
pcmsghdr.cast::<u8>().write_bytes(0, CAPACITY);
(*pcmsghdr).cmsg_len = cmsg_len;
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr);
let next = cmsg_nxthdr(pmhdr, pcmsghdr);
let libc_next = libc::CMSG_NXTHDR(&mhdr, pcmsghdr);
let next = cmsg_nxthdr(&mhdr, pcmsghdr);
assert_eq!(libc_next, next);

if libc_next != ptr::null_mut() {
(*libc_next).cmsg_len = next_cmsg_len;
let libc_next = libc::CMSG_NXTHDR(pmhdr, pcmsghdr);
let next = cmsg_nxthdr(pmhdr, pcmsghdr);
let libc_next = libc::CMSG_NXTHDR(&mhdr, pcmsghdr);
let next = cmsg_nxthdr(&mhdr, pcmsghdr);
assert_eq!(libc_next, next);
}
}
Expand Down

0 comments on commit c64bfc5

Please sign in to comment.