Skip to content

Commit 121dcd2

Browse files
committed
Fix sparkfun#454: use-after-free in Wire and SPI libraries.
Also add the WireShim to properly shutdown the Wire hardware.
1 parent feebcb6 commit 121dcd2

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

libraries/SPI/src/SPI.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ void arduino::MbedSPI::begin() {
8686
void arduino::MbedSPI::end() {
8787
if (dev) {
8888
delete dev;
89+
dev = NULL;
8990
}
9091
}
9192

libraries/Wire/src/Wire.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ arduino::MbedI2C::MbedI2C(int sda, int scl) : _sda(sda), _scl(scl), usedTxBuffer
1313

1414
void arduino::MbedI2C::begin() {
1515
if(!master){
16-
master = new mbed::I2C((PinName)_sda, (PinName)_scl);
16+
master = new WireShim((PinName)_sda, (PinName)_scl);
1717
setClock(100000); //Default to 100kHz
1818
}
1919
}
@@ -31,6 +31,7 @@ void arduino::MbedI2C::begin(uint8_t slaveAddr) {
3131
void arduino::MbedI2C::end() {
3232
if (master != NULL) {
3333
delete master;
34+
master = NULL;
3435
}
3536
#ifdef DEVICE_I2CSLAVE
3637
if (slave != NULL) {

libraries/Wire/src/Wire.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ typedef void (*voidFuncPtrParamInt)(int);
1919

2020
namespace arduino {
2121

22+
class WireShim: public mbed::I2C
23+
{
24+
public:
25+
using mbed::I2C::I2C;
26+
virtual ~WireShim(){
27+
i2c_free(&_i2c);
28+
};
29+
};
30+
2231
class MbedI2C : public HardwareI2C
2332
{
2433
public:
@@ -52,7 +61,7 @@ class MbedI2C : public HardwareI2C
5261
#ifdef DEVICE_I2CSLAVE
5362
mbed::I2CSlave* slave;
5463
#endif
55-
mbed::I2C* master;
64+
WireShim* master;
5665
int _sda;
5766
int _scl;
5867
int _address;

0 commit comments

Comments
 (0)