From: Yu Watanabe Date: Tue, 26 Sep 2023 16:14:58 +0000 (+0900) Subject: sd-journal: make bump_entry_array() return positive when a valid offset found X-Git-Tag: v255-rc1~374^2~5 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b7264911aa91ce1aede9da621cb8aa7eb0f88c2b;p=thirdparty%2Fsystemd.git sd-journal: make bump_entry_array() return positive when a valid offset found 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. --- diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index 31d4db97c0c..22de80bac0f 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -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;