1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

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
This commit is contained in:
Lennart Poettering 2023-01-23 22:46:12 +01:00
parent 8f8d7dff54
commit a133189eee

View 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 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);
if (*seqnum + 1 > ret)
ret = *seqnum + 1;
*seqnum = ret;
}
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(