]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: fix use of flink_tmpfile()
authorLennart Poettering <lennart@poettering.net>
Thu, 15 May 2025 10:23:57 +0000 (12:23 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Fri, 16 May 2025 06:28:42 +0000 (08:28 +0200)
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).

src/dissect/dissect.c
src/keyutil/keyutil.c
src/repart/repart.c
src/shared/hwdb-util.c
src/tpm2-setup/tpm2-setup.c

index 9db5f642bbc7b97fec6ad6feb381a1ea40299cad..f7de15e92f2d07b5116d54cb220ce8ec2e9f28c3 100644 (file)
@@ -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;
index fa2f039d880bd0f3793e2546d7d8499d3b996b75..cd7837ed89e57cb31b59e7c7a4a55104ea7dac54 100644 (file)
@@ -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;
 }
 
index 7cc5af9219d12a9c773d5014d675905110a404db..be4d66101acf6bcd4044518b2be6363836cd7d2c 100644 (file)
@@ -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;
index 82c9f02f735ee4b1239e3528ef0e63e9ef9d606a..72946b8cb0010da0d58ef23ddddc75360e649c1c 100644 (file)
@@ -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 ===");
index 09f402fc689b45f680d0b5a03724bdee950a1114..5821a23e95666798cf31aab0db603ac5d21ac9fb 100644 (file)
@@ -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;
 }