]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-file: be a tiny bit more careful with generating seqnums
authorLennart Poettering <lennart@poettering.net>
Mon, 23 Jan 2023 21:46:12 +0000 (22:46 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 26 Jan 2023 14:27:16 +0000 (15:27 +0100)
Let's handle overflows in a vaguely reasonable way, i.e. avoid the
special values 0 and UINT64_MAX

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

index cde0298262890bf4a4a0dee35dfdda2ae0d2f650..eae8d538fdc7279ff502e9e6e7f499941229e8ce 100644 (file)
@@ -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(