Skip to content

Commit 1944e32

Browse files
committed
usize::MAX as limit for memory mapping
1 parent 6756921 commit 1944e32

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

epserde/src/deser/mod.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -275,9 +275,12 @@ pub trait Deserialize: DeserInner {
275275
/// See the [trait documentation](Deserialize).
276276
#[cfg(feature = "std")]
277277
unsafe fn load_mem(path: impl AsRef<Path>) -> anyhow::Result<MemCase<Self>> {
278-
let file_len: usize = path.as_ref().metadata()?.len().try_into().map_err(|_| {
279-
anyhow::anyhow!("File too large for the current architecture (longer than usize::MAX)")
280-
})?;
278+
let file_len = path.as_ref().metadata()?.len();
279+
anyhow::ensure!(
280+
file_len <= isize::MAX as u64,
281+
"File too large for the current architecture (longer than isize::MAX)"
282+
);
283+
let file_len = file_len as usize;
281284
let file = std::fs::File::open(path)?;
282285
unsafe { Self::read_mem(file, file_len) }
283286
}
@@ -368,9 +371,12 @@ pub trait Deserialize: DeserInner {
368371
/// documentation](mmap_rs::MmapOptions::with_file).
369372
#[cfg(all(feature = "mmap", feature = "std"))]
370373
unsafe fn load_mmap(path: impl AsRef<Path>, flags: Flags) -> anyhow::Result<MemCase<Self>> {
371-
let file_len: usize = path.as_ref().metadata()?.len().try_into().map_err(|_| {
372-
anyhow::anyhow!("File too large for the current architecture (longer than usize::MAX)")
373-
})?;
374+
let file_len = path.as_ref().metadata()?.len();
375+
anyhow::ensure!(
376+
file_len <= isize::MAX as u64,
377+
"File too large for the current architecture (longer than isize::MAX)"
378+
);
379+
let file_len = file_len as usize;
374380
let file = std::fs::File::open(path)?;
375381
unsafe { Self::read_mmap(file, file_len, flags) }
376382
}
@@ -389,9 +395,12 @@ pub trait Deserialize: DeserInner {
389395
/// See the [trait documentation](Deserialize) and [mmap's `with_file`'s documentation](mmap_rs::MmapOptions::with_file).
390396
#[cfg(all(feature = "mmap", feature = "std"))]
391397
unsafe fn mmap(path: impl AsRef<Path>, flags: Flags) -> anyhow::Result<MemCase<Self>> {
392-
let file_len: usize = path.as_ref().metadata()?.len().try_into().map_err(|_| {
393-
anyhow::anyhow!("File too large for the current architecture (longer than usize::MAX)")
394-
})?;
398+
let file_len = path.as_ref().metadata()?.len();
399+
anyhow::ensure!(
400+
file_len <= isize::MAX as u64,
401+
"File too large for the current architecture (longer than isize::MAX)"
402+
);
403+
let file_len = file_len as usize;
395404
let file = std::fs::File::open(path)?;
396405

397406
let mut uninit: MaybeUninit<MemCase<Self>> = MaybeUninit::uninit();

0 commit comments

Comments
 (0)