From 6abcc68a198d0246077aaaed8af08db224f16775 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Mon, 30 Dec 2024 09:21:24 -0600 Subject: [PATCH] river: wrap monotonic time > 2^32-1 milliseconds Fixes: https://codeberg.org/river/river/issues/1176 --- river/Cursor.zig | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/river/Cursor.zig b/river/Cursor.zig index 235f29c..bb6334f 100644 --- a/river/Cursor.zig +++ b/river/Cursor.zig @@ -1091,8 +1091,13 @@ pub fn updateState(cursor: *Cursor) void { if (!cursor.hidden) { var now: posix.timespec = undefined; 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 + - @divTrunc(now.tv_nsec, std.time.ns_per_ms)); + // 2^32-1 milliseconds is ~50 days, which is a realistic uptime. + // 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); } },