From d0e3955bba84e2d0f72f29251e15e1d662e3b754 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Thu, 20 Sep 2018 01:29:24 -0400 Subject: [PATCH 1/2] avoid silently truncating the offset This should address one of the concerns that came up in https://github.com/danburkert/memmap-rs/pull/65. --- src/unix.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/unix.rs b/src/unix.rs index 4838e7e4..3b10b3a8 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -44,6 +44,13 @@ impl MmapInner { "memory map must have a non-zero length", )); } + let max_offset: libc::off_t = !0; + if offset > max_offset as u64 { + return Err(io::Error::new( + io::ErrorKind::InvalidInput, + "offset overflows libc::off_t", + )); + } unsafe { let ptr = libc::mmap( From cd36978d4d5175f83a7de92d07dd4c6f5f7bc04c Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Mon, 24 Sep 2018 15:37:07 -0400 Subject: [PATCH 2/2] use max_value() instead of !0 This avoids casting negative numbers into an unsigned type. --- src/unix.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/unix.rs b/src/unix.rs index 3b10b3a8..d69af2c3 100644 --- a/src/unix.rs +++ b/src/unix.rs @@ -44,7 +44,7 @@ impl MmapInner { "memory map must have a non-zero length", )); } - let max_offset: libc::off_t = !0; + let max_offset = libc::off_t::max_value(); if offset > max_offset as u64 { return Err(io::Error::new( io::ErrorKind::InvalidInput,