From c6ffc7fbb956cf1f46d2242930063a9d6dbe1e17 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Wed, 26 Aug 2026 15:17:52 +0200 Subject: [PATCH] Only unlink the temporary file when mkstemp actually created it The cleanup path ran unlink(template) unconditionally. Until mkstemp succeeds, template still holds the literal "/tmp/snapshot_XXXXXX", so any earlier failure -- an unresolvable source directory, or mkstemp itself -- deleted whatever file happened to carry that name: $ echo victim > /tmp/snapshot_XXXXXX $ fsnap create /nonexistent out.fsnap create failed: No such file or directory $ test -f /tmp/snapshot_XXXXXX || echo gone gone Guard it with fd != -1, which is true exactly when mkstemp turned the suffix into a real name. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_018KyCHPAxWzqa3vHKdXJkgx --- src/create.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/create.c b/src/create.c index d63f928..881a143 100644 --- a/src/create.c +++ b/src/create.c @@ -79,7 +79,11 @@ cleanup:; int cleanup_errno = 0; free(resolved); - unlink(template); + /* Only mkstemp turns the XXXXXX suffix into a real name. If it failed, + template still holds the literal and unlink would target whatever file + happens to carry that name. */ + if (fd != -1) + unlink(template); if (tmp != NULL && fclose(tmp) != 0) cleanup_errno = errno;