Skip to content

mktemp: strip OS error code from permission-denied message - #13815

Open
MsfPablo wants to merge 1 commit into
uutils:mainfrom
MsfPablo:mktemp-strip-os-error-v2
Open

mktemp: strip OS error code from permission-denied message#13815
MsfPablo wants to merge 1 commit into
uutils:mainfrom
MsfPablo:mktemp-strip-os-error-v2

Conversation

@MsfPablo

@MsfPablo MsfPablo commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

What

Creating a temporary file or directory in an unwritable directory leaked the raw Permission denied (os error 13) at path "..." text from the underlying tempfile crate, instead of the clean Permission denied message used elsewhere in uutils:

$ coreutils mktemp -p /tmp/noperm   # chmod 000
mktemp: Permission denied (os error 13) at path "/tmp.haKXBI6xNU"

Why

tempfile wraps the underlying io::Error as io::Error::new(kind, "{<inner display>} at path {path:?}"). That wrapper has raw_os_error() == None, so UIoError::Display skips its kind-match normalization branch and prints self.inner.to_string() verbatim — which still contains the (os error 13) suffix and the internal at path "..." detail.

Routing the error through a dedicated MkTempError::Io(std::io::Error) variant rendered with strip_errno (as @sylvestre suggested in the issue) drops both the (os error N) suffix and the internal path detail, matching how perms.rs (common-write-error) and csplit render io errors:

$ coreutils mktemp -p /tmp/noperm
mktemp: Permission denied
$ coreutils mktemp -d -p /tmp/noperm
mktemp: Permission denied

Changes

  • src/uu/mktemp/src/mktemp.rs: add MkTempError::Io(std::io::Error) rendered with strip_errno, and route the two catch-all Err(e) => Err(e.into()) arms in make_temp_file / make_temp_dir through it.
  • tests/by-util/test_mktemp.rs: add test_permission_denied_clean_message (Unix, skipped under root) asserting the clean message for both the file and directory cases.

Closes #13720.

Verification

  • cargo build --bin coreutils --no-default-features --features mktemp
  • cargo test --no-default-features --features mktemp --test tests -- mktemp → 41 passed
  • cargo fmt --all -- --check → clean
  • cargo clippy --no-default-features --features mktemp → clean

AI disclosure

This change was authored with the assistance of Claude (Anthropic). The diagnosis of the UIoError::Display fall-through, the fix design (mirroring perms.rs / csplit), the test, and the verification were all produced with AI assistance and reviewed by me.

Note on prior claim

I saw that @AnandajithS mentioned working on this in the issue and @Devel08 followed up. No PR appeared after several days, so I went ahead. Happy to close/defer this if a PR from the original claimant is in flight — let me know.

mktemp leaked the raw `Permission denied (os error 13) at path "..."`
text from the underlying tempfile crate when creating a temporary
file/dir in an unwritable directory, instead of the clean
`Permission denied` message used elsewhere in uutils.

Route the io::Error through a new MkTempError::Io variant that renders
with strip_errno, as maintainer sylvestre suggested in uutils#13720, so the
`(os error N)` suffix and the internal `at path ...` detail are
dropped. Add a regression test (skipped under root) that asserts the
clean message for both the file and directory cases.

Closes uutils#13720
@MsfPablo

MsfPablo commented Aug 8, 2026

Copy link
Copy Markdown
Contributor Author

Both failing lint jobs (Style and Lint (unix) and Style/lint (ubuntu-latest, all, true)) report the same error:

error: unnecessary qualification
   --> src/uu/stat/src/stat.rs:1040:47

This is in uu_stat, which this PR does not touch — it's a pre-existing lint already fixed on main in #13814 (commit fb4904b8). This branch was based off just before that fix landed, so it surfaces here. uu_mktemp itself checks clean (the log shows Checking uu_mktemp v0.10.0 passing). Updating the branch against main resolves it; happy to rebase if that's preferred.

@sylvestre

Copy link
Copy Markdown
Contributor

Could you please write the messages yourself? Llm comments are not helpful

/// underlying `tempfile` crate would otherwise embed.
#[cfg(unix)]
#[test]
fn test_permission_denied_clean_message() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn test_permission_denied_clean_message() {
fn test_permission_denied() {

The description should focus on testing the behavior being testing rather than lacking the specific (os error N) suffix (which is obvious).

@codspeed-hq

codspeed-hq Bot commented Aug 8, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 4.91%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 2 regressed benchmarks
✅ 345 untouched benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation du_max_depth_balanced_tree[(6, 4, 10)] 63 ms 66.6 ms -5.47%
Simulation du_summarize_balanced_tree[(5, 4, 10)] 16.3 ms 17 ms -4.35%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing MsfPablo:mktemp-strip-os-error-v2 (dc41de7) with main (a8efa79)

Open in CodSpeed

Footnotes

  1. 46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@Devel08

Devel08 commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Hi, it'd be good if the error message is mktemp: failed to create file via template '/tmp.XXXXXXXXXX': Permission denied (the path has to be path/tmp.XXXXXXXXXX), the error message that your branch gives is just Permission denied, which is worse than what we had in the main branch, that was the issue with using only strip_errno() on it, also the test can be as simple as just doing -p / as the runners don't have permission for it AFAIK :)

@github-actions

github-actions Bot commented Aug 8, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/date/resolution (fails in this run but passes in the 'main' branch)

@xtqqczze

xtqqczze commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Hi, it'd be good if the error message is mktemp: failed to create file via template '/tmp.XXXXXXXXXX': Permission denied

Agreed, I don't think stripping the path is an improvement.

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: permission denied error strip

4 participants