Skip to content

Commit 9427d4d

Browse files
committed
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.
1 parent 644431c commit 9427d4d

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

sim/drivers/SpiNorFlash.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "drivers/Spi.h"
55
#include <filesystem>
66
#include <iostream>
7+
#include <stdexcept>
78

89
using namespace Pinetime::Drivers;
910

@@ -63,6 +64,9 @@ uint8_t SpiNorFlash::ReadConfigurationRegister() {
6364

6465
void SpiNorFlash::Read(uint32_t address, uint8_t* buffer, size_t size) {
6566
static_assert(sizeof(uint8_t) == sizeof(char));
67+
if (address + size * sizeof(uint8_t) > memorySize) {
68+
throw std::runtime_error("SpiNorFlash::Read out of bounds");
69+
}
6670
memoryFile.seekp(address);
6771
memoryFile.read(reinterpret_cast<char *>(buffer), size);
6872
}
@@ -88,6 +92,9 @@ bool SpiNorFlash::EraseFailed() {
8892
}
8993

9094
void SpiNorFlash::Write(uint32_t address, const uint8_t* buffer, size_t size) {
95+
if (address + size * sizeof(uint8_t) > memorySize) {
96+
throw std::runtime_error("SpiNorFlash::Write out of bounds");
97+
}
9198
memoryFile.seekp(address);
9299
memoryFile.write(reinterpret_cast<const char *>(buffer), size);
93100
memoryFile.flush();

0 commit comments

Comments
 (0)