Skip to content

fix(mktemp): preserve .. literal prefix in template (#12312)#12363

Open
0xSoftBoi wants to merge 1 commit into
uutils:mainfrom
0xSoftBoi:fix/12312-mktemp-dotdot-prefix
Open

fix(mktemp): preserve .. literal prefix in template (#12312)#12363
0xSoftBoi wants to merge 1 commit into
uutils:mainfrom
0xSoftBoi:fix/12312-mktemp-dotdot-prefix

Conversation

@0xSoftBoi
Copy link
Copy Markdown
Contributor

Summary

mktemp /tmp/..XXXXXX was silently dropping the leading .., producing /tmp/<random> instead of /tmp/..<random>.

# before
$ mktemp /tmp/..XXXXXX
/tmp/fpQ7Y8

# after / GNU mktemp
$ mktemp /tmp/..XXXXXX
/tmp/..cmJm2X

Root cause

The (directory, prefix) split in Params::from relied on Path::file_name() and Path::parent() to break up the joined prefix_path. Path::file_name() returns None for any path whose final component is .. (it is a ParentDir, not a regular name), so the prefix fell back to the empty string while Path::parent() popped the preceding component — together they erased the .. from the output filename.

Fix

Detect a trailing .. component on prefix_from_template and split the directory and prefix from the raw template string before path normalization discards it, mirroring the existing handling for templates ending in MAIN_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>/..XXXXXX template: 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_mktemp is green on macOS (40 passed, 5 linux-only filtered), and cargo fmt --all -- --check and cargo clippy -p uu_mktemp --all-targets -- -D warnings are clean.

Fixes #12312

`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
@github-actions
Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/cut/bounded-memory (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mktemp dot-dot file prefix is silently dropped

1 participant