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

16 files changed

+45
-62
lines changed

client.c

+1-2
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

+5-7
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

+1-2
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

+4-10
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

+2-3
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

+2-3
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

+3-6
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

+2-4
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

+2-3
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

+1-2
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,

privops.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -547,9 +547,9 @@ PRV_BindSocket(int sock, struct sockaddr *address, socklen_t address_len)
547547
PrvResponse res;
548548

549549
SCK_SockaddrToIPSockAddr(address, address_len, &ip_saddr);
550-
if (ip_saddr.port != 0 && ip_saddr.port != CNF_GetNTPPort() &&
551-
ip_saddr.port != CNF_GetAcquisitionPort() && ip_saddr.port != CNF_GetPtpPort())
552-
assert(0);
550+
BRIEF_ASSERT(ip_saddr.port == 0 || ip_saddr.port == CNF_GetNTPPort() ||
551+
ip_saddr.port == CNF_GetAcquisitionPort() ||
552+
ip_saddr.port == CNF_GetPtpPort());
553553

554554
if (!have_helper())
555555
return bind(sock, address, address_len);

quantiles.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,8 @@ QNT_CreateInstance(int min_k, int max_k, int q, int repeat,
6262
QNT_Instance inst;
6363
long seed;
6464

65-
if (q < 2 || min_k > max_k || min_k < 1 || max_k >= q ||
66-
repeat < 1 || repeat > MAX_REPEAT || min_step <= 0.0 || large_step_delay < 0)
67-
assert(0);
65+
BRIEF_ASSERT(q >= 2 && min_k <= max_k && min_k >= 1 && max_k < q && repeat >= 1 &&
66+
repeat <= MAX_REPEAT && min_step > 0.0 && large_step_delay >= 0);
6867

6968
inst = MallocNew(struct QNT_Instance_Record);
7069
inst->n_quants = (max_k - min_k + 1) * repeat;
@@ -117,8 +116,7 @@ insert_initial_value(QNT_Instance inst, double value)
117116
{
118117
int i, j, r = inst->repeat;
119118

120-
if (inst->n_set * r >= inst->n_quants)
121-
assert(0);
119+
BRIEF_ASSERT(inst->n_set * r < inst->n_quants);
122120

123121
/* Keep the initial estimates repeated and ordered */
124122
for (i = inst->n_set; i > 0 && inst->quants[(i - 1) * r].est > value; i--) {
@@ -225,8 +223,7 @@ QNT_GetQuantile(QNT_Instance inst, int k)
225223
double estimates[MAX_REPEAT];
226224
int i;
227225

228-
if (k < inst->min_k || (k - inst->min_k) * inst->repeat >= inst->n_quants)
229-
assert(0);
226+
BRIEF_ASSERT(k >= inst->min_k && (k - inst->min_k) * inst->repeat < inst->n_quants);
230227

231228
for (i = 0; i < inst->repeat; i++)
232229
estimates[i] = inst->quants[(k - inst->min_k) * inst->repeat + i].est;

siv_nettle.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
#include "memory.h"
3737
#include "siv.h"
38+
#include "util.h"
3839

3940
struct SIV_Instance_Record {
4041
SIV_Algorithm algorithm;
@@ -158,8 +159,7 @@ SIV_GetMaxNonceLength(SIV_Instance instance)
158159
int
159160
SIV_GetTagLength(SIV_Instance instance)
160161
{
161-
if (instance->tag_length < 1 || instance->tag_length > SIV_MAX_TAG_LENGTH)
162-
assert(0);
162+
BRIEF_ASSERT(instance->tag_length >= 1 && instance->tag_length <= SIV_MAX_TAG_LENGTH);
163163
return instance->tag_length;
164164
}
165165

socket.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -1061,9 +1061,8 @@ receive_messages(int sock_fd, int flags, int max_messages, int *num_messages)
10611061
n = ARR_GetSize(recv_headers);
10621062
n = MIN(n, max_messages);
10631063

1064-
if (n < 1 || n > MAX_RECV_MESSAGES ||
1065-
n > ARR_GetSize(recv_messages) || n > ARR_GetSize(recv_sck_messages))
1066-
assert(0);
1064+
BRIEF_ASSERT(n >= 1 && n <= MAX_RECV_MESSAGES &&
1065+
n <= ARR_GetSize(recv_messages) && n <= ARR_GetSize(recv_sck_messages));
10671066

10681067
recv_flags = get_recv_flags(flags);
10691068

sources.c

+3-5
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,8 @@ void SRC_DestroyInstance(SRC_Instance instance)
322322
last_updated_inst = NULL;
323323

324324
assert(initialised);
325-
if (instance->index < 0 || instance->index >= n_sources ||
326-
instance != sources[instance->index])
327-
assert(0);
325+
BRIEF_ASSERT(instance->index >= 0 && instance->index < n_sources &&
326+
instance == sources[instance->index]);
328327

329328
SST_DeleteInstance(instance->stats);
330329
dead_index = instance->index;
@@ -763,8 +762,7 @@ mark_source(SRC_Instance inst, SRC_Status status)
763762
{
764763
set_source_status(inst, status);
765764

766-
if (status < SRC_OK || status >= sizeof (inst->reported_status))
767-
assert(0);
765+
BRIEF_ASSERT(status >= SRC_OK && status < sizeof (inst->reported_status));
768766

769767
if (!inst->reported_status[status]) {
770768
switch (status) {

util.h

+8
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,12 @@ extern int UTI_SplitString(char *string, char **words, int max_saved_words);
272272

273273
#define SQUARE(x) ((x) * (x))
274274

275+
/* Macro to make an assertion with the text of a long argument replaced
276+
with "0" to avoid bloating the compiled binary */
277+
#ifdef NDEBUG
278+
#define BRIEF_ASSERT(a)
279+
#else
280+
#define BRIEF_ASSERT(a) if (!(a)) assert(0)
281+
#endif
282+
275283
#endif /* GOT_UTIL_H */

0 commit comments

Comments
 (0)