Skip to content

Commit 32f9ae8

Browse files
Bekken, Kristian_saxrudWalberg, Irun
authored andcommitted
Inverted larger-than operators for bit_pos>NOT_A_PIN and port>NOT_A_PORT as part of code review of pinMode(). NOT_A_PIN and NOT_A_PORT are defined as 255
1 parent ede4146 commit 32f9ae8

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

cores/arduino/Arduino.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ extern const uint8_t PROGMEM digital_pin_to_interrupt[];
8080

8181
// Get the bit location within the hardware port of the given virtual pin.
8282
// This comes from the pins_*.c file for the active board configuration.
83-
//
83+
//
8484
// These perform slightly better as macros compared to inline functions
8585
//
8686
#define digitalPinToPort(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_port + pin) : NOT_A_PIN )
@@ -90,9 +90,10 @@ extern const uint8_t PROGMEM digital_pin_to_interrupt[];
9090
#define analogPinToBitMask(pin) ( (pin < NUM_ANALOG_INPUTS) ? (1 << analogPinToBitPosition(pin)) : NOT_A_PIN )
9191
#define digitalPinToTimer(pin) ( (pin < NUM_TOTAL_PINS) ? pgm_read_byte(digital_pin_to_timer + pin) : NOT_ON_TIMER )
9292

93-
#define portToPortStruct(port) ((PORT_t *)&PORTA + port)
93+
#define portToPortStruct(port) ( (port < NOT_A_PORT) ? ((PORT_t *)&PORTA + port) : NULL)
9494
#define digitalPinToPortStruct(pin) ( (pin < NUM_TOTAL_PINS) ? ((PORT_t *)&PORTA + digitalPinToPort(pin)) : NULL)
95-
#define getPINnCTRLregister(port, bit_pos) ( (port != NULL) ? ((uint8_t *)&(port->PIN0CTRL) + bit_pos) : NULL )
95+
#define getPINnCTRLregister(port, bit_pos) ( ((port != NULL) && (bit_pos < NOT_A_PIN)) ? ((uint8_t *)&(port->PIN0CTRL) + bit_pos) : NULL )
96+
9697
#define digitalPinToInterrupt(p) ( pgm_read_byte(digital_pin_to_interrupt + p) )
9798
#define portPinToInterrupt(port, bit_pos) ( pgm_read_byte(port_interrupt_offset + port) + bit_pos )
9899

0 commit comments

Comments
 (0)