Skip to content

Commit

Permalink
Added bounds to checking in digitalPinTo* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
BarryPSmith committed Dec 2, 2019
1 parent c8d6aef commit 8d4c261
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
45 changes: 36 additions & 9 deletions cores/arduino/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,51 @@ extern const uint8_t PROGMEM digital_pin_to_port_PGM[];
// extern const uint8_t PROGMEM digital_pin_to_bit_PGM[];
extern const uint8_t PROGMEM digital_pin_to_bit_mask_PGM[];
extern const uint8_t PROGMEM digital_pin_to_timer_PGM[];
extern const uint8_t digital_pin_count;

#define NOT_A_PIN 0
#define NOT_A_PORT 0

#define NOT_AN_INTERRUPT -1

// Get the bit location within the hardware port of the given virtual pin.
// This comes from the pins_*.c file for the active board configuration.
//
// These perform slightly better as macros compared to inline functions
//
#define digitalPinToPort(P) ( pgm_read_byte( digital_pin_to_port_PGM + (P) ) )
#define digitalPinToBitMask(P) ( pgm_read_byte( digital_pin_to_bit_mask_PGM + (P) ) )
#define digitalPinToTimer(P) ( pgm_read_byte( digital_pin_to_timer_PGM + (P) ) )
//
#define analogInPinToBit(P) (P)
#define portOutputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_output_PGM + (P))) )
#define portInputRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_input_PGM + (P))) )
#define portModeRegister(P) ( (volatile uint8_t *)( pgm_read_word( port_to_mode_PGM + (P))) )

#define NOT_A_PIN 0
#define NOT_A_PORT 0

#define NOT_AN_INTERRUPT -1
//
// bounds checking on these functions because they return a pointer to an address that will be modified.
// An out of bounds write can result in stack corruption and fun to track-down errors.
//
static inline const uint8_t digitalPinToPort(const uint8_t P) {
if (P > digital_pin_count) {
return NOT_A_PIN;
} else {
return pgm_read_byte( digital_pin_to_port_PGM + P );
}
}
static inline const uint8_t digitalPinToBitMask(const uint8_t P) {
if (P > digital_pin_count) {
return NOT_A_PIN;
} else {
return pgm_read_byte( digital_pin_to_bit_mask_PGM + P );
}
}
static inline const uint8_t digitalPinToTimer(const uint8_t P) {
if (P > digital_pin_count) {
return NOT_A_PIN;
} else {
return pgm_read_byte( digital_pin_to_timer_PGM + P );
}
}
// these defines are to maintain backwards compatibility with #ifdef
#define digitalPinToPort digitalPinToPort
#define digitalPinToBitMask digitalPinToBitMask
#define digitalPinToTimer digitalPinToTimer

#ifdef ARDUINO_MAIN
#define PA 1
Expand Down
2 changes: 2 additions & 0 deletions cores/arduino/wiring_digital.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "wiring_private.h"
#include "pins_arduino.h"

const uint8_t digital_pin_count = sizeof(digital_pin_to_port_PGM) / sizeof(digital_pin_to_port_PGM[0]);

void pinMode(uint8_t pin, uint8_t mode)
{
uint8_t bit = digitalPinToBitMask(pin);
Expand Down
2 changes: 2 additions & 0 deletions variants/circuitplay32u4/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ const uint8_t PROGMEM analog_pin_to_channel_PGM[] = {
9 // A11 D12 PD6 ADC9
};

extern const uint8_t digital_pin_count;

#endif /* ARDUINO_MAIN */

// These serial port names are intended to allow libraries and architecture-neutral
Expand Down

0 comments on commit 8d4c261

Please sign in to comment.