fix(mktemp): preserve .. literal prefix in template (#12312)#12363
Open
0xSoftBoi wants to merge 1 commit into
Open
fix(mktemp): preserve .. literal prefix in template (#12312)#123630xSoftBoi wants to merge 1 commit into
.. literal prefix in template (#12312)#123630xSoftBoi wants to merge 1 commit into
Conversation
`Path::file_name()` returns `None` for paths ending in `..`, and `Path::parent()` pops the preceding component, so the existing prefix extraction silently dropped a literal `..` from templates such as `mktemp /tmp/..XXXXXX`, producing `/tmp/<random>` instead of `/tmp/..<random>`. GNU mktemp keeps the literal `..`. Detect a trailing `..` component in `prefix_from_template` and split the directory and prefix from the raw string before Path normalization discards it. Other templates continue through the unchanged code path. Fixes uutils#12312
|
GNU testsuite comparison: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
mktemp /tmp/..XXXXXXwas silently dropping the leading.., producing/tmp/<random>instead of/tmp/..<random>.Root cause
The
(directory, prefix)split inParams::fromrelied onPath::file_name()andPath::parent()to break up the joinedprefix_path.Path::file_name()returnsNonefor any path whose final component is..(it is aParentDir, not a regular name), so the prefix fell back to the empty string whilePath::parent()popped the preceding component — together they erased the..from the output filename.Fix
Detect a trailing
..component onprefix_from_templateand split the directory and prefix from the raw template string before path normalization discards it, mirroring the existing handling for templates ending inMAIN_SEPARATOR. Every other template continues through the unchanged code path. This is intentionally a sibling fix to #12311 (which addresses the lone.case via the same hook).Tests
Added two regression tests in
tests/by-util/test_mktemp.rs:test_mktemp_dotdot_prefix— bare<dir>/..XXXXXXtemplate: asserts the resulting file name starts with.., matches the expected length, lives in<dir>(not<dir>/..), and actually exists on disk.test_mktemp_dotdot_prefix_with_tmpdir_flag— same property with-p <dir> ..XXXXXX.Full
cargo test --test tests test_mktempis green on macOS (40 passed, 5 linux-only filtered), andcargo fmt --all -- --checkandcargo clippy -p uu_mktemp --all-targets -- -D warningsare clean.Fixes #12312