Skip to content

Commit

Permalink
keymap: fix modular arithmetic
Browse files Browse the repository at this point in the history
Consider num_groups = 1.

Fixes: 67b03ce ("state: correctly wrap state->locked_group and ->group")
  • Loading branch information
mahkoh committed Mar 7, 2025
1 parent b274440 commit 5b54b39
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/keymap-priv.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,17 @@ XkbWrapGroupIntoRange(int32_t group,

case RANGE_WRAP:
default:
static_assert(XKB_MAX_GROUPS < INT32_MAX, "Max groups don't fit");
/*
* C99 says a negative dividend in a modulo operation always
* gives a negative result.
* % returns the remainder of the division, a number in the open interval
* (-num_groups, num_groups). If the number is negative, we must add num_groups
* to get the modulus.
*/
if (group < 0) {
static_assert(XKB_MAX_GROUPS < INT32_MAX, "Max groups don't fit");
return ((int32_t) num_groups + (group % (int32_t) num_groups));
int rem = group % (int32_t) num_groups;
if (rem < 0) {
return rem + (int32_t) num_groups;
} else {
return group % num_groups;
return rem;
}
}
}
Expand Down

0 comments on commit 5b54b39

Please sign in to comment.