From 7310df030f19d8e11586fc53f02f667031b78c54 Mon Sep 17 00:00:00 2001
From: Stefan Rueger <stefan.rueger@urclocks.com>
Date: Sun, 24 Jul 2022 18:48:22 +0100
Subject: [PATCH] Check stk500_recv() actually worked before accepting SYNC
 byte

---
 src/stk500.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/src/stk500.c b/src/stk500.c
index fa013012..208fb1a3 100644
--- a/src/stk500.c
+++ b/src/stk500.c
@@ -91,8 +91,6 @@ int stk500_getsync(PROGRAMMER * pgm)
   int attempt;
   int max_sync_attempts;
 
-  /*
-   * get in sync */
   buf[0] = Cmnd_STK_GET_SYNC;
   buf[1] = Sync_CRC_EOP;
   
@@ -119,11 +117,11 @@ int stk500_getsync(PROGRAMMER * pgm)
       usleep(50*1000);
       stk500_drain(pgm, 0);
     }
+
     stk500_send(pgm, buf, 2);
-    stk500_recv(pgm, resp, 1);
-    if (resp[0] == Resp_STK_INSYNC){
+    if(stk500_recv(pgm, resp, 1) >= 0 && resp[0] == Resp_STK_INSYNC)
       break;
-    }
+
     avrdude_message(MSG_INFO, "%s: stk500_getsync() attempt %d of %d: not in sync: resp=0x%02x\n",
                     progname, attempt + 1, max_sync_attempts, resp[0]);
   }