]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: check if return value of lseek() and friends is negative
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 18 Oct 2023 04:52:47 +0000 (13:52 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 19 Oct 2023 09:31:44 +0000 (18:31 +0900)
We usually check return value of syscalls or glibc functions by it is
negative or not, something like that `if (stat(path, &st) < 0)`.
Let's also use the same style for lseek() and friends even the type of
their return value is off_t.

Note, fseeko() returns int, instead of off_t.

15 files changed:
src/basic/io-util.c
src/boot/bootctl-install.c
src/core/execute.c
src/core/main.c
src/coredump/coredump.c
src/home/homed-home.c
src/import/import-raw.c
src/import/pull-job.c
src/import/pull-raw.c
src/journal-remote/journal-gatewayd.c
src/machine/machined-dbus.c
src/partition/repart.c
src/shared/elf-util.c
src/shared/machine-id-setup.c
src/shared/tpm2-util.c

index 25831e47dc1fbc4316b5c1b8bd77b1a71d6f3898..15eca4e3a86a5593393d73bb21ee5250738ea978 100644 (file)
@@ -283,7 +283,7 @@ ssize_t sparse_write(int fd, const void *p, size_t sz, size_t run_length) {
                                         return -EIO;
                         }
 
-                        if (lseek(fd, n, SEEK_CUR) == (off_t) -1)
+                        if (lseek(fd, n, SEEK_CUR) < 0)
                                 return -errno;
 
                         q += n;
index e6f66deeab08396f2a725eab08d184033c93359c..fb7cda7df565223a284e27fd57ae8702870d9c07 100644 (file)
@@ -233,7 +233,7 @@ static int copy_file_with_version_check(const char *from, const char *to, bool f
                         if (r < 0)
                                 return r;
 
-                        if (lseek(fd_from, 0, SEEK_SET) == (off_t) -1)
+                        if (lseek(fd_from, 0, SEEK_SET) < 0)
                                 return log_error_errno(errno, "Failed to seek in \"%s\": %m", from);
 
                         fd_to = safe_close(fd_to);
index 46fb8805b3aa856d3a33aca0f15352841f9d88c4..13c406aa1e4c5c4a191742f768c79cbfaf8f1981 100644 (file)
@@ -405,7 +405,7 @@ int exec_spawn(Unit *unit,
         if (r < 0)
                 return log_unit_error_errno(unit, r, "Failed to serialize parameters: %m");
 
-        if (fseeko(f, 0, SEEK_SET) == (off_t) -1)
+        if (fseeko(f, 0, SEEK_SET) < 0)
                 return log_unit_error_errno(unit, errno, "Failed to reseek on serialization stream: %m");
 
         r = fd_cloexec(fileno(f), false);
index cbcf3ddeeae2ef5ef504b185921b0b116bfb92c1..cfa29f3a58655534a91f404e7a5c0133e3039338 100644 (file)
@@ -1108,7 +1108,7 @@ static int prepare_reexecute(
         if (r < 0)
                 return r;
 
-        if (fseeko(f, 0, SEEK_SET) == (off_t) -1)
+        if (fseeko(f, 0, SEEK_SET) < 0)
                 return log_error_errno(errno, "Failed to rewind serialization fd: %m");
 
         r = fd_cloexec(fileno(f), false);
index 1962512c67b4afdb2045eec03f40820a34807624..151c5eb91a55db7dca03fca4ec6700afac925db9 100644 (file)
@@ -531,7 +531,7 @@ static int save_external_coredump(
                 _cleanup_close_ int fd_compressed = -EBADF;
                 uint64_t uncompressed_size = 0;
 
-                if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
+                if (lseek(fd, 0, SEEK_SET) < 0)
                         return log_error_errno(errno, "Failed to seek on coredump %s: %m", fn);
 
                 fn_compressed = strjoin(fn, default_compression_extension());
@@ -595,7 +595,7 @@ static int save_external_coredump(
         if (fstat(fd, &st) < 0)
                 return log_error_errno(errno, "Failed to fstat core file %s: %m", coredump_tmpfile_name(tmp));
 
-        if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
+        if (lseek(fd, 0, SEEK_SET) < 0)
                 return log_error_errno(errno, "Failed to seek on coredump %s: %m", fn);
 
         *ret_filename = TAKE_PTR(fn);
@@ -614,7 +614,7 @@ static int allocate_journal_field(int fd, size_t size, char **ret, size_t *ret_s
         assert(ret);
         assert(ret_size);
 
-        if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
+        if (lseek(fd, 0, SEEK_SET) < 0)
                 return log_warning_errno(errno, "Failed to seek: %m");
 
         field = malloc(9 + size);
index 206f6480acac0a830c9a22a8d9427f1c216c4df3..749670f94bea17b626224b9edcccca1d0fdbab42 100644 (file)
@@ -519,7 +519,7 @@ static int home_parse_worker_stdout(int _fd, UserRecord **ret) {
                 return 0;
         }
 
-        if (lseek(fd, SEEK_SET, 0) == (off_t) -1)
+        if (lseek(fd, SEEK_SET, 0) < 0)
                 return log_error_errno(errno, "Failed to seek to beginning of memfd: %m");
 
         f = take_fdopen(&fd, "r");
index feb6ac1bdda024302868dc09747a8399f72fd07a..2db3198ba6123fa32c7580ba3e3a9ffe6a15ae9f 100644 (file)
@@ -303,7 +303,7 @@ static int raw_import_open_disk(RawImport *i) {
                                        "Target file is not a regular file or block device");
 
         if (i->offset != UINT64_MAX) {
-                if (lseek(i->output_fd, i->offset, SEEK_SET) == (off_t) -1)
+                if (lseek(i->output_fd, i->offset, SEEK_SET) < 0)
                         return log_error_errno(errno, "Failed to seek to offset: %m");
         }
 
@@ -328,7 +328,7 @@ static int raw_import_try_reflink(RawImport *i) {
                 return 0;
 
         p = lseek(i->input_fd, 0, SEEK_CUR);
-        if (p == (off_t) -1)
+        if (p < 0)
                 return log_error_errno(errno, "Failed to read file offset of input file: %m");
 
         /* Let's only try a btrfs reflink, if we are reading from the beginning of the file */
index d05bf3cd4974081cb19ed846b7d4308c9d55c20b..bed7e6403057057c986a16f1a26baf38fb1064f9 100644 (file)
@@ -415,7 +415,7 @@ static int pull_job_open_disk(PullJob *j) {
                         return log_error_errno(errno, "Failed to stat disk file: %m");
 
                 if (j->offset != UINT64_MAX) {
-                        if (lseek(j->disk_fd, j->offset, SEEK_SET) == (off_t) -1)
+                        if (lseek(j->disk_fd, j->offset, SEEK_SET) < 0)
                                 return log_error_errno(errno, "Failed to seek on file descriptor: %m");
                 }
         }
index 3befa96a042cb45d6e0718f1025d1e400fe54a5e..e96be4dd7dbb6de241c51fbb1478b2ddc82b30b2 100644 (file)
@@ -370,7 +370,7 @@ static int raw_pull_make_local_copy(RawPull *i) {
                 assert(i->raw_job->disk_fd >= 0);
                 assert(i->offset == UINT64_MAX);
 
-                if (lseek(i->raw_job->disk_fd, SEEK_SET, 0) == (off_t) -1)
+                if (lseek(i->raw_job->disk_fd, SEEK_SET, 0) < 0)
                         return log_error_errno(errno, "Failed to seek to beginning of vendor image: %m");
         }
 
index 980b98137e6fbbe604bd4a002865f453997c0c95..09194710cb60e9f5d5d5a553e081f30c9b913fb3 100644 (file)
@@ -233,7 +233,7 @@ static ssize_t request_reader_entries(
                 }
 
                 sz = ftello(m->tmp);
-                if (sz == (off_t) -1) {
+                if (sz < 0) {
                         log_error_errno(errno, "Failed to retrieve file position: %m");
                         return MHD_CONTENT_READER_END_WITH_ERROR;
                 }
@@ -582,7 +582,7 @@ static ssize_t request_reader_fields(
                 }
 
                 sz = ftello(m->tmp);
-                if (sz == (off_t) -1) {
+                if (sz < 0) {
                         log_error_errno(errno, "Failed to retrieve file position: %m");
                         return MHD_CONTENT_READER_END_WITH_ERROR;
                 }
index 979eb3cee78685e08252b89d48aba84c12b38264..a12eb516cc5069c74b74752f64beb4464f74f535 100644 (file)
@@ -613,7 +613,7 @@ static int clean_pool_done(Operation *operation, int ret, sd_bus_error *error) {
         assert(operation);
         assert(operation->extra_fd >= 0);
 
-        if (lseek(operation->extra_fd, 0, SEEK_SET) == (off_t) -1)
+        if (lseek(operation->extra_fd, 0, SEEK_SET) < 0)
                 return -errno;
 
         f = take_fdopen(&operation->extra_fd, "r");
index 20c30c1e15c65fe06c1a82df16adde5ba13c5ebc..f5d41eeae1cc6193d4c85baf01a081bf617096af 100644 (file)
@@ -3562,7 +3562,7 @@ static int partition_target_prepare(
         };
 
         if (!need_path) {
-                if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1)
+                if (lseek(whole_fd, p->offset, SEEK_SET) < 0)
                         return log_error_errno(errno, "Failed to seek to partition offset: %m");
 
                 t->whole_fd = whole_fd;
@@ -3638,10 +3638,10 @@ static int partition_target_sync(Context *context, Partition *p, PartitionTarget
         } else if (t->fd >= 0) {
                 struct stat st;
 
-                if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1)
+                if (lseek(whole_fd, p->offset, SEEK_SET) < 0)
                         return log_error_errno(errno, "Failed to seek to partition offset: %m");
 
-                if (lseek(t->fd, 0, SEEK_SET) == (off_t) -1)
+                if (lseek(t->fd, 0, SEEK_SET) < 0)
                         return log_error_errno(errno, "Failed to seek to start of temporary file: %m");
 
                 if (fstat(t->fd, &st) < 0)
@@ -4200,7 +4200,7 @@ static int partition_format_verity_sig(Context *context, Partition *p) {
         if (r < 0)
                 return log_error_errno(r, "Failed to pad string to %s", FORMAT_BYTES(p->new_size));
 
-        if (lseek(whole_fd, p->offset, SEEK_SET) == (off_t) -1)
+        if (lseek(whole_fd, p->offset, SEEK_SET) < 0)
                 return log_error_errno(errno, "Failed to seek to partition %s offset: %m", strna(hint));
 
         r = loop_write(whole_fd, text, p->new_size);
index 57a1bb9daa0564c7237b5ab91d1933433f59ee64..5a4d3bbb50ba46431273d9d6cf5420311e02d3a3 100644 (file)
@@ -588,7 +588,7 @@ static int parse_core(int fd, const char *executable, char **ret, JsonVariant **
 
         assert(fd >= 0);
 
-        if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
+        if (lseek(fd, 0, SEEK_SET) < 0)
                 return log_warning_errno(errno, "Failed to seek to beginning of the core file: %m");
 
         if (ret && !memstream_init(&c.m))
@@ -644,7 +644,7 @@ static int parse_elf(int fd, const char *executable, char **ret, JsonVariant **r
 
         assert(fd >= 0);
 
-        if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
+        if (lseek(fd, 0, SEEK_SET) < 0)
                 return log_warning_errno(errno, "Failed to seek to beginning of the ELF file: %m");
 
         if (ret && !memstream_init(&c.m))
index 7263b6a204e0970131b480b8bb5e8b260b951b59..d6aa667adacaf4e41a5ea8f292dd081344d57ba7 100644 (file)
@@ -151,7 +151,7 @@ int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_
         }
 
         if (writable) {
-                if (lseek(fd, 0, SEEK_SET) == (off_t) -1)
+                if (lseek(fd, 0, SEEK_SET) < 0)
                         return log_error_errno(errno, "Failed to seek %s: %m", etc_machine_id);
 
                 if (ftruncate(fd, 0) < 0)
index bb7928bbd70c8373acd07df2f59463edeb8abfa4..46e5ec57e562da175fa64b525a686ea78bae99cf 100644 (file)
@@ -4586,7 +4586,7 @@ static int tpm2_userspace_log(
         if (r < 0)
                 return log_debug_errno(r, "Failed to format JSON: %m");
 
-        if (lseek(fd, 0, SEEK_END) == (off_t) -1)
+        if (lseek(fd, 0, SEEK_END) < 0)
                 return log_debug_errno(errno, "Failed to seek to end of JSON log: %m");
 
         r = loop_write(fd, f, SIZE_MAX);