>From cc6af408c97b04994ace885862e6687387e8ee79 Mon Sep 17 00:00:00 2001
From: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
Date: Sat, 10 May 2014 00:30:49 +0200
Subject: [PATCH] AT45DB: fix length calculations in read functions.

This fixes segfaults on reads, ouch.
Thanks to The Raven and Alexander Irenkov for reporting the problem.

Signed-off-by: Stefan Tauner <stefan.tauner@alumni.tuwien.ac.at>
---
 at45db.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/at45db.c b/at45db.c
index b1a81ef..f8ccd93 100644
--- a/at45db.c
+++ b/at45db.c
@@ -262,6 +262,7 @@ int spi_read_at45db(struct flashctx *flash, uint8_t *buf, unsigned int addr, uns
 			return ret;
 		}
 		addr += chunk;
+		len -= chunk;
 	}
 
 	return 0;
@@ -302,6 +303,7 @@ int spi_read_at45db_e8(struct flashctx *flash, uint8_t *buf, unsigned int addr,
 		/* Copy result without dummy bytes into buf and advance address counter respectively. */
 		memcpy(buf + addr, tmp + 4, chunk - 4);
 		addr += chunk - 4;
+		len -= chunk - 4;
 	}
 	return 0;
 }
-- 
Kind regards, Stefan Tauner

