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 <[email protected]>
---
 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


_______________________________________________
flashrom mailing list
[email protected]
http://www.flashrom.org/mailman/listinfo/flashrom

Reply via email to