From c90ea0c6dfc0696fdf4f7fa6aaf3844a32afd432 Mon Sep 17 00:00:00 2001 From: Neil McKechnie Date: Mon, 15 Nov 2021 13:25:11 +0000 Subject: [PATCH] Improve validation of parameters to non-HAL digital calls. When testing CS in minimal HAL mode but with mySetup.h and myAutomation.h files present, I experienced freezing of the arduino because the standard pinMode, digitalWrite etc don't validate the pin number passed to them. So I've added checks on the pin number to the configure, write and read functions in the minimal HAL. --- IODevice.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/IODevice.cpp b/IODevice.cpp index 63a6d8b..b61ecf8 100644 --- a/IODevice.cpp +++ b/IODevice.cpp @@ -305,18 +305,16 @@ bool IODevice::owns(VPIN id) { // Minimal implementations of public HAL interface, to support Arduino pin I/O and nothing more. void IODevice::begin() { DIAG(F("NO HAL CONFIGURED!")); } -bool IODevice::configure(VPIN pin, ConfigTypeEnum, int, int p[]) { +bool IODevice::configure(VPIN pin, ConfigTypeEnum configType, int nParams, int p[]) { + if (configType!=CONFIGURE_INPUT || nParams!=1 || pin >= NUM_DIGITAL_PINS) return false; #ifdef DIAG_IO DIAG(F("Arduino _configurePullup Pin:%d Val:%d"), pin, p[0]); #endif - if (p[0]) { - pinMode(pin, INPUT_PULLUP); - } else { - pinMode(pin, INPUT); - } + pinMode(pin, p[0] ? INPUT_PULLUP : INPUT); return true; } void IODevice::write(VPIN vpin, int value) { + if (vpin >= NUM_DIGITAL_PINS) return; digitalWrite(vpin, value); pinMode(vpin, OUTPUT); } @@ -324,6 +322,7 @@ void IODevice::writeAnalogue(VPIN, int, uint8_t, uint16_t) {} bool IODevice::isBusy(VPIN) { return false; } bool IODevice::hasCallback(VPIN) { return false; } int IODevice::read(VPIN vpin) { + if (vpin >= NUM_DIGITAL_PINS) return 0; return !digitalRead(vpin); // Return inverted state (5v=0, 0v=1) } int IODevice::readAnalogue(VPIN vpin) {