river: wrap monotonic time > 2^32-1 milliseconds

Fixes: https://codeberg.org/river/river/issues/1176
This commit is contained in:
Isaac Freund 2024-12-30 09:21:24 -06:00
parent ab879e245c
commit 6abcc68a19
No known key found for this signature in database
GPG Key ID: 86DED400DDFD7A11

View File

@ -1091,8 +1091,13 @@ pub fn updateState(cursor: *Cursor) void {
if (!cursor.hidden) { if (!cursor.hidden) {
var now: posix.timespec = undefined; var now: posix.timespec = undefined;
posix.clock_gettime(posix.CLOCK.MONOTONIC, &now) catch @panic("CLOCK_MONOTONIC not supported"); posix.clock_gettime(posix.CLOCK.MONOTONIC, &now) catch @panic("CLOCK_MONOTONIC not supported");
const msec: u32 = @intCast(now.tv_sec * std.time.ms_per_s + // 2^32-1 milliseconds is ~50 days, which is a realistic uptime.
@divTrunc(now.tv_nsec, std.time.ns_per_ms)); // This means that we must wrap if the monotonic time is greater than
// 2^32-1 milliseconds and hope that clients don't get too confused.
const msec: u32 = @intCast(@rem(
now.tv_sec *% std.time.ms_per_s +% @divTrunc(now.tv_nsec, std.time.ns_per_ms),
math.maxInt(u32),
));
cursor.passthrough(msec); cursor.passthrough(msec);
} }
}, },