Skip to content

Commit b25557d

Browse files
committed
.
1 parent 3ad763a commit b25557d

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/memcpy/memcpy.rs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,26 @@
33

44
use cfg_if::cfg_if;
55

6-
pub mod memcpy_trivial;
7-
6+
/// The type signature of a `memcpy` function
87
pub type Memcpy = unsafe fn(dst: *mut u8, src: *const u8, bytes: usize);
98

10-
pub static ALL_MEMCPYS: &[Memcpy] = &[
9+
/// Assert the arguments of `memcpy` are correct
10+
fn memcpy_assert(dst: *mut u8, src: *const u8, bytes: usize) {
11+
let src_before_dst = (src as usize + bytes) <= dst as usize;
12+
let dst_before_src = (dst as usize + bytes) <= src as usize;
13+
let buffers_do_not_overlap = src_before_dst || dst_before_src;
14+
debug_assert!(buffers_do_not_overlap);
15+
16+
// Buffers larger than isize::max_value are bogus.
17+
// See https://doc.rust-lang.org/std/primitive.pointer.html#method.offset
18+
19+
let size_fits_in_signed_offset = bytes <= isize::max_value() as usize;
20+
debug_assert!(size_fits_in_signed_offset);
21+
}
22+
23+
pub mod memcpy_trivial;
24+
25+
/// Ensure sure all implementations have the same type
26+
static ALL_MEMCPYS: &[Memcpy] = &[
1127
memcpy_trivial::memcpy,
1228
];

src/memcpy/memcpy_trivial.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
pub unsafe fn memcpy(dst: *mut u8, src: *const u8, bytes: usize) {
2+
crate::memcpy_assert(dst, src, bytes);
3+
24
for i in 0..bytes {
35
let dst_byte = dst.add(i);
46
let src_byte = src.add(i);

0 commit comments

Comments
 (0)