]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
variuos: fwrite() does not set errno
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 5 Dec 2023 18:02:14 +0000 (19:02 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 5 Dec 2023 18:18:23 +0000 (19:18 +0100)
The man page doesn't even mention errno. It just says that ferror() should
be used to check for errors. Those writes are unlikely to fail, but if they
do, errno might even be 0. Also, we have fflush_and_check() which does
additional paranoia around errno, because we apparently do not trust that
errno will always be set correctly.

src/analyze/analyze-srk.c
src/creds/creds.c
src/libsystemd/sd-journal/journal-verify.c
src/tpm2-setup/tpm2-setup.c

index 31382462254a59a2d97a8a72e4f382105c6472fd..0e24b416bb4efa77dae6550de00d630e64534a53 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "analyze.h"
 #include "analyze-srk.h"
+#include "fileio.h"
 #include "tpm2-util.h"
 
 int verb_srk(int argc, char *argv[], void *userdata) {
@@ -33,12 +34,15 @@ int verb_srk(int argc, char *argv[], void *userdata) {
                 return log_error_errno(r, "Failed to marshal SRK: %m");
 
         if (isatty(STDOUT_FILENO))
-                return log_error_errno(SYNTHETIC_ERRNO(EIO), "Refusing to write binary data to TTY, please redirect output to file.");
+                return log_error_errno(SYNTHETIC_ERRNO(EIO),
+                                       "Refusing to write binary data to TTY, please redirect output to file.");
 
         if (fwrite(marshalled, 1, marshalled_size, stdout) != marshalled_size)
-                return log_error_errno(errno, "Failed to write SRK to stdout: %m");
+                return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write SRK to stdout: %m");
 
-        fflush(stdout);
+        r = fflush_and_check(stdout);
+        if (r < 0)
+                return log_error_errno(r, "Failed to write SRK to stdout: %m");
 
         return EXIT_SUCCESS;
 #else
index 101e5abf9be39e2732780de0389301502a7eb5ab..7a98a5dcd34e1f5fdce2dcec31e6f3b02a90d829 100644 (file)
@@ -350,14 +350,15 @@ static int write_blob(FILE *f, const void *data, size_t size) {
         }
 
         if (fwrite(data, 1, size, f) != size)
-                return log_error_errno(errno, "Failed to write credential data: %m");
+                return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write credential data: %m");
 
         r = print_newline(f, data, size);
         if (r < 0)
                 return r;
 
-        if (fflush(f) != 0)
-                return log_error_errno(errno, "Failed to flush output: %m");
+        r = fflush_and_check(f);
+        if (r < 0)
+                return log_error_errno(r, "Failed to flush output: %m");
 
         return 0;
 }
index 8fc53beb42eb92f16494829e78c833f22d81aeab..bdaa01d66faa5b162d81a36324dae1c22e11950e 100644 (file)
@@ -384,7 +384,7 @@ static int journal_file_object_verify(JournalFile *f, uint64_t offset, Object *o
 
 static int write_uint64(FILE *fp, uint64_t p) {
         if (fwrite(&p, sizeof(p), 1, fp) != 1)
-                return -errno;
+                return -EIO;
 
         return 0;
 }
index be34d166d7ae048d5effed8b305b451f4bf0ca1a..0be7ffc6a5fff66f202b6be0a41b2463577ed7d8 100644 (file)
@@ -284,7 +284,8 @@ static int run(int argc, char *argv[]) {
         if (runtime_key.pkey) {
                 if (memcmp_nn(tpm2_key.fingerprint, tpm2_key.fingerprint_size,
                              runtime_key.fingerprint, runtime_key.fingerprint_size) != 0)
-                        return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "Saved runtime SRK differs from TPM SRK, refusing.");
+                        return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
+                                               "Saved runtime SRK differs from TPM SRK, refusing.");
 
                 if (arg_early) {
                         log_info("SRK saved in '%s' matches SRK in TPM2.", runtime_key.path);
@@ -351,7 +352,8 @@ static int run(int argc, char *argv[]) {
                 return log_error_errno(r, "Failed to marshal TPM2_PUBLIC key.");
 
         if (fwrite(marshalled, 1, marshalled_size, f) != marshalled_size)
-                return log_error_errno(errno, "Failed to write SRK public key file '%s'.", tpm2b_public_path);
+                return log_error_errno(SYNTHETIC_ERRNO(EIO),
+                                       "Failed to write SRK public key file '%s'.", tpm2b_public_path);
 
         if (fchmod(fileno(f), 0444) < 0)
                 return log_error_errno(errno, "Failed to adjust access mode of SRK public key file '%s' to 0444: %m", tpm2b_public_path);