Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ecbc6f7

Browse filesBrowse files
xbjfkyouknowone
authored andcommitted
Fix mmap aborting with invalid fd in debug mode
1 parent 5ce8604 commit ecbc6f7
Copy full SHA for ecbc6f7

File tree

1 file changed

+9
-9
lines changed
Filter options

1 file changed

+9
-9
lines changed

‎stdlib/src/mmap.rs

Copy file name to clipboardExpand all lines: stdlib/src/mmap.rs
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ mod mmap {
2323
};
2424
use crossbeam_utils::atomic::AtomicCell;
2525
use memmap2::{Advice, Mmap, MmapMut, MmapOptions};
26+
#[cfg(unix)]
27+
use nix::sys::stat::fstat;
2628
use nix::unistd;
2729
use num_traits::Signed;
2830
use std::fs::File;
29-
use std::io::Write;
31+
use std::io::{self, Write};
3032
use std::ops::{Deref, DerefMut};
3133
#[cfg(unix)]
32-
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
34+
use std::os::unix::io::{FromRawFd, RawFd};
3335

3436
fn advice_try_from_i32(vm: &VirtualMachine, i: i32) -> PyResult<Advice> {
3537
Ok(match i {
@@ -299,7 +301,7 @@ mod mmap {
299301
fn py_new(
300302
cls: PyTypeRef,
301303
MmapNewArgs {
302-
fileno: mut fd,
304+
fileno: fd,
303305
length,
304306
flags,
305307
prot,
@@ -348,12 +350,10 @@ mod mmap {
348350
};
349351

350352
if fd != -1 {
351-
let file = unsafe { File::from_raw_fd(fd) };
352-
let metadata = file.metadata().map_err(|err| err.to_pyexception(vm))?;
353-
let file_len: libc::off_t = metadata.len().try_into().expect("file size overflow");
354-
// File::from_raw_fd will consume the fd, so we
355-
// have to get it again.
356-
fd = file.into_raw_fd();
353+
let metadata = fstat(fd)
354+
.map_err(|err| io::Error::from_raw_os_error(err as i32).to_pyexception(vm))?;
355+
let file_len = metadata.st_size;
356+
357357
if map_size == 0 {
358358
if file_len == 0 {
359359
return Err(vm.new_value_error("cannot mmap an empty file".to_owned()));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.