From 9427d4ddb13949536b6a6c44419848622ab31142 Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Sun, 15 May 2022 22:46:07 +0200 Subject: [PATCH] SpiNorFlash: check Read/Write for out of bounds Just to be extra pedantic and to get an error while developing on InfiniSim. Because on PC we can easily add the debugger, on the device we can, but it is a bit more involved. --- sim/drivers/SpiNorFlash.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/sim/drivers/SpiNorFlash.cpp b/sim/drivers/SpiNorFlash.cpp index bca056f..4569355 100644 --- a/sim/drivers/SpiNorFlash.cpp +++ b/sim/drivers/SpiNorFlash.cpp @@ -4,6 +4,7 @@ #include "drivers/Spi.h" #include #include +#include using namespace Pinetime::Drivers; @@ -63,6 +64,9 @@ uint8_t SpiNorFlash::ReadConfigurationRegister() { void SpiNorFlash::Read(uint32_t address, uint8_t* buffer, size_t size) { static_assert(sizeof(uint8_t) == sizeof(char)); + if (address + size * sizeof(uint8_t) > memorySize) { + throw std::runtime_error("SpiNorFlash::Read out of bounds"); + } memoryFile.seekp(address); memoryFile.read(reinterpret_cast(buffer), size); } @@ -88,6 +92,9 @@ bool SpiNorFlash::EraseFailed() { } void SpiNorFlash::Write(uint32_t address, const uint8_t* buffer, size_t size) { + if (address + size * sizeof(uint8_t) > memorySize) { + throw std::runtime_error("SpiNorFlash::Write out of bounds"); + } memoryFile.seekp(address); memoryFile.write(reinterpret_cast(buffer), size); memoryFile.flush();