]> err.no Git - yubikey-personalization/commitdiff
move the sleep in yk_wait_for_status() to the top of the loop
authorKlas Lindfors <klas@yubico.com>
Tue, 17 Dec 2013 11:07:42 +0000 (12:07 +0100)
committerKlas Lindfors <klas@yubico.com>
Tue, 17 Dec 2013 11:07:42 +0000 (12:07 +0100)
fixes #38
it seems there is a race if a fast computer makes control transfers
very close to each other in time, this enforces a 10ms sleep at the
start of a read.

ykcore/ykcore.c

index f129e5b602e9dd6e0559e633ef21f35f84140ff2..4810625ba82d97073d4cceb85f310e6ef17a661e 100644 (file)
@@ -471,6 +471,13 @@ int yk_wait_for_key_status(YK_KEY *yk, uint8_t slot, unsigned int flags,
        slot = 0;
 
        while (slept_time < max_time_ms) {
+               Sleep(sleepval);
+               slept_time += sleepval;
+               /* exponential backoff, up to 500 ms */
+               sleepval *= 2;
+               if (sleepval > 500)
+                       sleepval = 500;
+
                /* Read a status report from the key */
                memset(data, 0, sizeof(data));
                if (!_ykusb_read(yk, REPORT_TYPE_FEATURE, slot, (char *) &data, FEATURE_RPT_SIZE))
@@ -512,13 +519,6 @@ int yk_wait_for_key_status(YK_KEY *yk, uint8_t slot, unsigned int flags,
                                break;
                        }
                }
-
-               Sleep(sleepval);
-               slept_time += sleepval;
-               /* exponential backoff, up to 500 ms */
-               sleepval *= 2;
-               if (sleepval > 500)
-                       sleepval = 500;
        }
 
        yk_errno = YK_ETIMEOUT;