Hash :
cb3565b1
Author :
Date :
2025-03-07T17:59:33
keymap: Fix segfault due to invalid group wrapping
The modular arithmetic is incorrect for negative values, e.g. for
num_groups = 1. It triggers a segfault for the following settings:
- layouts count (per key or total) N: `N > 0`, and
- layout index n: `n = - k * N` (`k > 0`)
% returns the *remainder* of the division, not the modulus (see C11
standard 6.5.5 “Multiplicative operators”: a % b = a - (a/b)*b. While
both operators return the same result for positive operands, they do
not for e.g. a negative dividend: remainder may be negative (in the
open interval ]-num_groups, num_groups[) while the modulus is always
positive. So if the remainder is negative, we must add `num_groups` to
get the modulus.
Fixes: 67b03cea ("state: correctly wrap state->locked_group and ->group")
Signed-off-by: Julian Orth <ju.orth@gmail.com>
Co-authored-by: Julian Orth <ju.orth@gmail.com>
Co-authored-by: Pierre Le Marre <dev@wismill.eu>