From: Lennart Poettering Date: Thu, 15 May 2025 10:23:57 +0000 (+0200) Subject: tree-wide: fix use of flink_tmpfile() X-Git-Tag: v258-rc1~590 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=22b5ddb90f282d6fce3035379d8c8f971519e951;p=thirdparty%2Fsystemd.git tree-wide: fix use of flink_tmpfile() Let's always make sure the temporary file returned by fopen_tmpfile() and friends is removed on failure (via _cleanup_). Conversely, make sure that once the file is renamed into its final place, we no longer try to remove the temporary file (since it's not going to exist anymore). --- diff --git a/src/dissect/dissect.c b/src/dissect/dissect.c index 9db5f642bbc..f7de15e92f2 100644 --- a/src/dissect/dissect.c +++ b/src/dissect/dissect.c @@ -1789,6 +1789,8 @@ static int action_list_or_mtree_or_copy_or_make_archive(DissectedImage *m, LoopD r = flink_tmpfile(f, tar, arg_target, LINK_TMPFILE_REPLACE); if (r < 0) return log_error_errno(r, "Failed to move archive file into place: %m"); + + tar = mfree(tar); } return 0; diff --git a/src/keyutil/keyutil.c b/src/keyutil/keyutil.c index fa2f039d880..cd7837ed89e 100644 --- a/src/keyutil/keyutil.c +++ b/src/keyutil/keyutil.c @@ -8,6 +8,7 @@ #include "build.h" #include "fd-util.h" #include "fileio.h" +#include "fs-util.h" #include "log.h" #include "main-func.h" #include "memstream-util.h" @@ -383,7 +384,7 @@ static int verb_pkcs7(int argc, char *argv[], void *userdata) { ASN1_STRING_set0(signer_info->enc_digest, TAKE_PTR(pkcs1), pkcs1_len); _cleanup_fclose_ FILE *output = NULL; - _cleanup_free_ char *tmp = NULL; + _cleanup_(unlink_and_freep) char *tmp = NULL; r = fopen_tmpfile_linkable(arg_output, O_WRONLY|O_CLOEXEC, &tmp, &output); if (r < 0) return log_error_errno(r, "Failed to open temporary file: %m"); @@ -396,6 +397,8 @@ static int verb_pkcs7(int argc, char *argv[], void *userdata) { if (r < 0) return log_error_errno(r, "Failed to link temporary file to %s: %m", arg_output); + tmp = mfree(tmp); + return 0; } diff --git a/src/repart/repart.c b/src/repart/repart.c index 7cc5af9219d..be4d66101ac 100644 --- a/src/repart/repart.c +++ b/src/repart/repart.c @@ -7556,6 +7556,8 @@ static int context_fstab(Context *context) { if (r < 0) return log_error_errno(r, "Failed to link temporary file to %s: %m", path); + t = mfree(t); + log_info("%s written.", path); return 0; @@ -7633,6 +7635,8 @@ static int context_crypttab(Context *context) { if (r < 0) return log_error_errno(r, "Failed to link temporary file to %s: %m", path); + t = mfree(t); + log_info("%s written.", path); return 0; diff --git a/src/shared/hwdb-util.c b/src/shared/hwdb-util.c index 82c9f02f735..72946b8cb00 100644 --- a/src/shared/hwdb-util.c +++ b/src/shared/hwdb-util.c @@ -409,6 +409,8 @@ static int trie_store(struct trie *trie, const char *filename, bool compat) { if (r < 0) return r; + filename_tmp = mfree(filename_tmp); + /* write succeeded */ log_debug("=== trie on-disk ==="); diff --git a/src/tpm2-setup/tpm2-setup.c b/src/tpm2-setup/tpm2-setup.c index 09f402fc689..5821a23e956 100644 --- a/src/tpm2-setup/tpm2-setup.c +++ b/src/tpm2-setup/tpm2-setup.c @@ -385,6 +385,8 @@ static int run(int argc, char *argv[]) { if (r < 0) return log_error_errno(r, "Failed to move SRK public key file to '%s': %m", tpm2b_public_path); + t = mfree(t); + log_info("SRK public key saved to '%s' in TPM2B_PUBLIC format.", tpm2b_public_path); return 0; }