diff --git a/tests/pass-dep/shims/libc-fs.rs b/tests/pass-dep/shims/libc-fs.rs index ce4c7bf2ab..68e4bee0bc 100644 --- a/tests/pass-dep/shims/libc-fs.rs +++ b/tests/pass-dep/shims/libc-fs.rs @@ -17,9 +17,9 @@ fn main() { test_dup_stdout_stderr(); test_canonicalize_too_long(); test_rename(); - test_ftruncate(libc::ftruncate); + test_ftruncate::(libc::ftruncate); #[cfg(target_os = "linux")] - test_ftruncate(libc::ftruncate64); + test_ftruncate::(libc::ftruncate64); test_readlink(); test_file_open_unix_allow_two_args(); test_file_open_unix_needs_three_args(); @@ -162,7 +162,12 @@ fn test_rename() { remove_file(&path2).unwrap(); } -fn test_ftruncate(ftruncate: unsafe extern "C" fn(fd: libc::c_int, length: i64) -> libc::c_int) { +fn test_ftruncate( + ftruncate: unsafe extern "C" fn(fd: libc::c_int, length: T) -> libc::c_int, +) { + // libc::off_t is i32 in target i686-unknown-linux-gnu + // https://docs.rs/libc/latest/i686-unknown-linux-gnu/libc/type.off_t.html + let bytes = b"hello"; let path = prepare("miri_test_libc_fs_ftruncate.txt"); let mut file = File::create(&path).unwrap(); @@ -174,7 +179,8 @@ fn test_ftruncate(ftruncate: unsafe extern "C" fn(fd: libc::c_int, length: i64) let fd = unsafe { libc::open(c_path.as_ptr(), libc::O_RDWR) }; // Truncate to a bigger size - let mut res = unsafe { ftruncate(fd, 10) }; + let length_10: i64 = 10; + let mut res = unsafe { ftruncate(fd, Length::convert(length_10)) }; assert_eq!(res, 0); assert_eq!(file.metadata().unwrap().len(), 10); @@ -184,7 +190,8 @@ fn test_ftruncate(ftruncate: unsafe extern "C" fn(fd: libc::c_int, length: i64) assert_eq!(file.metadata().unwrap().len(), 10); // Truncate to smaller size - res = unsafe { ftruncate(fd, 2) }; + let length_2: i64 = 2; + res = unsafe { ftruncate(fd, Length::convert(length_2)) }; assert_eq!(res, 0); assert_eq!(file.metadata().unwrap().len(), 2); @@ -251,3 +258,20 @@ fn test_posix_mkstemp() { assert_eq!(e.kind(), std::io::ErrorKind::InvalidInput); } } + +// Trait implemented for test_ftruncate to convert i64 to the provided type +trait Length: Sized { + fn convert(t: i64) -> Self; +} + +impl Length for i32 { + fn convert(t: i64) -> i32 { + t as i32 + } +} + +impl Length for i64 { + fn convert(t: i64) -> i64 { + t + } +}