Skip to content

Commit

Permalink
Fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
CraftSpider committed Nov 21, 2024
1 parent 4a6345e commit ea07bef
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
41 changes: 21 additions & 20 deletions src/shims/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let handle = if is_dir && exists {
let fh = &mut this.machine.fds;
let fd = fh.insert_new(DirHandle { path: file_name.into() });
Ok(Handle::File(fd as u32))
Ok(Handle::File(fd))
} else if creation_disposition == open_existing && desired_access == 0 {
// Windows supports handles with no permissions. These allow things such as reading
// metadata, but not file content.
let fh = &mut this.machine.fds;
let fd = fh.insert_new(MetadataHandle { path: file_name.into() });
Ok(Handle::File(fd as u32))
Ok(Handle::File(fd))
} else {
options.open(file_name).map(|file| {
let fh = &mut this.machine.fds;
let fd = fh.insert_new(FileHandle { file, writable: desired_write });
Handle::File(fd as u32)
Handle::File(fd)
})
};

Expand Down Expand Up @@ -313,7 +313,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.invalid_handle("GetFileInformationByHandle")?
};

let Some(desc) = this.machine.fds.get(fd as i32) else {
let Some(desc) = this.machine.fds.get(fd) else {
this.invalid_handle("GetFileInformationByHandle")?
};

Expand All @@ -340,18 +340,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
let accessed = extract_windows_epoch(metadata.accessed())?.unwrap_or((0, 0));
let written = extract_windows_epoch(metadata.modified())?.unwrap_or((0, 0));

this.write_int_fields_named(
&[("dwFileAttributes", attributes as i128)],
&file_information,
)?;
this.write_int_fields_named(&[("dwFileAttributes", attributes.into())], &file_information)?;
write_filetime_field(this, &file_information, "ftCreationTime", created)?;
write_filetime_field(this, &file_information, "ftLastAccessTime", accessed)?;
write_filetime_field(this, &file_information, "ftLastWriteTime", written)?;
this.write_int_fields_named(
&[
("dwVolumeSerialNumber", 0),
("nFileSizeHigh", (size >> 32) as i128),
("nFileSizeLow", size as u32 as i128),
("nFileSizeHigh", (size >> 32).into()),
("nFileSizeLow", (size & 0xFFFFFFFF).into()),
("nNumberOfLinks", 1),
("nFileIndexHigh", 0),
("nFileIndexLow", 0),
Expand Down Expand Up @@ -451,7 +448,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
res.ok().map(|n| u32::try_from(n).unwrap())
}
Handle::File(fd) => {
let Some(desc) = this.machine.fds.get(fd as i32) else {
let Some(desc) = this.machine.fds.get(fd) else {
this.invalid_handle("NtWriteFile")?
};

Expand Down Expand Up @@ -540,7 +537,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
res.ok().map(|n| u32::try_from(n).unwrap())
}
Handle::File(fd) => {
let Some(desc) = this.machine.fds.get(fd as i32) else {
let Some(desc) = this.machine.fds.get(fd) else {
this.invalid_handle("NtReadFile")?
};

Expand Down Expand Up @@ -584,7 +581,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
_ => this.invalid_handle("SetFilePointerEx")?,
};

let Some(desc) = this.machine.fds.get(fd as i32) else {
let Some(desc) = this.machine.fds.get(fd) else {
throw_unsup_format!("`SetFilePointerEx` is only supported on file backed handles");
};

Expand All @@ -604,7 +601,10 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {

match desc.seek(this.machine.communicate(), seek)? {
Ok(n) => {
this.write_scalar(Scalar::from_i64(n as i64), &this.deref_pointer(new_fp)?)?;
this.write_scalar(
Scalar::from_i64(n.try_into().unwrap()),
&this.deref_pointer(new_fp)?,
)?;
interp_ok(this.eval_windows("c", "TRUE"))
}
Err(e) => {
Expand All @@ -619,14 +619,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
fn extract_windows_epoch<'tcx>(
time: io::Result<SystemTime>,
) -> InterpResult<'tcx, Option<(u32, u32)>> {
// seconds in a year * 10 million (nanoseconds/second / 100)
const TIME_TO_EPOCH: u64 = 31_536_000 * 10_000_000;
// (seconds in a year) * (369 years between 1970 and 1601) * 10 million (nanoseconds/second / 100)
const TIME_TO_EPOCH: u64 = 31_556_926 * 369 * 10_000_000;
match time.ok() {
Some(time) => {
let duration = system_time_to_duration(&time)?;
let secs = duration.as_secs() * 10_000_000;
let nanos_hundred = (duration.subsec_nanos() / 100) as u64;
let total = secs + nanos_hundred + TIME_TO_EPOCH;
let secs = duration.as_secs().saturating_mul(10_000_000);
let nanos_hundred: u64 = (duration.subsec_nanos() / 100).into();
let total = secs.saturating_add(nanos_hundred).saturating_add(TIME_TO_EPOCH);
#[allow(clippy::cast_possible_truncation)]
interp_ok(Some((total as u32, (total >> 32) as u32)))
}
None => interp_ok(None),
Expand All @@ -640,7 +641,7 @@ fn write_filetime_field<'tcx>(
(low, high): (u32, u32),
) -> InterpResult<'tcx> {
cx.write_int_fields_named(
&[("dwLowDateTime", low as i128), ("dwHighDateTime", high as i128)],
&[("dwLowDateTime", low.into()), ("dwHighDateTime", high.into())],
&cx.project_field_named(val, name)?,
)
}
11 changes: 6 additions & 5 deletions src/shims/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum Handle {
Null,
Pseudo(PseudoHandle),
Thread(ThreadId),
File(u32),
File(i32),
Invalid,
}

Expand Down Expand Up @@ -84,15 +84,15 @@ impl Handle {
Self::Null => 0,
Self::Pseudo(pseudo_handle) => pseudo_handle.value(),
Self::Thread(thread) => thread.to_u32(),
Self::File(fd) => fd,
#[expect(clippy::cast_sign_loss)]
Self::File(fd) => fd as u32,
Self::Invalid => 0x1FFFFFFF,
}
}

fn packed_disc_size() -> u32 {
// We ensure that INVALID_HANDLE_VALUE (0xFFFFFFFF) decodes to Handle::Invalid
// see https://devblogs.microsoft.com/oldnewthing/20230914-00/?p=108766
#[expect(clippy::arithmetic_side_effects)] // cannot overflow
let variant_count = variant_count::<Self>();

// however, std's ilog2 is floor(log2(x))
Expand Down Expand Up @@ -132,7 +132,8 @@ impl Handle {
Self::NULL_DISCRIMINANT if data == 0 => Some(Self::Null),
Self::PSEUDO_DISCRIMINANT => Some(Self::Pseudo(PseudoHandle::from_value(data)?)),
Self::THREAD_DISCRIMINANT => Some(Self::Thread(ThreadId::new_unchecked(data))),
Self::FILE_DISCRIMINANT => Some(Self::File(data)),
#[expect(clippy::cast_possible_wrap)]
Self::FILE_DISCRIMINANT => Some(Self::File(data as i32)),
Self::INVALID_DISCRIMINANT => Some(Self::Invalid),
_ => None,
}
Expand Down Expand Up @@ -255,7 +256,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
this.eval_windows("c", "TRUE")
}
Handle::File(fd) =>
if let Some(file) = this.machine.fds.get(fd as i32) {
if let Some(file) = this.machine.fds.get(fd) {
let err = file.close(this.machine.communicate(), this)?;
if let Err(e) = err {
this.set_last_error(e)?;
Expand Down

0 comments on commit ea07bef

Please sign in to comment.