From: Yu Watanabe Date: Wed, 18 Oct 2023 04:52:47 +0000 (+0900) Subject: tree-wide: check if return value of lseek() and friends is negative X-Git-Tag: v255-rc1~156^2~6 X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fsystemd.git;a=commitdiff_plain;h=86cbbc6d052bc3cc97aa2ece58603a9939f9ee6a tree-wide: check if return value of lseek() and friends is negative 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. --- diff --git a/src/basic/io-util.c b/src/basic/io-util.c index 25831e47dc1..15eca4e3a86 100644 --- a/src/basic/io-util.c +++ b/src/basic/io-util.c @@ -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; diff --git a/src/boot/bootctl-install.c b/src/boot/bootctl-install.c index e6f66deeab0..fb7cda7df56 100644 --- a/src/boot/bootctl-install.c +++ b/src/boot/bootctl-install.c @@ -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); diff --git a/src/core/execute.c b/src/core/execute.c index 46fb8805b3a..13c406aa1e4 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -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); diff --git a/src/core/main.c b/src/core/main.c index cbcf3ddeeae..cfa29f3a586 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -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); diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index 1962512c67b..151c5eb91a5 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -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); diff --git a/src/home/homed-home.c b/src/home/homed-home.c index 206f6480aca..749670f94be 100644 --- a/src/home/homed-home.c +++ b/src/home/homed-home.c @@ -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"); diff --git a/src/import/import-raw.c b/src/import/import-raw.c index feb6ac1bdda..2db3198ba61 100644 --- a/src/import/import-raw.c +++ b/src/import/import-raw.c @@ -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 */ diff --git a/src/import/pull-job.c b/src/import/pull-job.c index d05bf3cd497..bed7e640305 100644 --- a/src/import/pull-job.c +++ b/src/import/pull-job.c @@ -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"); } } diff --git a/src/import/pull-raw.c b/src/import/pull-raw.c index 3befa96a042..e96be4dd7db 100644 --- a/src/import/pull-raw.c +++ b/src/import/pull-raw.c @@ -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"); } diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index 980b98137e6..09194710cb6 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -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; } diff --git a/src/machine/machined-dbus.c b/src/machine/machined-dbus.c index 979eb3cee78..a12eb516cc5 100644 --- a/src/machine/machined-dbus.c +++ b/src/machine/machined-dbus.c @@ -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"); diff --git a/src/partition/repart.c b/src/partition/repart.c index 20c30c1e15c..f5d41eeae1c 100644 --- a/src/partition/repart.c +++ b/src/partition/repart.c @@ -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); diff --git a/src/shared/elf-util.c b/src/shared/elf-util.c index 57a1bb9daa0..5a4d3bbb50b 100644 --- a/src/shared/elf-util.c +++ b/src/shared/elf-util.c @@ -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)) diff --git a/src/shared/machine-id-setup.c b/src/shared/machine-id-setup.c index 7263b6a204e..d6aa667adac 100644 --- a/src/shared/machine-id-setup.c +++ b/src/shared/machine-id-setup.c @@ -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) diff --git a/src/shared/tpm2-util.c b/src/shared/tpm2-util.c index bb7928bbd70..46e5ec57e56 100644 --- a/src/shared/tpm2-util.c +++ b/src/shared/tpm2-util.c @@ -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);