ITADN

`TempPath::from_path` stores relative paths verbatim, so cleanup can delete the wrong file after `chdir`

#395Openmeng-xu-cs 创建于 2026-03-10
M
meng-xu-cscommented
`TempPath::from_path` currently stores a relative path verbatim. Later, `Drop` and `close()` delete via that stored path. Because the path is re-resolved against the process current working directory, this can delete a different file than the caller intended: std::env::set_current_dir(&a)?; File::create("victim")?; let tp = tempfile::TempPath::from_path("victim"); std::env::set_current_dir(&b)?; File::create("victim")?; drop(tp); Expected: - `a/victim` is removed. Actual: - `b/victim` is removed. - `a/victim` remains. This seems inconsistent with `util::create_helper`, which already normalizes to absolute paths specifically to avoid cwd-sensitive cleanup, and with the earlier absolute-path fixes in #80 and #323. `TempPath::from_path` appears to have missed the same normalization when it was added in #159. Possible fixes: 1. Normalize relative inputs to absolute paths at construction time. 2. If a fallible conversion is preferred, add `try_from_path(...) -> io::Result<TempPath>` and deprecate/document the current API accordingly. 3. If raw relative-path semantics are needed, expose that under an explicit `*_unchecked` or directory-relative API rather than the default constructor.
0 条评论