From a7a45b0e710dabb267859bf8794e19eefc173a7b Mon Sep 17 00:00:00 2001 From: Lars-Dominik Braun Date: Tue, 11 Nov 2014 15:27:10 +0100 Subject: Fix i2c out of bounds write MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes longstanding bug that caused strange behavior wrt LED’s states. --- i2c.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/i2c.c b/i2c.c index a94b3a0..26da2d8 100644 --- a/i2c.c +++ b/i2c.c @@ -72,6 +72,8 @@ void twInit () { bool twRequest (const twMode mode, const uint8_t address, const uint8_t subaddress, uint8_t * const data, const uint8_t count) { assert (twr.status == TWST_OK); + assert (count > 0); + assert (data != NULL); twr.mode = mode; twr.address = address; @@ -208,7 +210,7 @@ static void twIntRead () { if (status == TW_MR_DATA_ACK) { twr.data[twr.i] = TWDR; ++twr.i; - if (twr.i < twr.count-1) { + if (twr.i < twr.count) { /* read another byte, not the last one */ twFlushContRaw (); /* step stays the same */ @@ -225,8 +227,6 @@ static void twIntRead () { case 6: if (status == TW_MR_DATA_NACK) { - /* receive final byte, send stop */ - twr.data[twr.i] = TWDR; twStopRaw (); twr.status = TWST_OK; } else { -- cgit v1.2.3