i2c: prevent double free of _dev pointer

this caused heap corruption
This commit is contained in:
Andrew Tridgell
2015-02-14 10:22:55 +11:00
committed by Lorenz Meier
parent cbe44e572b
commit 1b8a830a38
+5 -1
View File
@@ -74,8 +74,10 @@ I2C::I2C(const char *name,
I2C::~I2C()
{
if (_dev)
if (_dev) {
up_i2cuninitialize(_dev);
_dev = nullptr;
}
}
int
@@ -118,6 +120,7 @@ I2C::init()
// is smaller than the bus frequency
if (_bus_clocks[bus_index] > _frequency) {
(void)up_i2cuninitialize(_dev);
_dev = nullptr;
log("FAIL: too slow for bus #%u: %u KHz, device max: %u KHz)",
_bus, _bus_clocks[bus_index] / 1000, _frequency / 1000);
ret = -EINVAL;
@@ -164,6 +167,7 @@ I2C::init()
out:
if ((ret != OK) && (_dev != nullptr)) {
up_i2cuninitialize(_dev);
_dev = nullptr;
}
return ret;
}