Skip to content

Commit 303a5dc

Browse files
Check for valid pin properly (fixes #4605)
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.)
1 parent 244b20b commit 303a5dc

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

Diff for: hardware/arduino/avr/cores/arduino/wiring_digital.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,16 @@
2626
#include "wiring_private.h"
2727
#include "pins_arduino.h"
2828

29+
#define PIN_MAX (sizeof digital_pin_to_port_PGM / sizeof *digital_pin_to_port_PGM)
30+
2931
void pinMode(uint8_t pin, uint8_t mode)
3032
{
31-
uint8_t bit = digitalPinToBitMask(pin);
32-
uint8_t port = digitalPinToPort(pin);
33+
if (pin >= PIN_MAX) return;
34+
35+
uint8_t bit = digitalPinToBitMask(pin); // bit mask, or 0 for invalid pins
36+
uint8_t port = digitalPinToPort(pin); // must be a valid port (even for invalid pins)
3337
volatile uint8_t *reg, *out;
3438

35-
if (port == NOT_A_PIN) return;
36-
3739
// JWS: can I let the optimizer do this?
3840
reg = portModeRegister(port);
3941
out = portOutputRegister(port);
@@ -137,13 +139,13 @@ static void turnOffPWM(uint8_t timer)
137139

138140
void digitalWrite(uint8_t pin, uint8_t val)
139141
{
142+
if (pin >= PIN_MAX) return;
143+
140144
uint8_t timer = digitalPinToTimer(pin);
141145
uint8_t bit = digitalPinToBitMask(pin);
142146
uint8_t port = digitalPinToPort(pin);
143147
volatile uint8_t *out;
144148

145-
if (port == NOT_A_PIN) return;
146-
147149
// If the pin that support PWM output, we need to turn it off
148150
// before doing a digital write.
149151
if (timer != NOT_ON_TIMER) turnOffPWM(timer);
@@ -164,12 +166,12 @@ void digitalWrite(uint8_t pin, uint8_t val)
164166

165167
int digitalRead(uint8_t pin)
166168
{
169+
if (pin >= PIN_MAX) return LOW;
170+
167171
uint8_t timer = digitalPinToTimer(pin);
168172
uint8_t bit = digitalPinToBitMask(pin);
169173
uint8_t port = digitalPinToPort(pin);
170174

171-
if (port == NOT_A_PIN) return LOW;
172-
173175
// If the pin that support PWM output, we need to turn it off
174176
// before getting a digital reading.
175177
if (timer != NOT_ON_TIMER) turnOffPWM(timer);

0 commit comments

Comments
 (0)