Skip to content

Commit ea07bef

Browse files
committed
Fix clippy
1 parent 4a6345e commit ea07bef

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

src/shims/windows/fs.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -267,18 +267,18 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
267267
let handle = if is_dir && exists {
268268
let fh = &mut this.machine.fds;
269269
let fd = fh.insert_new(DirHandle { path: file_name.into() });
270-
Ok(Handle::File(fd as u32))
270+
Ok(Handle::File(fd))
271271
} else if creation_disposition == open_existing && desired_access == 0 {
272272
// Windows supports handles with no permissions. These allow things such as reading
273273
// metadata, but not file content.
274274
let fh = &mut this.machine.fds;
275275
let fd = fh.insert_new(MetadataHandle { path: file_name.into() });
276-
Ok(Handle::File(fd as u32))
276+
Ok(Handle::File(fd))
277277
} else {
278278
options.open(file_name).map(|file| {
279279
let fh = &mut this.machine.fds;
280280
let fd = fh.insert_new(FileHandle { file, writable: desired_write });
281-
Handle::File(fd as u32)
281+
Handle::File(fd)
282282
})
283283
};
284284

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

316-
let Some(desc) = this.machine.fds.get(fd as i32) else {
316+
let Some(desc) = this.machine.fds.get(fd) else {
317317
this.invalid_handle("GetFileInformationByHandle")?
318318
};
319319

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

343-
this.write_int_fields_named(
344-
&[("dwFileAttributes", attributes as i128)],
345-
&file_information,
346-
)?;
343+
this.write_int_fields_named(&[("dwFileAttributes", attributes.into())], &file_information)?;
347344
write_filetime_field(this, &file_information, "ftCreationTime", created)?;
348345
write_filetime_field(this, &file_information, "ftLastAccessTime", accessed)?;
349346
write_filetime_field(this, &file_information, "ftLastWriteTime", written)?;
350347
this.write_int_fields_named(
351348
&[
352349
("dwVolumeSerialNumber", 0),
353-
("nFileSizeHigh", (size >> 32) as i128),
354-
("nFileSizeLow", size as u32 as i128),
350+
("nFileSizeHigh", (size >> 32).into()),
351+
("nFileSizeLow", (size & 0xFFFFFFFF).into()),
355352
("nNumberOfLinks", 1),
356353
("nFileIndexHigh", 0),
357354
("nFileIndexLow", 0),
@@ -451,7 +448,7 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
451448
res.ok().map(|n| u32::try_from(n).unwrap())
452449
}
453450
Handle::File(fd) => {
454-
let Some(desc) = this.machine.fds.get(fd as i32) else {
451+
let Some(desc) = this.machine.fds.get(fd) else {
455452
this.invalid_handle("NtWriteFile")?
456453
};
457454

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

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

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

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

605602
match desc.seek(this.machine.communicate(), seek)? {
606603
Ok(n) => {
607-
this.write_scalar(Scalar::from_i64(n as i64), &this.deref_pointer(new_fp)?)?;
604+
this.write_scalar(
605+
Scalar::from_i64(n.try_into().unwrap()),
606+
&this.deref_pointer(new_fp)?,
607+
)?;
608608
interp_ok(this.eval_windows("c", "TRUE"))
609609
}
610610
Err(e) => {
@@ -619,14 +619,15 @@ pub trait EvalContextExt<'tcx>: crate::MiriInterpCxExt<'tcx> {
619619
fn extract_windows_epoch<'tcx>(
620620
time: io::Result<SystemTime>,
621621
) -> InterpResult<'tcx, Option<(u32, u32)>> {
622-
// seconds in a year * 10 million (nanoseconds/second / 100)
623-
const TIME_TO_EPOCH: u64 = 31_536_000 * 10_000_000;
622+
// (seconds in a year) * (369 years between 1970 and 1601) * 10 million (nanoseconds/second / 100)
623+
const TIME_TO_EPOCH: u64 = 31_556_926 * 369 * 10_000_000;
624624
match time.ok() {
625625
Some(time) => {
626626
let duration = system_time_to_duration(&time)?;
627-
let secs = duration.as_secs() * 10_000_000;
628-
let nanos_hundred = (duration.subsec_nanos() / 100) as u64;
629-
let total = secs + nanos_hundred + TIME_TO_EPOCH;
627+
let secs = duration.as_secs().saturating_mul(10_000_000);
628+
let nanos_hundred: u64 = (duration.subsec_nanos() / 100).into();
629+
let total = secs.saturating_add(nanos_hundred).saturating_add(TIME_TO_EPOCH);
630+
#[allow(clippy::cast_possible_truncation)]
630631
interp_ok(Some((total as u32, (total >> 32) as u32)))
631632
}
632633
None => interp_ok(None),
@@ -640,7 +641,7 @@ fn write_filetime_field<'tcx>(
640641
(low, high): (u32, u32),
641642
) -> InterpResult<'tcx> {
642643
cx.write_int_fields_named(
643-
&[("dwLowDateTime", low as i128), ("dwHighDateTime", high as i128)],
644+
&[("dwLowDateTime", low.into()), ("dwHighDateTime", high.into())],
644645
&cx.project_field_named(val, name)?,
645646
)
646647
}

src/shims/windows/handle.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub enum Handle {
2121
Null,
2222
Pseudo(PseudoHandle),
2323
Thread(ThreadId),
24-
File(u32),
24+
File(i32),
2525
Invalid,
2626
}
2727

@@ -84,15 +84,15 @@ impl Handle {
8484
Self::Null => 0,
8585
Self::Pseudo(pseudo_handle) => pseudo_handle.value(),
8686
Self::Thread(thread) => thread.to_u32(),
87-
Self::File(fd) => fd,
87+
#[expect(clippy::cast_sign_loss)]
88+
Self::File(fd) => fd as u32,
8889
Self::Invalid => 0x1FFFFFFF,
8990
}
9091
}
9192

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

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

0 commit comments

Comments
 (0)