Skip to content

Commit 724cca6

Browse files
authored
Rollup merge of #93847 - solid-rs:fix-kmc-solid-fs-ts, r=yaahc
kmc-solid: Use the filesystem thread-safety wrapper Fixes the thread unsafety of the `std::fs` implementation used by the [`*-kmc-solid_*`](https://doc.rust-lang.org/nightly/rustc/platform-support/kmc-solid.html) Tier 3 targets. Neither the SOLID filesystem API nor built-in filesystem drivers guarantee thread safety by default. Although this may suffice in general embedded-system use cases, and in fact the API can be used from multiple threads without any problems in many cases, this has been a source of unsoundness in `std::sys::solid::fs`. This commit updates the implementation to leverage the filesystem thread-safety wrapper (which uses a pluggable synchronization mechanism) to enforce thread safety. This is done by prefixing all paths passed to the filesystem API with `\TS`. (Note that relative paths aren't supported in this platform.)
2 parents 32c8acd + 64406c5 commit 724cca6

File tree

1 file changed

+20
-1
lines changed
  • library/std/src/sys/solid

1 file changed

+20
-1
lines changed

library/std/src/sys/solid/fs.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,26 @@ impl OpenOptions {
289289
}
290290

291291
fn cstr(path: &Path) -> io::Result<CString> {
292-
Ok(CString::new(path.as_os_str().as_bytes())?)
292+
let path = path.as_os_str().as_bytes();
293+
294+
if !path.starts_with(br"\") {
295+
// Relative paths aren't supported
296+
return Err(crate::io::const_io_error!(
297+
crate::io::ErrorKind::Unsupported,
298+
"relative path is not supported on this platform",
299+
));
300+
}
301+
302+
// Apply the thread-safety wrapper
303+
const SAFE_PREFIX: &[u8] = br"\TS";
304+
let wrapped_path = [SAFE_PREFIX, &path, &[0]].concat();
305+
306+
CString::from_vec_with_nul(wrapped_path).map_err(|_| {
307+
crate::io::const_io_error!(
308+
io::ErrorKind::InvalidInput,
309+
"path provided contains a nul byte",
310+
)
311+
})
293312
}
294313

295314
impl File {

0 commit comments

Comments
 (0)