]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal: Correctly advance offset when iterating hash table entries
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 15 Dec 2021 17:18:25 +0000 (18:18 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Wed, 15 Dec 2021 17:21:19 +0000 (18:21 +0100)
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

index 78580ca84d6777ee8bd2a70c0d8ca6feec6ff157..bf0933189cc9ce1c157f16a8644eb151632b0579 100644 (file)
@@ -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;