From 9f370024c0c2c180bfe2eb3dc0966ef36dcd2d40 Mon Sep 17 00:00:00 2001 From: Stefan Rueger Date: Mon, 17 Jul 2023 19:30:59 +0100 Subject: [PATCH] Return -1 in stk500v2_open() or stk600v2_open() if drain() or getsync() fails --- src/stk500v2.c | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/stk500v2.c b/src/stk500v2.c index f5bf5499..eb25dc67 100644 --- a/src/stk500v2.c +++ b/src/stk500v2.c @@ -2124,14 +2124,9 @@ static int stk500v2_open(PROGRAMMER *pgm, const char *port) { if (serdev && serdev->usbsn) pgm->usbsn = serdev->usbsn; - /* - * drain any extraneous input - */ - stk500v2_drain(pgm, 0); - - stk500v2_getsync(pgm); - - stk500v2_drain(pgm, 0); + // Drain any extraneous input, synchronise and drain again + if(stk500v2_drain(pgm, 0) < 0 || stk500v2_getsync(pgm) < 0 || stk500v2_drain(pgm, 0) < 0) + return -1; if (pgm->bitclock != 0.0) { if (pgm->set_sck_period(pgm, pgm->bitclock) != 0) @@ -2181,14 +2176,9 @@ static int stk600_open(PROGRAMMER *pgm, const char *port) { return -1; } - /* - * drain any extraneous input - */ - stk500v2_drain(pgm, 0); - - stk500v2_getsync(pgm); - - stk500v2_drain(pgm, 0); + // Drain any extraneous input, synchronise and drain again + if(stk500v2_drain(pgm, 0) < 0 || stk500v2_getsync(pgm) < 0 || stk500v2_drain(pgm, 0) < 0) + return -1; if (pgm->bitclock != 0.0) { if (pgm->set_sck_period(pgm, pgm->bitclock) != 0)