[flashrom] [PATCH 2/3] Break endless loop in serialport_write().

Stefan Tauner stefan.tauner at student.tuwien.ac.at
Mon Nov 26 16:52:16 CET 2012


serialport_write could loop endlessly when used with a seemingly valid port
that does always return 0 on writes instead of an error. Give up after 10k tries.

Signed-off-by: Stefan Tauner <stefan.tauner at student.tuwien.ac.at>
---
 serial.c |   11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/serial.c b/serial.c
index 11a749c..a3cdefb 100644
--- a/serial.c
+++ b/serial.c
@@ -264,6 +264,7 @@ int serialport_write(unsigned char *buf, unsigned int writecnt)
 #else
 	ssize_t tmp = 0;
 #endif
+	uint empty_writes = 10000;
 
 	while (writecnt > 0) {
 #ifdef _WIN32
@@ -275,9 +276,15 @@ int serialport_write(unsigned char *buf, unsigned int writecnt)
 			msg_perr("Serial port write error!\n");
 			return 1;
 		}
-		if (!tmp)
+		if (!tmp) {
 			msg_pdbg("Empty write\n");
-		writecnt -= tmp; 
+			empty_writes--;
+			if (empty_writes == 0) {
+				msg_perr("Serial port seems dead!\n");
+				return 1;
+			}
+		}
+		writecnt -= tmp;
 		buf += tmp;
 	}
 
-- 
Kind regards, Stefan Tauner





More information about the flashrom mailing list