Skip to content

Commit 1667ded

Browse files
committed
fix fn GetCurrentDirectoryW + clarify return types of Windows shims
1 parent 7e0cc83 commit 1667ded

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

src/shims/env.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
105105
#[allow(non_snake_case)]
106106
fn GetEnvironmentVariableW(
107107
&mut self,
108-
name_op: OpTy<'tcx, Tag>, // LPCWSTR
109-
buf_op: OpTy<'tcx, Tag>, // LPWSTR
110-
size_op: OpTy<'tcx, Tag>, // DWORD
111-
) -> InterpResult<'tcx, u32> {
108+
name_op: OpTy<'tcx, Tag>, // LPCWSTR
109+
buf_op: OpTy<'tcx, Tag>, // LPWSTR
110+
size_op: OpTy<'tcx, Tag>, // DWORD
111+
) -> InterpResult<'tcx, u32> { // Returns DWORD (u32 in Windows)
112112
let this = self.eval_context_mut();
113113
this.assert_target_os("windows", "GetEnvironmentVariableW");
114114

@@ -125,17 +125,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
125125
let buf_ptr = this.read_scalar(buf_op)?.not_undef()?;
126126
// `buf_size` represents the size in characters.
127127
let buf_size = u64::from(this.read_scalar(size_op)?.to_u32()?);
128-
let (success, len) = this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?;
129-
130-
if success {
131-
// If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer,
132-
// not including the terminating null character.
133-
u32::try_from(len).unwrap()
134-
} else {
135-
// If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters,
136-
// required to hold the string and its terminating null character and the contents of lpBuffer are undefined.
137-
u32::try_from(len).unwrap().checked_add(1).unwrap()
138-
}
128+
HowWasBufferSize(this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?)
139129
}
140130
None => {
141131
let envvar_not_found = this.eval_windows("ERROR_ENVVAR_NOT_FOUND")?;
@@ -326,10 +316,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
326316

327317
// If we cannot get the current directory, we return 0
328318
match env::current_dir() {
329-
Ok(cwd) => {
330-
let len = this.write_path_to_wide_str(&cwd, buf, size)?.1;
331-
return Ok(u32::try_from(len).unwrap())
332-
}
319+
Ok(cwd) =>
320+
return Ok(HowWasBufferSize(this.write_path_to_wide_str(&cwd, buf, size)?)),
333321
Err(e) => this.set_last_error_from_io_error(e)?,
334322
}
335323
Ok(0)
@@ -411,3 +399,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
411399
Ok(())
412400
}
413401
}
402+
403+
// Local helper function to be used in Windows shims
404+
#[allow(non_snake_case)]
405+
fn HowWasBufferSize((success, len): (bool, u64)) -> u32 {
406+
if success {
407+
// If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer,
408+
// not including the terminating null character.
409+
u32::try_from(len).unwrap()
410+
} else {
411+
// If lpBuffer is not large enough to hold the data, the return value is the buffer size, in characters,
412+
// required to hold the string and its terminating null character and the contents of lpBuffer are undefined.
413+
u32::try_from(len.checked_add(1).unwrap()).unwrap()
414+
}
415+
}

0 commit comments

Comments
 (0)