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.
This commit is contained in:
Yu Watanabe
2023-10-19 18:31:44 +09:00
parent 4820c9d417
commit 86cbbc6d05
15 changed files with 23 additions and 23 deletions
+1 -1
View 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;
+1 -1
View 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);
+1 -1
View 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);
+1 -1
View 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);
+3 -3
View 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);
+1 -1
View 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");
+2 -2
View 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 */
+1 -1
View 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");
}
}
+1 -1
View 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");
}
+2 -2
View 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;
}
+1 -1
View 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");
+4 -4
View 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);
+2 -2
View 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))
+1 -1
View 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)
+1 -1
View 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);