Fix int8_t overflow in RGB heatmap effect #18410
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
The RGB heatmap effect currently has an issue where pressing the leftmost keys on the keyboard may light up the rightmost keys on the keyboard as well.
This is because the distance calculation for the RGB heatmap effect uses
int8_t
to store the row distance and column distance. However, LED coordinates are stored asuint8_t
, so it is possible to overflow theint8_t
when calculating the row distance and column distance.Changing the row and column distance to
int16_t
fixes this overflow and resolves the issue.QUESTION:
There might still be an issue if any keyboard does not follow the recommendation in https://github.com/qmk/qmk_firmware/blob/ecb4ba70b1d48835e75da6b0a8ad566c2f126b9f/docs/feature_rgb_matrix.md to define LED coordinates in the inclusive range
{ 0..224, 0..64 }
. That document does suggest that the maximum value for both coordinates is255
. If anyone does that, the intermediateint16_t
will overflow again.Ist this a problem that should be addressed? Or is it okay to rely on the keyboards to stay in the recommended range?
I have reproduced the issue on a RP2040-based keyboard (my Lotus58; not in the main QMK repo) as well as an AVR-based keyboard (BM40HSRGB; in the main QMK repo). I have also verified the fix on both of them.
Types of Changes
Issues Fixed or Closed by This PR
Checklist