diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index e5593cc4..dfcec6db 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Changed - Documented the MSRV 1.74 +- PathBuf::push: clear the original path whenever the pushed path is absolute ## [v0.1.2](https://github.com/trussed-dev/littlefs2/releases/tag/core-0.1.2) - 2025-10-16 diff --git a/core/src/lib.rs b/core/src/lib.rs index 439e5f03..cd033bc5 100644 --- a/core/src/lib.rs +++ b/core/src/lib.rs @@ -6,6 +6,9 @@ //! //! [`littlefs2`]: https://docs.rs/littlefs2 +#[cfg(test)] +extern crate std; + mod fs; mod io; mod object_safe; diff --git a/core/src/path.rs b/core/src/path.rs index 7eea88fe..8904ce15 100644 --- a/core/src/path.rs +++ b/core/src/path.rs @@ -528,20 +528,14 @@ impl PathBuf { /// Extends `self` with `path` pub fn push(&mut self, path: &Path) { - match path.as_ref() { - // no-operation - "" => return, - - // `self` becomes `/` (root), to match `std::Path` implementation - // NOTE(allow) cast is necessary on some architectures (e.g. x86) - #[allow(clippy::unnecessary_cast)] - "/" => { - self.buf[0] = b'/' as c_char; - self.buf[1] = 0; - self.len = 2; - return; - } - _ => {} + if path.is_empty() { + return; + } + + if path.as_ref().starts_with("/") { + // Following the standard library, if the path being pushed is absolute + // then we make it replace the current path + self.clear(); } let src = path.as_ref().as_bytes(); @@ -900,4 +894,20 @@ mod tests { let path = path!("/some/path/.././file.extension/"); assert_eq!(path.file_name(), None); } + + #[test] + fn matching_std() { + for test_case in ["/", "/other", "eaiu"] { + let mut path_littlefs2 = PathBuf::from_path(path!("/root")); + let mut path_std = std::path::PathBuf::from("/root"); + + path_littlefs2.push(&PathBuf::try_from(test_case).unwrap()); + path_std.push(test_case); + + assert_eq!( + path_littlefs2.as_str(), + path_std.as_path().to_str().unwrap() + ); + } + } }