Skip to content

Commit 4f3d1aa

Browse files
committed
Future proof validate_len() for 128bit
Instead of assuming `isize` fits in `u64`, `try_from()` into it. Signed-off-by: Thomas Habets <[email protected]>
1 parent 05b2d0e commit 4f3d1aa

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

src/lib.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,13 @@ impl MmapOptions {
250250
// This is not a problem on 64-bit targets, but on 32-bit one
251251
// having a file or an anonymous mapping larger than 2GB is quite normal
252252
// and we have to prevent it.
253-
//
254-
// The code below is essentially the same as in Rust's std:
255-
// https://github.com/rust-lang/rust/blob/db78ab70a88a0a5e89031d7ee4eccec835dcdbde/library/alloc/src/raw_vec.rs#L495
256-
if len > isize::MAX as u64 {
253+
if TryInto::<isize>::try_into(len).is_err() {
257254
return Err(Error::new(
258255
ErrorKind::InvalidData,
259256
"memory map length overflows isize",
260257
));
261258
}
262-
259+
// If an unsigned number (u64) fits in isize, then it fits in usize.
263260
Ok(len as usize)
264261
}
265262

0 commit comments

Comments
 (0)