Skip to content

Commit

Permalink
Fix sparkfun#454: use-after-free in Wire and SPI libraries.
Browse files Browse the repository at this point in the history
Also add the WireShim to properly shutdown the Wire
hardware.
  • Loading branch information
nigelb committed Nov 11, 2022
1 parent feebcb6 commit 121dcd2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions libraries/SPI/src/SPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ void arduino::MbedSPI::begin() {
void arduino::MbedSPI::end() {
if (dev) {
delete dev;
dev = NULL;
}
}

Expand Down
3 changes: 2 additions & 1 deletion libraries/Wire/src/Wire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(sda), _scl(scl), usedTxBuffer

void arduino::MbedI2C::begin() {
if(!master){
master = new mbed::I2C((PinName)_sda, (PinName)_scl);
master = new WireShim((PinName)_sda, (PinName)_scl);
setClock(100000); //Default to 100kHz
}
}
Expand All @@ -31,6 +31,7 @@ void arduino::MbedI2C::begin(uint8_t slaveAddr) {
void arduino::MbedI2C::end() {
if (master != NULL) {
delete master;
master = NULL;
}
#ifdef DEVICE_I2CSLAVE
if (slave != NULL) {
Expand Down
11 changes: 10 additions & 1 deletion libraries/Wire/src/Wire.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ typedef void (*voidFuncPtrParamInt)(int);

namespace arduino {

class WireShim: public mbed::I2C
{
public:
using mbed::I2C::I2C;
virtual ~WireShim(){
i2c_free(&_i2c);
};
};

class MbedI2C : public HardwareI2C
{
public:
Expand Down Expand Up @@ -52,7 +61,7 @@ class MbedI2C : public HardwareI2C
#ifdef DEVICE_I2CSLAVE
mbed::I2CSlave* slave;
#endif
mbed::I2C* master;
WireShim* master;
int _sda;
int _scl;
int _address;
Expand Down

0 comments on commit 121dcd2

Please sign in to comment.