]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: make bump_entry_array() return positive when a valid offset found
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 26 Sep 2023 16:14:58 +0000 (01:14 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 30 Sep 2023 11:10:15 +0000 (20:10 +0900)
When it returns 0 offset, then the subsequent
journal_file_move_to_object() will fail. Let's return
generic_array_get() earlier in such situation.

src/libsystemd/sd-journal/journal-file.c

index 31d4db97c0c792cf0ecd6c88518de99439d325e9..22de80bac0f5b1b8e2eafc603953d78addc8505a 100644 (file)
@@ -2698,47 +2698,48 @@ static int bump_array_index(uint64_t *i, direction_t direction, uint64_t n) {
 
 static int bump_entry_array(
                 JournalFile *f,
-                Object *o,
-                uint64_t offset,
-                uint64_t first,
+                Object *o,       /* the current entry array object. */
+                uint64_t offset, /* the offset of the entry array object. */
+                uint64_t first,  /* The offset of the first entry array object in the chain. */
                 direction_t direction,
                 uint64_t *ret) {
 
-        uint64_t p, q = 0;
         int r;
 
         assert(f);
-        assert(offset);
         assert(ret);
 
         if (direction == DIRECTION_DOWN) {
                 assert(o);
+                assert(o->object.type == OBJECT_ENTRY_ARRAY);
+
                 *ret = le64toh(o->entry_array.next_entry_array_offset);
-                return 0;
-        }
+        } else {
 
-        /* Entry array chains are a singly linked list, so to find the previous array in the chain, we have
-         * to start iterating from the top. */
+                /* Entry array chains are a singly linked list, so to find the previous array in the chain, we have
+                 * to start iterating from the top. */
 
-        p = first;
+                assert(offset > 0);
 
-        while (p > 0 && p != offset) {
-                r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, p, &o);
-                if (r < 0)
-                        return r;
+                uint64_t p = first, q = 0;
+                while (p > 0 && p != offset) {
+                        r = journal_file_move_to_object(f, OBJECT_ENTRY_ARRAY, p, &o);
+                        if (r < 0)
+                                return r;
 
-                q = p;
-                p = le64toh(o->entry_array.next_entry_array_offset);
-        }
+                        q = p;
+                        p = le64toh(o->entry_array.next_entry_array_offset);
+                }
 
-        /* If we can't find the previous entry array in the entry array chain, we're likely dealing with a
-         * corrupted journal file. */
-        if (p == 0)
-                return -EBADMSG;
+                /* If we can't find the previous entry array in the entry array chain, we're likely dealing with a
+                 * corrupted journal file. */
+                if (p == 0)
+                        return -EBADMSG;
 
-        *ret = q;
+                *ret = q;
+        }
 
-        return 0;
+        return *ret > 0;
 }
 
 static int generic_array_get(
@@ -2781,7 +2782,7 @@ static int generic_array_get(
                          * array and start iterating entries from there. */
 
                         r = bump_entry_array(f, NULL, a, first, DIRECTION_UP, &a);
-                        if (r < 0)
+                        if (r <= 0)
                                 return r;
 
                         i = UINT64_MAX;
@@ -2843,7 +2844,7 @@ static int generic_array_get(
                 } while (bump_array_index(&i, direction, k) > 0);
 
                 r = bump_entry_array(f, o, a, first, direction, &a);
-                if (r < 0)
+                if (r <= 0)
                         return r;
 
                 t += k;