Skip to content

contrib/timer-wheel: fix always-false idx < 0 comparison - #4773

Open
ThalesBarretto wants to merge 1 commit into
gluster:develfrom
ThalesBarretto:fix-timer-wheel-idx-signed-cast
Open

contrib/timer-wheel: fix always-false idx < 0 comparison#4773
ThalesBarretto wants to merge 1 commit into
gluster:develfrom
ThalesBarretto:fix-timer-wheel-idx-signed-cast

Conversation

@ThalesBarretto

Copy link
Copy Markdown
Contributor

Restores the (signed long) cast the pre-4.8 Linux cascading timer-wheel port dropped, so
the past-expiry branch in __gf_tw_add_timer() is live again.

idx is unsigned long, so idx < 0 is always false (-Wtype-limits): a timer whose
expiry is already past / == now (idx wrapped near ULONG_MAX) misses all four size
buckets and falls through to tv5 instead of tv1.

Latent for current callers — every insert path adds base->timer_sec first, so idx >= 0
in practice — so this silences the warning and restores the intended safety net rather than
fixing an observed misfire. One line: idx < 0 -> (long)idx < 0, matching the kernel's
__internal_add_timer().

Fixes: #4770

Draft for discussion.

__gf_tw_add_timer() classifies a timer by idx = expires - timer_sec,
where idx is unsigned long. The branch handling an expiry already in
the past tests "idx < 0", always false for an unsigned value: the
compiler warns (-Wtype-limits) and the branch is dead, so a past/now
expiry is misfiled into tv5 instead of firing promptly from tv1.

The wheel is a port of the pre-4.8 Linux kernel cascading timer wheel,
whose __internal_add_timer() uses "(signed long) idx < 0" to detect the
unsigned wrap. The port dropped the cast; restore it.

No functional change for current callers: every insert path adds
timer_sec first, so idx >= 0 in practice. The fix silences the warning
and restores the past-expiry safety net.

Fixes: gluster#4770
Signed-off-by: Thales Antunes de Oliveira Barretto <thales.barretto.git@gmail.com>
@ThalesBarretto
ThalesBarretto marked this pull request as ready for review September 11, 2026 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

contrib/timer-wheel: always-false idx < 0 disables the past-expiry branch (-Wtype-limits)

2 participants