Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix int8_t overflow in RGB heatmap effect #18410

Merged
merged 1 commit into from
Sep 19, 2022

Conversation

RagingCactus
Copy link
Contributor

@RagingCactus RagingCactus commented Sep 18, 2022

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 as uint8_t, so it is possible to overflow the int8_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 is 255. If anyone does that, the intermediate int16_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

  • Core
  • Bugfix
  • New feature
  • Enhancement/optimization
  • Keyboard (addition or update)
  • Keymap/layout/userspace (addition or update)
  • Documentation

Issues Fixed or Closed by This PR

Checklist

  • My code follows the code style of this project: C, Python
  • I have read the PR Checklist document and have made the appropriate changes.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • I have tested the changes and verified that they work and don't break anything (as well as I can manage).
    • I have tested this change on a RP2040-based keyboard as well as an AVR-based keyboard. It fixes the issue on both of them.

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 as uint8_t, so it is possible to overflow the
int8_t when calculating the row distance and column distance.

Changing the row and column distance to int16_t fixes this overflow and
resolves the original issue.
@github-actions github-actions bot added the core label Sep 18, 2022
@zvecr zvecr added the bug label Sep 19, 2022
Copy link
Member

@daskygit daskygit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, not sure how I missed this.

Ist this a problem that should be addressed? Or is it okay to rely on the keyboards to stay in the recommended range?

I think maybe a little bit of a rework is in order which is probably best submitted to develop. Reading through it again there's potential for a speed up and the user configurable defines should have some hard limits.

@daskygit daskygit requested a review from a team September 19, 2022 10:53
@daskygit daskygit merged commit 652d1d8 into qmk:master Sep 19, 2022
@daskygit
Copy link
Member

Thanks again.

@RagingCactus RagingCactus deleted the fix-rgb-heatmap-overflow branch September 19, 2022 18:35
tacahiroy pushed a commit to tacahiroy/qmk_firmware that referenced this pull request Oct 2, 2022
aivalja pushed a commit to aivalja/qmk_firmware that referenced this pull request Nov 7, 2022
ramonimbao pushed a commit to ramonimbao/qmk_firmware that referenced this pull request Nov 28, 2022
nolanseaton pushed a commit to nolanseaton/qmk_firmware that referenced this pull request Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants