mirror of
https://gitlab.com/chrony/chrony.git
synced 2025-12-03 15:45:07 -05:00
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.
This commit is contained in:
3
client.c
3
client.c
@@ -946,8 +946,7 @@ process_cmd_add_source(CMD_Request *msg, char *line)
|
|||||||
}
|
}
|
||||||
|
|
||||||
msg->data.ntp_source.type = htonl(type);
|
msg->data.ntp_source.type = htonl(type);
|
||||||
if (strlen(data.name) >= sizeof (msg->data.ntp_source.name))
|
BRIEF_ASSERT(strlen(data.name) < sizeof (msg->data.ntp_source.name));
|
||||||
assert(0);
|
|
||||||
strncpy((char *)msg->data.ntp_source.name, data.name,
|
strncpy((char *)msg->data.ntp_source.name, data.name,
|
||||||
sizeof (msg->data.ntp_source.name));
|
sizeof (msg->data.ntp_source.name));
|
||||||
msg->data.ntp_source.port = htonl(data.port);
|
msg->data.ntp_source.port = htonl(data.port);
|
||||||
|
|||||||
12
cmdmon.c
12
cmdmon.c
@@ -146,19 +146,17 @@ do_size_checks(void)
|
|||||||
request.command = htons(i);
|
request.command = htons(i);
|
||||||
request_length = PKL_CommandLength(&request);
|
request_length = PKL_CommandLength(&request);
|
||||||
padding_length = PKL_CommandPaddingLength(&request);
|
padding_length = PKL_CommandPaddingLength(&request);
|
||||||
if (padding_length > MAX_PADDING_LENGTH || padding_length > request_length ||
|
BRIEF_ASSERT(padding_length <= MAX_PADDING_LENGTH && padding_length <= request_length &&
|
||||||
request_length > sizeof (CMD_Request) ||
|
request_length <= sizeof (CMD_Request) &&
|
||||||
(request_length && request_length < offsetof(CMD_Request, data)))
|
(request_length == 0 || request_length >= offsetof(CMD_Request, data)));
|
||||||
assert(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (i = 1; i < N_REPLY_TYPES; i++) {
|
for (i = 1; i < N_REPLY_TYPES; i++) {
|
||||||
reply.reply = htons(i);
|
reply.reply = htons(i);
|
||||||
reply.status = STT_SUCCESS;
|
reply.status = STT_SUCCESS;
|
||||||
reply_length = PKL_ReplyLength(&reply);
|
reply_length = PKL_ReplyLength(&reply);
|
||||||
if ((reply_length && reply_length < offsetof(CMD_Reply, data)) ||
|
BRIEF_ASSERT((reply_length == 0 || reply_length >= offsetof(CMD_Reply, data)) &&
|
||||||
reply_length > sizeof (CMD_Reply))
|
reply_length <= sizeof (CMD_Reply));
|
||||||
assert(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
3
conf.c
3
conf.c
@@ -2828,8 +2828,7 @@ CNF_GetNtsTrustedCertsPaths(const char ***paths, uint32_t **ids)
|
|||||||
*paths = ARR_GetElements(nts_trusted_certs_paths);
|
*paths = ARR_GetElements(nts_trusted_certs_paths);
|
||||||
*ids = ARR_GetElements(nts_trusted_certs_ids);
|
*ids = ARR_GetElements(nts_trusted_certs_ids);
|
||||||
|
|
||||||
if (ARR_GetSize(nts_trusted_certs_paths) != ARR_GetSize(nts_trusted_certs_ids))
|
BRIEF_ASSERT(ARR_GetSize(nts_trusted_certs_paths) == ARR_GetSize(nts_trusted_certs_ids));
|
||||||
assert(0);
|
|
||||||
|
|
||||||
return ARR_GetSize(nts_trusted_certs_paths);
|
return ARR_GetSize(nts_trusted_certs_paths);
|
||||||
}
|
}
|
||||||
|
|||||||
14
local.c
14
local.c
@@ -184,10 +184,8 @@ void
|
|||||||
LCL_Finalise(void)
|
LCL_Finalise(void)
|
||||||
{
|
{
|
||||||
/* Make sure all handlers have been removed */
|
/* Make sure all handlers have been removed */
|
||||||
if (change_list.next != &change_list)
|
BRIEF_ASSERT(change_list.next == &change_list);
|
||||||
assert(0);
|
BRIEF_ASSERT(dispersion_notify_list.next == &dispersion_notify_list);
|
||||||
if (dispersion_notify_list.next != &dispersion_notify_list)
|
|
||||||
assert(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ================================================== */
|
/* ================================================== */
|
||||||
@@ -225,9 +223,7 @@ LCL_AddParameterChangeHandler(LCL_ParameterChangeHandler handler, void *anything
|
|||||||
|
|
||||||
/* Check that the handler is not already registered */
|
/* Check that the handler is not already registered */
|
||||||
for (ptr = change_list.next; ptr != &change_list; ptr = ptr->next) {
|
for (ptr = change_list.next; ptr != &change_list; ptr = ptr->next) {
|
||||||
if (!(ptr->handler != handler || ptr->anything != anything)) {
|
BRIEF_ASSERT(ptr->handler != handler || ptr->anything != anything);
|
||||||
assert(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new_entry = MallocNew(ChangeListEntry);
|
new_entry = MallocNew(ChangeListEntry);
|
||||||
@@ -301,9 +297,7 @@ LCL_AddDispersionNotifyHandler(LCL_DispersionNotifyHandler handler, void *anythi
|
|||||||
|
|
||||||
/* Check that the handler is not already registered */
|
/* Check that the handler is not already registered */
|
||||||
for (ptr = dispersion_notify_list.next; ptr != &dispersion_notify_list; ptr = ptr->next) {
|
for (ptr = dispersion_notify_list.next; ptr != &dispersion_notify_list; ptr = ptr->next) {
|
||||||
if (!(ptr->handler != handler || ptr->anything != anything)) {
|
BRIEF_ASSERT(ptr->handler != handler || ptr->anything != anything);
|
||||||
assert(0);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
new_entry = MallocNew(DispersionNotifyListEntry);
|
new_entry = MallocNew(DispersionNotifyListEntry);
|
||||||
|
|||||||
@@ -2337,9 +2337,8 @@ process_response(NCR_Instance inst, int saved, NTP_Local_Address *local_addr,
|
|||||||
inst->valid_rx = 1;
|
inst->valid_rx = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((unsigned int)local_receive.source >= sizeof (tss_chars) ||
|
BRIEF_ASSERT((unsigned int)local_receive.source < sizeof (tss_chars) &&
|
||||||
(unsigned int)local_transmit.source >= sizeof (tss_chars))
|
(unsigned int)local_transmit.source < sizeof (tss_chars));
|
||||||
assert(0);
|
|
||||||
|
|
||||||
DEBUG_LOG("NTP packet lvm=%o stratum=%d poll=%d prec=%d root_delay=%.9f root_disp=%.9f refid=%"PRIx32" [%s]",
|
DEBUG_LOG("NTP packet lvm=%o stratum=%d poll=%d prec=%d root_delay=%.9f root_disp=%.9f refid=%"PRIx32" [%s]",
|
||||||
message->lvm, message->stratum, message->poll, message->precision,
|
message->lvm, message->stratum, message->poll, message->precision,
|
||||||
|
|||||||
@@ -457,9 +457,8 @@ change_source_address(NTP_Remote_Address *old_addr, NTP_Remote_Address *new_addr
|
|||||||
if (replacement)
|
if (replacement)
|
||||||
record->resolved_addr = new_addr->ip_addr;
|
record->resolved_addr = new_addr->ip_addr;
|
||||||
|
|
||||||
if (record->remote_addr != NCR_GetRemoteAddress(record->data) ||
|
BRIEF_ASSERT(record->remote_addr == NCR_GetRemoteAddress(record->data) &&
|
||||||
UTI_CompareIPs(&record->remote_addr->ip_addr, &new_addr->ip_addr, NULL) != 0)
|
UTI_CompareIPs(&record->remote_addr->ip_addr, &new_addr->ip_addr, NULL) == 0);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
if (!UTI_IsIPReal(&old_addr->ip_addr) && UTI_IsIPReal(&new_addr->ip_addr)) {
|
if (!UTI_IsIPReal(&old_addr->ip_addr) && UTI_IsIPReal(&new_addr->ip_addr)) {
|
||||||
if (auto_start_sources)
|
if (auto_start_sources)
|
||||||
|
|||||||
@@ -520,8 +520,7 @@ generate_key(int index)
|
|||||||
ServerKey *key;
|
ServerKey *key;
|
||||||
int key_length;
|
int key_length;
|
||||||
|
|
||||||
if (index < 0 || index >= MAX_SERVER_KEYS)
|
BRIEF_ASSERT(index >= 0 && index < MAX_SERVER_KEYS);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
/* Prefer AES-128-GCM-SIV if available. Note that if older keys loaded
|
/* Prefer AES-128-GCM-SIV if available. Note that if older keys loaded
|
||||||
from ntsdumpdir use a different algorithm, responding to NTP requests
|
from ntsdumpdir use a different algorithm, responding to NTP requests
|
||||||
@@ -534,8 +533,7 @@ generate_key(int index)
|
|||||||
key = &server_keys[index];
|
key = &server_keys[index];
|
||||||
|
|
||||||
key_length = SIV_GetKeyLength(algorithm);
|
key_length = SIV_GetKeyLength(algorithm);
|
||||||
if (key_length > sizeof (key->key))
|
BRIEF_ASSERT(key_length <= sizeof (key->key));
|
||||||
assert(0);
|
|
||||||
|
|
||||||
UTI_GetRandomBytesUrandom(key->key, key_length);
|
UTI_GetRandomBytesUrandom(key->key, key_length);
|
||||||
memset(key->key + key_length, 0, sizeof (key->key) - key_length);
|
memset(key->key + key_length, 0, sizeof (key->key) - key_length);
|
||||||
@@ -961,8 +959,7 @@ NKS_GenerateCookie(NKE_Context *context, NKE_Cookie *cookie)
|
|||||||
header->key_id = htonl(key->id);
|
header->key_id = htonl(key->id);
|
||||||
|
|
||||||
nonce = cookie->cookie + sizeof (*header);
|
nonce = cookie->cookie + sizeof (*header);
|
||||||
if (key->nonce_length > sizeof (cookie->cookie) - sizeof (*header))
|
BRIEF_ASSERT(key->nonce_length <= sizeof (cookie->cookie) - sizeof (*header));
|
||||||
assert(0);
|
|
||||||
UTI_GetRandomBytes(nonce, key->nonce_length);
|
UTI_GetRandomBytes(nonce, key->nonce_length);
|
||||||
|
|
||||||
plaintext_length = context->c2s.length + context->s2c.length;
|
plaintext_length = context->c2s.length + context->s2c.length;
|
||||||
|
|||||||
@@ -663,8 +663,7 @@ create_credentials(const char **certs, const char **keys, int n_certs_keys,
|
|||||||
goto error;
|
goto error;
|
||||||
|
|
||||||
if (certs && keys) {
|
if (certs && keys) {
|
||||||
if (trusted_certs || trusted_certs_ids)
|
BRIEF_ASSERT(!trusted_certs && !trusted_certs_ids);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
for (i = 0; i < n_certs_keys; i++) {
|
for (i = 0; i < n_certs_keys; i++) {
|
||||||
if (!UTI_CheckFilePermissions(keys[i], 0771))
|
if (!UTI_CheckFilePermissions(keys[i], 0771))
|
||||||
@@ -675,8 +674,7 @@ create_credentials(const char **certs, const char **keys, int n_certs_keys,
|
|||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (certs || keys || n_certs_keys > 0)
|
BRIEF_ASSERT(!certs && !keys && n_certs_keys <= 0);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
if (trusted_cert_set == 0 && !CNF_GetNoSystemCert()) {
|
if (trusted_cert_set == 0 && !CNF_GetNoSystemCert()) {
|
||||||
r = gnutls_certificate_set_x509_system_trust(credentials);
|
r = gnutls_certificate_set_x509_system_trust(credentials);
|
||||||
|
|||||||
@@ -104,9 +104,8 @@ NNA_GenerateAuthEF(NTP_Packet *packet, NTP_PacketInfo *info, SIV_Instance siv,
|
|||||||
body = (unsigned char *)(header + 1);
|
body = (unsigned char *)(header + 1);
|
||||||
ciphertext = body + nonce_length + nonce_padding;
|
ciphertext = body + nonce_length + nonce_padding;
|
||||||
|
|
||||||
if ((unsigned char *)header + auth_length !=
|
BRIEF_ASSERT((unsigned char *)header + auth_length ==
|
||||||
ciphertext + ciphertext_length + ciphertext_padding + additional_padding)
|
ciphertext + ciphertext_length + ciphertext_padding + additional_padding);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
memcpy(body, nonce, nonce_length);
|
memcpy(body, nonce, nonce_length);
|
||||||
memset(body + nonce_length, 0, nonce_padding);
|
memset(body + nonce_length, 0, nonce_padding);
|
||||||
|
|||||||
@@ -259,8 +259,7 @@ NNS_GenerateResponseAuth(NTP_Packet *request, NTP_PacketInfo *req_info,
|
|||||||
|
|
||||||
/* Make sure this is a response to the request from the last call
|
/* Make sure this is a response to the request from the last call
|
||||||
of NNS_CheckRequestAuth() */
|
of NNS_CheckRequestAuth() */
|
||||||
if (UTI_CompareNtp64(&server->req_tx, &request->transmit_ts) != 0)
|
BRIEF_ASSERT(UTI_CompareNtp64(&server->req_tx, &request->transmit_ts) == 0);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
for (parsed = NTP_HEADER_LENGTH; parsed < req_info->length; parsed += ef_length) {
|
for (parsed = NTP_HEADER_LENGTH; parsed < req_info->length; parsed += ef_length) {
|
||||||
if (!NEF_ParseField(request, req_info->length, parsed,
|
if (!NEF_ParseField(request, req_info->length, parsed,
|
||||||
|
|||||||
@@ -547,9 +547,9 @@ PRV_BindSocket(int sock, struct sockaddr *address, socklen_t address_len)
|
|||||||
PrvResponse res;
|
PrvResponse res;
|
||||||
|
|
||||||
SCK_SockaddrToIPSockAddr(address, address_len, &ip_saddr);
|
SCK_SockaddrToIPSockAddr(address, address_len, &ip_saddr);
|
||||||
if (ip_saddr.port != 0 && ip_saddr.port != CNF_GetNTPPort() &&
|
BRIEF_ASSERT(ip_saddr.port == 0 || ip_saddr.port == CNF_GetNTPPort() ||
|
||||||
ip_saddr.port != CNF_GetAcquisitionPort() && ip_saddr.port != CNF_GetPtpPort())
|
ip_saddr.port == CNF_GetAcquisitionPort() ||
|
||||||
assert(0);
|
ip_saddr.port == CNF_GetPtpPort());
|
||||||
|
|
||||||
if (!have_helper())
|
if (!have_helper())
|
||||||
return bind(sock, address, address_len);
|
return bind(sock, address, address_len);
|
||||||
|
|||||||
11
quantiles.c
11
quantiles.c
@@ -62,9 +62,8 @@ QNT_CreateInstance(int min_k, int max_k, int q, int repeat,
|
|||||||
QNT_Instance inst;
|
QNT_Instance inst;
|
||||||
long seed;
|
long seed;
|
||||||
|
|
||||||
if (q < 2 || min_k > max_k || min_k < 1 || max_k >= q ||
|
BRIEF_ASSERT(q >= 2 && min_k <= max_k && min_k >= 1 && max_k < q && repeat >= 1 &&
|
||||||
repeat < 1 || repeat > MAX_REPEAT || min_step <= 0.0 || large_step_delay < 0)
|
repeat <= MAX_REPEAT && min_step > 0.0 && large_step_delay >= 0);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
inst = MallocNew(struct QNT_Instance_Record);
|
inst = MallocNew(struct QNT_Instance_Record);
|
||||||
inst->n_quants = (max_k - min_k + 1) * repeat;
|
inst->n_quants = (max_k - min_k + 1) * repeat;
|
||||||
@@ -117,8 +116,7 @@ insert_initial_value(QNT_Instance inst, double value)
|
|||||||
{
|
{
|
||||||
int i, j, r = inst->repeat;
|
int i, j, r = inst->repeat;
|
||||||
|
|
||||||
if (inst->n_set * r >= inst->n_quants)
|
BRIEF_ASSERT(inst->n_set * r < inst->n_quants);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
/* Keep the initial estimates repeated and ordered */
|
/* Keep the initial estimates repeated and ordered */
|
||||||
for (i = inst->n_set; i > 0 && inst->quants[(i - 1) * r].est > value; i--) {
|
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)
|
|||||||
double estimates[MAX_REPEAT];
|
double estimates[MAX_REPEAT];
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (k < inst->min_k || (k - inst->min_k) * inst->repeat >= inst->n_quants)
|
BRIEF_ASSERT(k >= inst->min_k && (k - inst->min_k) * inst->repeat < inst->n_quants);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
for (i = 0; i < inst->repeat; i++)
|
for (i = 0; i < inst->repeat; i++)
|
||||||
estimates[i] = inst->quants[(k - inst->min_k) * inst->repeat + i].est;
|
estimates[i] = inst->quants[(k - inst->min_k) * inst->repeat + i].est;
|
||||||
|
|||||||
@@ -35,6 +35,7 @@
|
|||||||
|
|
||||||
#include "memory.h"
|
#include "memory.h"
|
||||||
#include "siv.h"
|
#include "siv.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
struct SIV_Instance_Record {
|
struct SIV_Instance_Record {
|
||||||
SIV_Algorithm algorithm;
|
SIV_Algorithm algorithm;
|
||||||
@@ -158,8 +159,7 @@ SIV_GetMaxNonceLength(SIV_Instance instance)
|
|||||||
int
|
int
|
||||||
SIV_GetTagLength(SIV_Instance instance)
|
SIV_GetTagLength(SIV_Instance instance)
|
||||||
{
|
{
|
||||||
if (instance->tag_length < 1 || instance->tag_length > SIV_MAX_TAG_LENGTH)
|
BRIEF_ASSERT(instance->tag_length >= 1 && instance->tag_length <= SIV_MAX_TAG_LENGTH);
|
||||||
assert(0);
|
|
||||||
return instance->tag_length;
|
return instance->tag_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
5
socket.c
5
socket.c
@@ -1061,9 +1061,8 @@ receive_messages(int sock_fd, int flags, int max_messages, int *num_messages)
|
|||||||
n = ARR_GetSize(recv_headers);
|
n = ARR_GetSize(recv_headers);
|
||||||
n = MIN(n, max_messages);
|
n = MIN(n, max_messages);
|
||||||
|
|
||||||
if (n < 1 || n > MAX_RECV_MESSAGES ||
|
BRIEF_ASSERT(n >= 1 && n <= MAX_RECV_MESSAGES &&
|
||||||
n > ARR_GetSize(recv_messages) || n > ARR_GetSize(recv_sck_messages))
|
n <= ARR_GetSize(recv_messages) && n <= ARR_GetSize(recv_sck_messages));
|
||||||
assert(0);
|
|
||||||
|
|
||||||
recv_flags = get_recv_flags(flags);
|
recv_flags = get_recv_flags(flags);
|
||||||
|
|
||||||
|
|||||||
@@ -322,9 +322,8 @@ void SRC_DestroyInstance(SRC_Instance instance)
|
|||||||
last_updated_inst = NULL;
|
last_updated_inst = NULL;
|
||||||
|
|
||||||
assert(initialised);
|
assert(initialised);
|
||||||
if (instance->index < 0 || instance->index >= n_sources ||
|
BRIEF_ASSERT(instance->index >= 0 && instance->index < n_sources &&
|
||||||
instance != sources[instance->index])
|
instance == sources[instance->index]);
|
||||||
assert(0);
|
|
||||||
|
|
||||||
SST_DeleteInstance(instance->stats);
|
SST_DeleteInstance(instance->stats);
|
||||||
dead_index = instance->index;
|
dead_index = instance->index;
|
||||||
@@ -763,8 +762,7 @@ mark_source(SRC_Instance inst, SRC_Status status)
|
|||||||
{
|
{
|
||||||
set_source_status(inst, status);
|
set_source_status(inst, status);
|
||||||
|
|
||||||
if (status < SRC_OK || status >= sizeof (inst->reported_status))
|
BRIEF_ASSERT(status >= SRC_OK && status < sizeof (inst->reported_status));
|
||||||
assert(0);
|
|
||||||
|
|
||||||
if (!inst->reported_status[status]) {
|
if (!inst->reported_status[status]) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
|
|||||||
8
util.h
8
util.h
@@ -272,4 +272,12 @@ extern int UTI_SplitString(char *string, char **words, int max_saved_words);
|
|||||||
|
|
||||||
#define SQUARE(x) ((x) * (x))
|
#define SQUARE(x) ((x) * (x))
|
||||||
|
|
||||||
|
/* Macro to make an assertion with the text of a long argument replaced
|
||||||
|
with "0" to avoid bloating the compiled binary */
|
||||||
|
#ifdef NDEBUG
|
||||||
|
#define BRIEF_ASSERT(a)
|
||||||
|
#else
|
||||||
|
#define BRIEF_ASSERT(a) if (!(a)) assert(0)
|
||||||
|
#endif
|
||||||
|
|
||||||
#endif /* GOT_UTIL_H */
|
#endif /* GOT_UTIL_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user