Skip to content

Navigation Menu

Sign in
Appearance settings

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

Fix mmap aborting with invalid fd in debug mode #5727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions 18 stdlib/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ mod mmap {
};
use crossbeam_utils::atomic::AtomicCell;
use memmap2::{Advice, Mmap, MmapMut, MmapOptions};
#[cfg(unix)]
use nix::sys::stat::fstat;
use nix::unistd;
use num_traits::Signed;
use std::fs::File;
use std::io::Write;
use std::io::{self, Write};
use std::ops::{Deref, DerefMut};
#[cfg(unix)]
use std::os::unix::io::{FromRawFd, IntoRawFd, RawFd};
use std::os::unix::io::{FromRawFd, RawFd};

fn advice_try_from_i32(vm: &VirtualMachine, i: i32) -> PyResult<Advice> {
Ok(match i {
Expand Down Expand Up @@ -299,7 +301,7 @@ mod mmap {
fn py_new(
cls: PyTypeRef,
MmapNewArgs {
fileno: mut fd,
fileno: fd,
length,
flags,
prot,
Expand Down Expand Up @@ -348,12 +350,10 @@ mod mmap {
};

if fd != -1 {
let file = unsafe { File::from_raw_fd(fd) };
let metadata = file.metadata().map_err(|err| err.to_pyexception(vm))?;
let file_len: libc::off_t = metadata.len().try_into().expect("file size overflow");
// File::from_raw_fd will consume the fd, so we
// have to get it again.
fd = file.into_raw_fd();
let metadata = fstat(fd)
.map_err(|err| io::Error::from_raw_os_error(err as i32).to_pyexception(vm))?;
let file_len = metadata.st_size;

if map_size == 0 {
if file_len == 0 {
return Err(vm.new_value_error("cannot mmap an empty file".to_owned()));
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.