fix(GFX_Button): initialize currstate/laststate - #513
Open
94xhn wants to merge 1 commit into
Open
Conversation
Adafruit_GFX_Button's constructor only initializes _gfx, leaving currstate/laststate as indeterminate memory. justPressed()/ justReleased() read these before the first press() call, so they can report a spurious press/release transition on a button that was never actually touched. Fixes adafruit#401 Signed-off-by: 94xhn <87560781+94xhn@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #401.
Problem
Adafruit_GFX_Button's constructor only initializes_gfx:currstate/laststate(declared inAdafruit_GFX.hwith no default member initializer) are left as indeterminate memory.justPressed()/justReleased()read them:If either is called before the first
press()call, the result depends on whatever garbage happened to be on the stack/heap at construction time - a button that was never touched can spuriously report a press or release transition.Fix
Initialize both to
falsein the constructor, matching the "not pressed, no prior state" starting condition every other code path already assumes.Test plan
No hardware handy, so I verified the logic with a standalone harness modeling the class's exact fields/methods: before the fix, constructing a
Buttonover deliberately non-zeroed memory and callingjustPressed()before anypress()call can returntrue; after the fix, it correctly returnsfalse.Disclosure
Generative AI (Claude) was used to help investigate this issue, implement, and verify this fix. All changes were reviewed by me before submission.