diff options
author | Alan Somers <asomers@gmail.com> | 2022-01-22 14:37:48 -0700 |
---|---|---|
committer | Alan Somers <asomers@gmail.com> | 2022-01-22 14:48:48 -0700 |
commit | 475da53d562293553934d0e579e91f2bf3be30f7 (patch) | |
tree | 251b9ec921e40c1d9a70f08bfdb8e931d3a8b129 /test | |
parent | c84316b7fce588eb9e91b917550a0dfd350dcad1 (diff) | |
download | nix-475da53d562293553934d0e579e91f2bf3be30f7.zip |
Better type safety for mqueue
On some platforms, mqd_t is a pointer. That means code like the below
can trigger a segfault. Fix it by defining a Newtype around mqd_t that
prevents use-after-free and dangling pointer scenarios.
```rust
fn invalid_mqd_t() {
let mqd: libc::mqd_t = std::ptr::null_mut();
mq_close(mqd).unwrap();
}
```
Also, get test coverage for mqueue in CI on FreeBSD.
Diffstat (limited to 'test')
-rw-r--r-- | test/test_mq.rs | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/test/test_mq.rs b/test/test_mq.rs index 088ee7e0..8aff840d 100644 --- a/test/test_mq.rs +++ b/test/test_mq.rs @@ -41,13 +41,13 @@ fn test_mq_send_and_receive() { }; let mqd0 = r0.unwrap(); let msg_to_send = "msg_1"; - mq_send(mqd0, msg_to_send.as_bytes(), 1).unwrap(); + mq_send(&mqd0, msg_to_send.as_bytes(), 1).unwrap(); let oflag1 = MQ_OFlag::O_CREAT | MQ_OFlag::O_RDONLY; let mqd1 = mq_open(mq_name, oflag1, mode, Some(&attr)).unwrap(); let mut buf = [0u8; 32]; let mut prio = 0u32; - let len = mq_receive(mqd1, &mut buf, &mut prio).unwrap(); + let len = mq_receive(&mqd1, &mut buf, &mut prio).unwrap(); assert_eq!(prio, 1); mq_close(mqd1).unwrap(); @@ -71,7 +71,7 @@ fn test_mq_getattr() { }; let mqd = r.unwrap(); - let read_attr = mq_getattr(mqd).unwrap(); + let read_attr = mq_getattr(&mqd).unwrap(); assert_attr_eq!(read_attr, initial_attr); mq_close(mqd).unwrap(); } @@ -98,20 +98,20 @@ fn test_mq_setattr() { let mqd = r.unwrap(); let new_attr = MqAttr::new(0, 20, MSG_SIZE * 2, 100); - let old_attr = mq_setattr(mqd, &new_attr).unwrap(); + let old_attr = mq_setattr(&mqd, &new_attr).unwrap(); assert_attr_eq!(old_attr, initial_attr); // No changes here because according to the Linux man page only // O_NONBLOCK can be set (see tests below) #[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))] { - let new_attr_get = mq_getattr(mqd).unwrap(); + let new_attr_get = mq_getattr(&mqd).unwrap(); assert_ne!(new_attr_get, new_attr); } let new_attr_non_blocking = MqAttr::new(MQ_OFlag::O_NONBLOCK.bits() as mq_attr_member_t, 10, MSG_SIZE, 0); - mq_setattr(mqd, &new_attr_non_blocking).unwrap(); - let new_attr_get = mq_getattr(mqd).unwrap(); + mq_setattr(&mqd, &new_attr_non_blocking).unwrap(); + let new_attr_get = mq_getattr(&mqd).unwrap(); // now the O_NONBLOCK flag has been set #[cfg(not(any(target_os = "dragonfly", target_os = "netbsd")))] @@ -142,12 +142,12 @@ fn test_mq_set_nonblocking() { return; }; let mqd = r.unwrap(); - mq_set_nonblock(mqd).unwrap(); - let new_attr = mq_getattr(mqd); + mq_set_nonblock(&mqd).unwrap(); + let new_attr = mq_getattr(&mqd); let o_nonblock_bits = MQ_OFlag::O_NONBLOCK.bits() as mq_attr_member_t; assert_eq!(new_attr.unwrap().flags() & o_nonblock_bits, o_nonblock_bits); - mq_remove_nonblock(mqd).unwrap(); - let new_attr = mq_getattr(mqd); + mq_remove_nonblock(&mqd).unwrap(); + let new_attr = mq_getattr(&mqd); assert_eq!(new_attr.unwrap().flags() & o_nonblock_bits, 0); mq_close(mqd).unwrap(); } |