Skip to content

Commit 17040a7

Browse files
committed
Wrap read and write in helper function
1 parent 08b0206 commit 17040a7

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

tests/pass-dep/libc/libc-eventfd.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,29 @@ fn main() {
1010
test_race();
1111
}
1212

13+
fn read_bytes<const N: usize>(fd: i32, buf: &mut [u8; N]) -> i32 {
14+
let res: i32 =
15+
unsafe { libc::read(fd, buf.as_mut_ptr().cast(), N as libc::size_t).try_into().unwrap() };
16+
return res;
17+
}
18+
19+
fn write_bytes<const N: usize>(fd: i32, data: [u8; N]) -> i32 {
20+
let res: i32 =
21+
unsafe { libc::write(fd, data.as_ptr() as *const libc::c_void, N).try_into().unwrap() };
22+
return res;
23+
}
24+
1325
fn test_read_write() {
1426
let flags = libc::EFD_NONBLOCK | libc::EFD_CLOEXEC;
1527
let fd = unsafe { libc::eventfd(0, flags) };
1628
let sized_8_data: [u8; 8] = 1_u64.to_ne_bytes();
1729
// Write 1 to the counter.
18-
let res: i64 = unsafe {
19-
libc::write(fd, sized_8_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
20-
};
30+
let res = write_bytes(fd, sized_8_data);
2131
assert_eq!(res, 8);
2232

2333
// Read 1 from the counter.
2434
let mut buf: [u8; 8] = [0; 8];
25-
let res: i32 = unsafe {
26-
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
27-
};
35+
let res = read_bytes(fd, &mut buf);
2836
// Read returns number of bytes has been read, which is always 8.
2937
assert_eq!(res, 8);
3038
// Check the value of counter read.
@@ -34,51 +42,40 @@ fn test_read_write() {
3442
// After read, the counter is currently 0, read counter 0 should fail with return
3543
// value -1.
3644
let mut buf: [u8; 8] = [0; 8];
37-
let res: i32 = unsafe {
38-
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
39-
};
45+
let res = read_bytes(fd, &mut buf);
4046
assert_eq!(res, -1);
4147

4248
// Write with supplied buffer that > 8 bytes should be allowed.
4349
let sized_9_data: [u8; 9];
4450
if cfg!(target_endian = "big") {
45-
// Adjust the data based on the endianess of host system.
51+
// Adjust the data based on the endianness of host system.
4652
sized_9_data = [0, 0, 0, 0, 0, 0, 0, 1, 0];
4753
} else {
4854
sized_9_data = [1, 0, 0, 0, 0, 0, 0, 0, 0];
4955
}
50-
let res: i64 = unsafe {
51-
libc::write(fd, sized_9_data.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
52-
};
56+
let res = write_bytes(fd, sized_9_data);
5357
assert_eq!(res, 8);
5458

5559
// Read with supplied buffer that < 8 bytes should fail with return
5660
// value -1.
5761
let mut buf: [u8; 7] = [1; 7];
58-
let res: i32 = unsafe {
59-
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
60-
};
62+
let res = read_bytes(fd, &mut buf);
6163
assert_eq!(res, -1);
6264

6365
// Write with supplied buffer that < 8 bytes should fail with return
6466
// value -1.
65-
let res: i64 = unsafe {
66-
libc::write(fd, sized_8_data[0..7].as_ptr() as *const libc::c_void, 7).try_into().unwrap()
67-
};
67+
let size_7_data: [u8; 7] = [1; 7];
68+
let res = write_bytes(fd, size_7_data);
6869
assert_eq!(res, -1);
6970

7071
// Read with supplied buffer > 8 bytes should be allowed.
7172
let mut buf: [u8; 9] = [1; 9];
72-
let res: i32 = unsafe {
73-
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
74-
};
73+
let res = read_bytes(fd, &mut buf);
7574
assert_eq!(res, 8);
7675

7776
// Write u64::MAX should fail.
7877
let u64_max_bytes: [u8; 8] = [255; 8];
79-
let res: i64 = unsafe {
80-
libc::write(fd, u64_max_bytes.as_ptr() as *const libc::c_void, 8).try_into().unwrap()
81-
};
78+
let res = write_bytes(fd, u64_max_bytes);
8279
assert_eq!(res, -1);
8380
}
8481

@@ -88,9 +85,7 @@ fn test_race() {
8885
let fd = unsafe { libc::eventfd(0, flags) };
8986
let thread1 = thread::spawn(move || {
9087
let mut buf: [u8; 8] = [0; 8];
91-
let res: i32 = unsafe {
92-
libc::read(fd, buf.as_mut_ptr().cast(), buf.len() as libc::size_t).try_into().unwrap()
93-
};
88+
let res = read_bytes(fd, &mut buf);
9489
// read returns number of bytes has been read, which is always 8.
9590
assert_eq!(res, 8);
9691
let counter = u64::from_ne_bytes(buf);
@@ -99,8 +94,7 @@ fn test_race() {
9994
});
10095
unsafe { VAL = 1 };
10196
let data: [u8; 8] = 1_u64.to_ne_bytes();
102-
let res: i64 =
103-
unsafe { libc::write(fd, data.as_ptr() as *const libc::c_void, 8).try_into().unwrap() };
97+
let res = write_bytes(fd, data);
10498
// write returns number of bytes written, which is always 8.
10599
assert_eq!(res, 8);
106100
thread::yield_now();

0 commit comments

Comments
 (0)