Skip to content

Commit 4be2c66

Browse files
committed
Auto merge of rust-lang#3689 - adwinwhite:lseek64, r=RalfJung
Fix ICE caused by seeking past `i64::MAX` Make Miri behave the same as standard library on file seeking offset. Fixes rust-lang#3680.
2 parents be307ca + c741315 commit 4be2c66

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/tools/miri/src/shims/unix/fs.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,14 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
395395
// Isolation check is done via `FileDescriptor` trait.
396396

397397
let seek_from = if whence == this.eval_libc_i32("SEEK_SET") {
398-
SeekFrom::Start(u64::try_from(offset).unwrap())
398+
if offset < 0 {
399+
// Negative offsets return `EINVAL`.
400+
let einval = this.eval_libc("EINVAL");
401+
this.set_last_error(einval)?;
402+
return Ok(Scalar::from_i64(-1));
403+
} else {
404+
SeekFrom::Start(u64::try_from(offset).unwrap())
405+
}
399406
} else if whence == this.eval_libc_i32("SEEK_CUR") {
400407
SeekFrom::Current(i64::try_from(offset).unwrap())
401408
} else if whence == this.eval_libc_i32("SEEK_END") {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//@ignore-target-windows: File handling is not implemented yet
2+
//@compile-flags: -Zmiri-disable-isolation
3+
4+
use std::fs::remove_file;
5+
use std::io::{ErrorKind, Seek};
6+
7+
#[path = "../../utils/mod.rs"]
8+
mod utils;
9+
10+
fn main() {
11+
let path = utils::prepare("miri_test_fs_seek_i64_max_plus_1.txt");
12+
13+
let mut f = std::fs::File::create(&path).unwrap();
14+
let error = f.seek(std::io::SeekFrom::Start(i64::MAX as u64 + 1)).unwrap_err();
15+
16+
// It should be error due to negative offset.
17+
assert_eq!(error.kind(), ErrorKind::InvalidInput);
18+
19+
// Cleanup
20+
remove_file(&path).unwrap();
21+
}

0 commit comments

Comments
 (0)