Skip to content
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

Simple Bounds checking on APIs like pinMode who use the array g_pin_cfg array #95

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cores/arduino/analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ void analogWrite(pin_size_t pinNumber, int value)

if(ptr != nullptr) {
//check the pinmux in case it's been modified by a call to pinMode()
if (pinNumber >= (g_pin_cfg_size / sizeof(g_pin_cfg[0]))) return; /* pinNumber > sizeof of pin table */
bool has_peripheral_mux = R_PFS->PORT[g_pin_cfg[pinNumber].pin >> IOPORT_PRV_PORT_OFFSET].PIN[g_pin_cfg[pinNumber].pin & BSP_IO_PRV_8BIT_MASK].PmnPFS & IOPORT_CFG_PERIPHERAL_PIN;
if (!has_peripheral_mux) {
ptr->end();
Expand Down
3 changes: 3 additions & 0 deletions cores/arduino/digital.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Arduino.h"

void pinMode(pin_size_t pin, const PinMode mode) {
if (pin >= (g_pin_cfg_size / sizeof(g_pin_cfg[0]))) return; /* pinNumber > sizeof of pin table */
switch (mode) {
case INPUT:
case INPUT_PULLDOWN: // TODO: document the INPUT_PULLDOWN is unavailable
Expand All @@ -19,10 +20,12 @@ void pinMode(pin_size_t pin, const PinMode mode) {
}

void digitalWrite(pin_size_t pin, PinStatus val) {
if (pin >= (g_pin_cfg_size / sizeof(g_pin_cfg[0]))) return; /* pinNumber > sizeof of pin table */
R_IOPORT_PinWrite(NULL, g_pin_cfg[pin].pin, val == LOW ? BSP_IO_LEVEL_LOW : BSP_IO_LEVEL_HIGH);
}

PinStatus digitalRead(pin_size_t pin) {
if (pin >= (g_pin_cfg_size / sizeof(g_pin_cfg[0]))) return LOW; /* pinNumber > sizeof of pin table */
bsp_io_level_t ret;
R_IOPORT_PinRead(NULL, g_pin_cfg[pin].pin, &ret);
return (ret == BSP_IO_LEVEL_LOW ? LOW : HIGH);
Expand Down