Skip to content

Commit e0590b2

Browse files
committed
panic if libc constants have the wrong size
1 parent e78da52 commit e0590b2

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

src/shims/fs.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -356,22 +356,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
356356

357357
let file_type = metadata.file_type();
358358

359+
let mode_name = if file_type.is_file() {
360+
"S_IFREG"
361+
} else if file_type.is_dir() {
362+
"S_IFDIR"
363+
} else {
364+
"S_IFLNK"
365+
};
366+
359367
// The `mode` field specifies the type of the file and the permissions over the file for
360368
// the owner, its group and other users. Given that we can only provide the file type
361369
// without using platform specific methods, we only set the bits corresponding to the file
362370
// type. This should be an `__u16` but `libc` provides its values as `u32`.
363-
let mode = if file_type.is_file() {
364-
this.eval_libc("S_IFREG")?
365-
} else if file_type.is_dir() {
366-
this.eval_libc("S_IFDIR")?
367-
} else {
368-
this.eval_libc("S_IFLNK")?
369-
}.to_u32()?;
370-
371-
let mode: u16 = mode.try_into().map_err(|e|
372-
err_unsup_format!("Failed to convert 32-bit value to u16: {}",
373-
e
374-
))?;
371+
let mode: u16 = this.eval_libc(mode_name)?
372+
.to_u32()?
373+
.try_into()
374+
.unwrap_or_else(|_| bug!("libc contains bad value for `{}` constant", mode_name));
375375

376376
let size = metadata.len();
377377

0 commit comments

Comments
 (0)