]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
test: fix ntp_core unit test
authorMiroslav Lichvar <mlichvar@redhat.com>
Mon, 14 Sep 2020 08:19:10 +0000 (10:19 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 16 Sep 2020 09:15:16 +0000 (11:15 +0200)
Fix setting of key_id in the response.

Fixes: f6625717cdb0 ("test: improve ntp_core unit test")
test/unit/ntp_core.c

index 40a5ae22bdb5d35f9b9e92f9bf21d16198f273b0..82e27311e771c61591dd132850be38de1d5aff5a 100644 (file)
@@ -234,7 +234,9 @@ send_response(int interleaved, int authenticated, int allow_update, int valid_ts
         ((unsigned char *)res)[res_length - auth_len + random() % auth_len]++;
         break;
       case 3:
-        res_length -= 4 + 4 * (random() % (auth_len / 4));
+        res_length -= 4 + auth_len;
+        auth_len = 4 * (random() % (auth_len / 4));
+        res_length += 4 + auth_len;
         break;
       case 4:
         if (NTP_LVM_TO_VERSION(res->lvm) == 4 && random() % 2 &&
@@ -248,6 +250,7 @@ send_response(int interleaved, int authenticated, int allow_update, int valid_ts
           res_length += 4 + auth_len;
         } else {
           memset((unsigned char *)res + res_length, 0, 4);
+          auth_len += 4;
           res_length += 4;
         }
         break;
@@ -256,6 +259,9 @@ send_response(int interleaved, int authenticated, int allow_update, int valid_ts
     }
   }
 
+  assert(res_length <= sizeof (*res));
+  assert(res_length >= NTP_HEADER_LENGTH + auth_len);
+
   if (authenticated)
     *(uint32_t *)((unsigned char *)res + res_length - auth_len - 4) = htonl(key_id);
 }