Skip to content

Commit 1f3537a

Browse files
committed
add a test for our behavior around isize::MAX
1 parent e60bf54 commit 1f3537a

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/lib.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl MmapOptions {
130130
// - https://github.com/rust-lang/rust/blob/1.29.0/src/liballoc/raw_vec.rs#L735-L742
131131
if len > isize::MAX as u64 {
132132
return Err(Error::new(
133-
ErrorKind::InvalidData,
133+
ErrorKind::InvalidInput,
134134
"memory map length overflows isize",
135135
));
136136
}
@@ -641,7 +641,9 @@ mod test {
641641
#[cfg(windows)]
642642
extern crate winapi;
643643

644+
use std::fs;
644645
use std::fs::OpenOptions;
646+
use std::io;
645647
use std::io::{Read, Write};
646648
#[cfg(windows)]
647649
use std::os::windows::fs::OpenOptionsExt;
@@ -1012,4 +1014,38 @@ mod test {
10121014
let mmap = mmap.make_exec().expect("make_exec");
10131015
drop(mmap);
10141016
}
1017+
1018+
#[test]
1019+
fn isize_max() {
1020+
let tempdir = tempdir::TempDir::new("mmap").unwrap();
1021+
let path = tempdir.path().join("mmap");
1022+
1023+
let content = b"some bytes";
1024+
fs::write(&path, content).unwrap();
1025+
let file = OpenOptions::new()
1026+
.read(true)
1027+
.write(true)
1028+
.create(true)
1029+
.open(&path)
1030+
.unwrap();
1031+
1032+
// Mapping the file should succeed.
1033+
unsafe { MmapOptions::new().map_mut(&file).unwrap() };
1034+
1035+
// Mapping the file with excess length should succeed.
1036+
unsafe { MmapOptions::new().len(1_000_000).map_mut(&file).unwrap() };
1037+
1038+
// Mapping the file with a length equal to isize::MAX will almost certainly fail on 64-bit
1039+
// systems, but it might succeed on 32-bit. Either way, it shouldn't fail with an
1040+
// InvalidInput error.
1041+
let res = unsafe { MmapOptions::new().len(isize::max_value() as usize).map_mut(&file) };
1042+
if let Err(err) = res {
1043+
assert!(io::ErrorKind::InvalidInput != err.kind());
1044+
}
1045+
1046+
// But mapping the file with a length larger than isize::MAX must fail with InvalidInput,
1047+
// becuase it's UB to create a slice that large.
1048+
let err = unsafe { MmapOptions::new().len(isize::max_value() as usize + 1).map_mut(&file).unwrap_err() };
1049+
assert_eq!(io::ErrorKind::InvalidInput, err.kind());
1050+
}
10151051
}

0 commit comments

Comments
 (0)