Skip to content

Commit 1b0abc5

Browse files
committed
small refactorings to 'src/shims/os_str.rs' & 'src/shims/env.rs'
1 parent 1667ded commit 1b0abc5

File tree

2 files changed

+62
-60
lines changed

2 files changed

+62
-60
lines changed

src/shims/env.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +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-
HowWasBufferSize(this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?)
128+
windows_check_buffer_size(this.write_os_str_to_wide_str(&var, buf_ptr, buf_size)?)
129129
}
130130
None => {
131131
let envvar_not_found = this.eval_windows("ERROR_ENVVAR_NOT_FOUND")?;
@@ -317,7 +317,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
317317
// If we cannot get the current directory, we return 0
318318
match env::current_dir() {
319319
Ok(cwd) =>
320-
return Ok(HowWasBufferSize(this.write_path_to_wide_str(&cwd, buf, size)?)),
320+
return Ok(windows_check_buffer_size(this.write_path_to_wide_str(&cwd, buf, size)?)),
321321
Err(e) => this.set_last_error_from_io_error(e)?,
322322
}
323323
Ok(0)
@@ -400,16 +400,17 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
400400
}
401401
}
402402

403-
// Local helper function to be used in Windows shims
404-
#[allow(non_snake_case)]
405-
fn HowWasBufferSize((success, len): (bool, u64)) -> u32 {
403+
/// Check whether an operation that writes to a target buffer was successful.
404+
/// Accordingly select return value.
405+
/// Local helper function to be used in Windows shims.
406+
fn windows_check_buffer_size((success, len): (bool, u64)) -> u32 {
406407
if success {
407-
// If the function succeeds, the return value is the number of characters stored in the buffer pointed to by lpBuffer,
408+
// If the function succeeds, the return value is the number of characters stored in the target buffer,
408409
// not including the terminating null character.
409410
u32::try_from(len).unwrap()
410411
} 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.
412+
// If the target buffer was not large enough to hold the data, the return value is the buffer size, in characters,
413+
// required to hold the string and its terminating null character.
413414
u32::try_from(len.checked_add(1).unwrap()).unwrap()
414415
}
415416
}

src/shims/os_str.rs

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,53 @@ use rustc::ty::layout::LayoutOf;
1313

1414
use crate::*;
1515

16+
/// Represent how path separator conversion should be done.
17+
enum Pathconversion {
18+
HostToTarget,
19+
TargetToHost,
20+
}
21+
22+
/// Perform path separator conversion if needed.
23+
fn convert_path_separator<'a>(
24+
os_str: &'a OsStr,
25+
target_os: &str,
26+
direction: Pathconversion,
27+
) -> Cow<'a, OsStr> {
28+
#[cfg(windows)]
29+
return if target_os == "windows" {
30+
// Windows-on-Windows, all fine.
31+
Cow::Borrowed(os_str)
32+
} else {
33+
// Unix target, Windows host.
34+
let (from, to) = match direction {
35+
Pathconversion::HostToTarget => ('\\', '/'),
36+
Pathconversion::TargetToHost => ('/', '\\'),
37+
};
38+
let converted = os_str
39+
.encode_wide()
40+
.map(|wchar| if wchar == from as u16 { to as u16 } else { wchar })
41+
.collect::<Vec<_>>();
42+
Cow::Owned(OsString::from_wide(&converted))
43+
};
44+
#[cfg(unix)]
45+
return if target_os == "windows" {
46+
// Windows target, Unix host.
47+
let (from, to) = match direction {
48+
Pathconversion::HostToTarget => ('/', '\\'),
49+
Pathconversion::TargetToHost => ('\\', '/'),
50+
};
51+
let converted = os_str
52+
.as_bytes()
53+
.iter()
54+
.map(|&wchar| if wchar == from as u8 { to as u8 } else { wchar })
55+
.collect::<Vec<_>>();
56+
Cow::Owned(OsString::from_vec(converted))
57+
} else {
58+
// Unix-on-Unix, all is fine.
59+
Cow::Borrowed(os_str)
60+
};
61+
}
62+
1663
impl<'mir, 'tcx> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
1764
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
1865
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
@@ -177,9 +224,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
177224
'mir: 'a,
178225
{
179226
let this = self.eval_context_ref();
180-
let os_str: &'a OsStr = this.read_os_str_from_c_str(scalar)?;
227+
let os_str = this.read_os_str_from_c_str(scalar)?;
181228

182-
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os, PathConversionDirection::TargetToHost) {
229+
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os, Pathconversion::TargetToHost) {
183230
Cow::Borrowed(x) => Cow::Borrowed(Path::new(x)),
184231
Cow::Owned(y) => Cow::Owned(PathBuf::from(y)),
185232
})
@@ -188,9 +235,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
188235
/// Read a null-terminated sequence of `u16`s, and perform path separator conversion if needed.
189236
fn read_path_from_wide_str(&self, scalar: Scalar<Tag>) -> InterpResult<'tcx, PathBuf> {
190237
let this = self.eval_context_ref();
191-
let os_str: OsString = this.read_os_str_from_wide_str(scalar)?;
238+
let os_str = this.read_os_str_from_wide_str(scalar)?;
192239

193-
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os, PathConversionDirection::TargetToHost)))
240+
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os, Pathconversion::TargetToHost)))
194241
}
195242

196243
/// Write a Path to the machine memory (as a null-terminated sequence of bytes),
@@ -202,7 +249,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
202249
size: u64,
203250
) -> InterpResult<'tcx, (bool, u64)> {
204251
let this = self.eval_context_mut();
205-
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, PathConversionDirection::HostToTarget);
252+
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, Pathconversion::HostToTarget);
206253
this.write_os_str_to_c_str(&os_str, scalar, size)
207254
}
208255

@@ -215,53 +262,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
215262
size: u64,
216263
) -> InterpResult<'tcx, (bool, u64)> {
217264
let this = self.eval_context_mut();
218-
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, PathConversionDirection::HostToTarget);
265+
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, Pathconversion::HostToTarget);
219266
this.write_os_str_to_wide_str(&os_str, scalar, size)
220267
}
221268
}
222-
223-
enum PathConversionDirection {
224-
HostToTarget,
225-
TargetToHost,
226-
}
227-
228-
/// Perform path separator conversion if needed.
229-
fn convert_path_separator<'a>(
230-
os_str: &'a OsStr,
231-
target_os: &str,
232-
direction: PathConversionDirection,
233-
) -> Cow<'a, OsStr> {
234-
#[cfg(windows)]
235-
return if target_os == "windows" {
236-
// Windows-on-Windows, all fine.
237-
Cow::Borrowed(os_str)
238-
} else {
239-
// Unix target, Windows host.
240-
let (from, to) = match direction {
241-
PathConversionDirection::HostToTarget => ('\\', '/'),
242-
PathConversionDirection::TargetToHost => ('/', '\\'),
243-
};
244-
let converted = os_str
245-
.encode_wide()
246-
.map(|wchar| if wchar == from as u16 { to as u16 } else { wchar })
247-
.collect::<Vec<_>>();
248-
Cow::Owned(OsString::from_wide(&converted))
249-
};
250-
#[cfg(unix)]
251-
return if target_os == "windows" {
252-
// Windows target, Unix host.
253-
let (from, to) = match direction {
254-
PathConversionDirection::HostToTarget => ('/', '\\'),
255-
PathConversionDirection::TargetToHost => ('\\', '/'),
256-
};
257-
let converted = os_str
258-
.as_bytes()
259-
.iter()
260-
.map(|&wchar| if wchar == from as u8 { to as u8 } else { wchar })
261-
.collect::<Vec<_>>();
262-
Cow::Owned(OsString::from_vec(converted))
263-
} else {
264-
// Unix-on-Unix, all is fine.
265-
Cow::Borrowed(os_str)
266-
};
267-
}

0 commit comments

Comments
 (0)