From cdbba4487811bb14714ba462713298b2bda719c7 Mon Sep 17 00:00:00 2001 From: Daan De Meyer Date: Wed, 15 Dec 2021 18:18:25 +0100 Subject: [PATCH] journal: Correctly advance offset when iterating hash table entries pread() is not guaranteed to completely fill up the given buffer with data which we assumed until now. Instead, only increment the offset by the number of bytes that were actually read. --- src/journal/journald-file.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/journal/journald-file.c b/src/journal/journald-file.c index 78580ca84d..bf0933189c 100644 --- a/src/journal/journald-file.c +++ b/src/journal/journald-file.c @@ -81,6 +81,7 @@ static int journald_file_punch_holes(JournalFile *f) { HashItem items[4096 / sizeof(HashItem)]; uint64_t p, sz; size_t to_read; + ssize_t n; int r; r = journald_file_entry_array_punch_hole( @@ -92,14 +93,12 @@ static int journald_file_punch_holes(JournalFile *f) { sz = le64toh(f->header->data_hash_table_size); to_read = MIN((size_t) f->last_stat.st_blksize, sizeof(items)); - for (uint64_t i = p; i < p + sz; i += sizeof(items)) { - ssize_t n_read; + for (uint64_t i = p; i < p + sz; i += n) { + n = pread(f->fd, items, MIN(to_read, p + sz - i), i); + if (n < 0) + return n; - n_read = pread(f->fd, items, MIN(to_read, p + sz - i), i); - if (n_read < 0) - return n_read; - - for (size_t j = 0; j < (size_t) n_read / sizeof(HashItem); j++) { + for (size_t j = 0; j < (size_t) n / sizeof(HashItem); j++) { Object o; for (uint64_t q = le64toh(items[j].head_hash_offset); q != 0;