-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Check for valid pin properly (fixes #4605) #4649
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
Conversation
Fix for `pinMode()`, `digitalWrite()`, `digitalRead()` (issue #4605). Current behavior: look up pin number in `digital_pin_to_port_PGM[]` and then check if it returned `NOT_A_PIN`. Causes undefined behavior if provided `pin` number is out of the range of `digital_pin_to_port_PGM[]`. Proposed behavior (from issue #4605): check if `pin` is within the valid range of `digital_pin_to_port_PGM[]`, and THEN look it up. Additionally, remove second check for `port` not being `NOT_A_PIN` (which was useful for boards where the pin numbering skips some numbers). This can still be achieved by making `bit = digitalPinToBitMask(pin)` be 0 for invalid pins, which causes further bitwise operations such as `*reg &= ~bit;` and `*out |= bit;` to not actually modify the value of the register. (This removal makes the operation complete a bit faster for valid pins and slower for invalid pins, which I think is a good trade; plus it saves binary size.)
I talked with him on IRC. the patch (after he explained it again) makes totally sense and is essential to be applied. Otherwise a wrong pin can cause very undefined behavior. Also it now makes things simpler. |
It's good to see someone's trying to get this fixed again, after all these years! Hope it can actually happen this time... |
Any idea how to do this for the Due? The sam file looks quite more complicated. The variant.cpp file could definitely use some documentation explaining how to use it... (the fact that it's written using Hungarian notation (incorrectly, by the way) doesn't help) |
this is a excellent fix! |
Does anyone know why this hasn't been merged? |
Closing here; if someone wants to adopt this PR, please reopen it using this link https://github.com/arduino/ArduinoCore-avr/compare/pr_4649?expand=1 , thanks. |
Fix for
pinMode()
,digitalWrite()
,digitalRead()
(issue #4605).Current behavior: look up pin number in
digital_pin_to_port_PGM[]
and then check if it returnedNOT_A_PIN
. Causes undefined behavior if providedpin
number is out of the range ofdigital_pin_to_port_PGM[]
.Proposed behavior (from issue #4605): check if
pin
is within the valid range ofdigital_pin_to_port_PGM[]
, and THEN look it up.Additionally, remove second check for
port
not beingNOT_A_PIN
(which was useful for boards where the pin numbering skips some numbers). This can still be achieved by makingbit = digitalPinToBitMask(pin)
be 0 for invalid pins, which causes further bitwise operations such as*reg &= ~bit;
and*out |= bit;
to not actually modify the value of the register. (This removal makes the operation complete a bit faster for valid pins and slower for invalid pins, which I think is a good trade; plus it saves binary size.)