From: Lennart Poettering Date: Mon, 23 Jan 2023 21:46:12 +0000 (+0100) Subject: journal-file: be a tiny bit more careful with generating seqnums X-Git-Tag: v253-rc2~55 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a133189eeee08d269ff975e2923da43b5fe0b5c0;p=thirdparty%2Fsystemd.git journal-file: be a tiny bit more careful with generating seqnums Let's handle overflows in a vaguely reasonable way, i.e. avoid the special values 0 and UINT64_MAX --- diff --git a/src/libsystemd/sd-journal/journal-file.c b/src/libsystemd/sd-journal/journal-file.c index cde02982628..eae8d538fdc 100644 --- a/src/libsystemd/sd-journal/journal-file.c +++ b/src/libsystemd/sd-journal/journal-file.c @@ -991,35 +991,37 @@ int journal_file_read_object_header(JournalFile *f, ObjectType type, uint64_t of return 0; } +static uint64_t inc_seqnum(uint64_t seqnum) { + if (seqnum < UINT64_MAX-1) + return seqnum + 1; + + return 1; /* skip over UINT64_MAX and 0 when we run out of seqnums and start again */ +} + static uint64_t journal_file_entry_seqnum( JournalFile *f, uint64_t *seqnum) { - uint64_t ret; + uint64_t next_seqnum; assert(f); assert(f->header); /* Picks a new sequence number for the entry we are about to add and returns it. */ - ret = le64toh(f->header->tail_entry_seqnum) + 1; + next_seqnum = inc_seqnum(le64toh(f->header->tail_entry_seqnum)); - if (seqnum) { - /* If an external seqnum counter was passed, we update both the local and the external one, - * and set it to the maximum of both */ - - if (*seqnum + 1 > ret) - ret = *seqnum + 1; - - *seqnum = ret; - } + /* If an external seqnum counter was passed, we update both the local and the external one, and set + * it to the maximum of both */ + if (seqnum) + *seqnum = next_seqnum = MAX(inc_seqnum(*seqnum), next_seqnum); - f->header->tail_entry_seqnum = htole64(ret); + f->header->tail_entry_seqnum = htole64(next_seqnum); if (f->header->head_entry_seqnum == 0) - f->header->head_entry_seqnum = htole64(ret); + f->header->head_entry_seqnum = htole64(next_seqnum); - return ret; + return next_seqnum; } int journal_file_append_object(