Skip to content

Commit 711c7c0

Browse files
committed
rewrite some assertions for better readability
Some assertions are written as "if (x) assert(0)" to avoid having the text of a long argument compiled in the binary. Rewrite them to use a new BRIEF_ASSERT macro to make the condition easier to read in its non-negated form and make it easier to turn it back to the full-text assert if needed.
1 parent 454ce62 commit 711c7c0

File tree

16 files changed

+45
-62
lines changed

16 files changed

+45
-62
lines changed

client.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -946,8 +946,7 @@ process_cmd_add_source(CMD_Request *msg, char *line)
946946
}
947947

948948
msg->data.ntp_source.type = htonl(type);
949-
if (strlen(data.name) >= sizeof (msg->data.ntp_source.name))
950-
assert(0);
949+
BRIEF_ASSERT(strlen(data.name) < sizeof (msg->data.ntp_source.name));
951950
strncpy((char *)msg->data.ntp_source.name, data.name,
952951
sizeof (msg->data.ntp_source.name));
953952
msg->data.ntp_source.port = htonl(data.port);

cmdmon.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,17 @@ do_size_checks(void)
146146
request.command = htons(i);
147147
request_length = PKL_CommandLength(&request);
148148
padding_length = PKL_CommandPaddingLength(&request);
149-
if (padding_length > MAX_PADDING_LENGTH || padding_length > request_length ||
150-
request_length > sizeof (CMD_Request) ||
151-
(request_length && request_length < offsetof(CMD_Request, data)))
152-
assert(0);
149+
BRIEF_ASSERT(padding_length <= MAX_PADDING_LENGTH && padding_length <= request_length &&
150+
request_length <= sizeof (CMD_Request) &&
151+
(request_length == 0 || request_length >= offsetof(CMD_Request, data)));
153152
}
154153

155154
for (i = 1; i < N_REPLY_TYPES; i++) {
156155
reply.reply = htons(i);
157156
reply.status = STT_SUCCESS;
158157
reply_length = PKL_ReplyLength(&reply);
159-
if ((reply_length && reply_length < offsetof(CMD_Reply, data)) ||
160-
reply_length > sizeof (CMD_Reply))
161-
assert(0);
158+
BRIEF_ASSERT((reply_length == 0 || reply_length >= offsetof(CMD_Reply, data)) &&
159+
reply_length <= sizeof (CMD_Reply));
162160
}
163161
}
164162

conf.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2828,8 +2828,7 @@ CNF_GetNtsTrustedCertsPaths(const char ***paths, uint32_t **ids)
28282828
*paths = ARR_GetElements(nts_trusted_certs_paths);
28292829
*ids = ARR_GetElements(nts_trusted_certs_ids);
28302830

2831-
if (ARR_GetSize(nts_trusted_certs_paths) != ARR_GetSize(nts_trusted_certs_ids))
2832-
assert(0);
2831+
BRIEF_ASSERT(ARR_GetSize(nts_trusted_certs_paths) == ARR_GetSize(nts_trusted_certs_ids));
28332832

28342833
return ARR_GetSize(nts_trusted_certs_paths);
28352834
}

local.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,8 @@ void
184184
LCL_Finalise(void)
185185
{
186186
/* Make sure all handlers have been removed */
187-
if (change_list.next != &change_list)
188-
assert(0);
189-
if (dispersion_notify_list.next != &dispersion_notify_list)
190-
assert(0);
187+
BRIEF_ASSERT(change_list.next == &change_list);
188+
BRIEF_ASSERT(dispersion_notify_list.next == &dispersion_notify_list);
191189
}
192190

193191
/* ================================================== */
@@ -225,9 +223,7 @@ LCL_AddParameterChangeHandler(LCL_ParameterChangeHandler handler, void *anything
225223

226224
/* Check that the handler is not already registered */
227225
for (ptr = change_list.next; ptr != &change_list; ptr = ptr->next) {
228-
if (!(ptr->handler != handler || ptr->anything != anything)) {
229-
assert(0);
230-
}
226+
BRIEF_ASSERT(ptr->handler != handler || ptr->anything != anything);
231227
}
232228

233229
new_entry = MallocNew(ChangeListEntry);
@@ -301,9 +297,7 @@ LCL_AddDispersionNotifyHandler(LCL_DispersionNotifyHandler handler, void *anythi
301297

302298
/* Check that the handler is not already registered */
303299
for (ptr = dispersion_notify_list.next; ptr != &dispersion_notify_list; ptr = ptr->next) {
304-
if (!(ptr->handler != handler || ptr->anything != anything)) {
305-
assert(0);
306-
}
300+
BRIEF_ASSERT(ptr->handler != handler || ptr->anything != anything);
307301
}
308302

309303
new_entry = MallocNew(DispersionNotifyListEntry);

ntp_core.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2337,9 +2337,8 @@ process_response(NCR_Instance inst, int saved, NTP_Local_Address *local_addr,
23372337
inst->valid_rx = 1;
23382338
}
23392339

2340-
if ((unsigned int)local_receive.source >= sizeof (tss_chars) ||
2341-
(unsigned int)local_transmit.source >= sizeof (tss_chars))
2342-
assert(0);
2340+
BRIEF_ASSERT((unsigned int)local_receive.source < sizeof (tss_chars) &&
2341+
(unsigned int)local_transmit.source < sizeof (tss_chars));
23432342

23442343
DEBUG_LOG("NTP packet lvm=%o stratum=%d poll=%d prec=%d root_delay=%.9f root_disp=%.9f refid=%"PRIx32" [%s]",
23452344
message->lvm, message->stratum, message->poll, message->precision,

ntp_sources.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,8 @@ change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr
457457
if (replacement)
458458
record->resolved_addr = new_addr->ip_addr;
459459

460-
if (record->remote_addr != NCR_GetRemoteAddress(record->data) ||
461-
UTI_CompareIPs(&record->remote_addr->ip_addr, &new_addr->ip_addr, NULL) != 0)
462-
assert(0);
460+
BRIEF_ASSERT(record->remote_addr == NCR_GetRemoteAddress(record->data) &&
461+
UTI_CompareIPs(&record->remote_addr->ip_addr, &new_addr->ip_addr, NULL) == 0);
463462

464463
if (!UTI_IsIPReal(&old_addr->ip_addr) && UTI_IsIPReal(&new_addr->ip_addr)) {
465464
if (auto_start_sources)

nts_ke_server.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -520,8 +520,7 @@ generate_key(int index)
520520
ServerKey *key;
521521
int key_length;
522522

523-
if (index < 0 || index >= MAX_SERVER_KEYS)
524-
assert(0);
523+
BRIEF_ASSERT(index >= 0 && index < MAX_SERVER_KEYS);
525524

526525
/* Prefer AES-128-GCM-SIV if available. Note that if older keys loaded
527526
from ntsdumpdir use a different algorithm, responding to NTP requests
@@ -534,8 +533,7 @@ generate_key(int index)
534533
key = &server_keys[index];
535534

536535
key_length = SIV_GetKeyLength(algorithm);
537-
if (key_length > sizeof (key->key))
538-
assert(0);
536+
BRIEF_ASSERT(key_length <= sizeof (key->key));
539537

540538
UTI_GetRandomBytesUrandom(key->key, key_length);
541539
memset(key->key + key_length, 0, sizeof (key->key) - key_length);
@@ -961,8 +959,7 @@ NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie)
961959
header->key_id = htonl(key->id);
962960

963961
nonce = cookie->cookie + sizeof (*header);
964-
if (key->nonce_length > sizeof (cookie->cookie) - sizeof (*header))
965-
assert(0);
962+
BRIEF_ASSERT(key->nonce_length <= sizeof (cookie->cookie) - sizeof (*header));
966963
UTI_GetRandomBytes(nonce, key->nonce_length);
967964

968965
plaintext_length = context->c2s.length + context->s2c.length;

nts_ke_session.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ create_credentials(const char **certs, const char **keys, int n_certs_keys,
663663
goto error;
664664

665665
if (certs && keys) {
666-
if (trusted_certs || trusted_certs_ids)
667-
assert(0);
666+
BRIEF_ASSERT(!trusted_certs && !trusted_certs_ids);
668667

669668
for (i = 0; i < n_certs_keys; i++) {
670669
if (!UTI_CheckFilePermissions(keys[i], 0771))
@@ -675,8 +674,7 @@ create_credentials(const char **certs, const char **keys, int n_certs_keys,
675674
goto error;
676675
}
677676
} else {
678-
if (certs || keys || n_certs_keys > 0)
679-
assert(0);
677+
BRIEF_ASSERT(!certs && !keys && n_certs_keys <= 0);
680678

681679
if (trusted_cert_set == 0 && !CNF_GetNoSystemCert()) {
682680
r = gnutls_certificate_set_x509_system_trust(credentials);

nts_ntp_auth.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ NNA_GenerateAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv,
104104
body = (unsigned char *)(header + 1);
105105
ciphertext = body + nonce_length + nonce_padding;
106106

107-
if ((unsigned char *)header + auth_length !=
108-
ciphertext + ciphertext_length + ciphertext_padding + additional_padding)
109-
assert(0);
107+
BRIEF_ASSERT((unsigned char *)header + auth_length ==
108+
ciphertext + ciphertext_length + ciphertext_padding + additional_padding);
110109

111110
memcpy(body, nonce, nonce_length);
112111
memset(body + nonce_length, 0, nonce_padding);

nts_ntp_server.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ NNS_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *req_info,
259259

260260
/* Make sure this is a response to the request from the last call
261261
of NNS_CheckRequestAuth() */
262-
if (UTI_CompareNtp64(&server->req_tx, &request->transmit_ts) != 0)
263-
assert(0);
262+
BRIEF_ASSERT(UTI_CompareNtp64(&server->req_tx, &request->transmit_ts) == 0);
264263

265264
for (parsed = NTP_HEADER_LENGTH; parsed < req_info->length; parsed += ef_length) {
266265
if (!NEF_ParseField(request, req_info->length, parsed,

0 commit comments

Comments
 (0)