48ea54
commit d0b24860363a3704e28569ce9a6987717834edea
48ea54
Author: Miroslav Lichvar <mlichvar@redhat.com>
48ea54
Date:   Tue Dec 5 11:08:24 2017 +0100
48ea54
48ea54
    client: don't call select() with invalid timeout
48ea54
    
48ea54
    If the system clock was stepped forward after chronyc sent a request and
48ea54
    before it read the clock in order to calculate the receive timeout,
48ea54
    select() could be called with a negative timeout, which resulted in an
48ea54
    infinite loop waiting for select() to succeed.
48ea54
    
48ea54
    Fix the submit_request() function to not call select() with a negative
48ea54
    timeout. Also, return immediately on any error of select().
48ea54
48ea54
diff --git a/client.c b/client.c
48ea54
index 5c3a99e..4e23158 100644
48ea54
--- a/client.c
48ea54
+++ b/client.c
48ea54
@@ -1394,9 +1394,16 @@ submit_request(CMD_Request *request, CMD_Reply *reply)
48ea54
 
48ea54
     timeout = initial_timeout / 1000.0 * (1U << (n_attempts - 1)) -
48ea54
               UTI_DiffTimespecsToDouble(&ts_now, &ts_start);
48ea54
-    UTI_DoubleToTimeval(timeout, &tv;;
48ea54
     DEBUG_LOG("Timeout %f seconds", timeout);
48ea54
 
48ea54
+    /* Avoid calling select() with an invalid timeout */
48ea54
+    if (timeout <= 0.0) {
48ea54
+      new_attempt = 1;
48ea54
+      continue;
48ea54
+    }
48ea54
+
48ea54
+    UTI_DoubleToTimeval(timeout, &tv;;
48ea54
+
48ea54
     FD_ZERO(&rdfd);
48ea54
     FD_ZERO(&wrfd);
48ea54
     FD_ZERO(&exfd);
48ea54
@@ -1410,6 +1417,7 @@ submit_request(CMD_Request *request, CMD_Reply *reply)
48ea54
 
48ea54
     if (select_status < 0) {
48ea54
       DEBUG_LOG("select failed : %s", strerror(errno));
48ea54
+      return 0;
48ea54
     } else if (select_status == 0) {
48ea54
       /* Timeout must have elapsed, try a resend? */
48ea54
       new_attempt = 1;