Skip to content

Commit

Permalink
cleanup: Add more const where possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 31, 2024
1 parent 511bfe3 commit f70e588
Show file tree
Hide file tree
Showing 13 changed files with 33 additions and 33 deletions.
4 changes: 2 additions & 2 deletions other/DHT_bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static bool manage_keys(DHT *dht)
if (keys_file != nullptr) {
/* If file was opened successfully -- load keys,
otherwise save new keys */
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
const size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);

if (read_size != KEYS_SIZE) {
printf("Error while reading the key file\nExiting.\n");
Expand Down Expand Up @@ -134,7 +134,7 @@ int main(int argc, char *argv[])

/* let user override default by cmdline */
bool ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);
const int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);

if (argvoffset < 0) {
return 1;
Expand Down
10 changes: 5 additions & 5 deletions other/bootstrap_daemon/src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS);
log_write(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS);

uint16_t default_ports[] = {DEFAULT_TCP_RELAY_PORTS};
const uint16_t default_ports[] = {DEFAULT_TCP_RELAY_PORTS};

// Check to avoid calling malloc(0) later on
// NOLINTNEXTLINE, clang-tidy: error: suspicious comparison of 'sizeof(expr)' to a constant [bugprone-sizeof-expression,-warnings-as-errors]
Expand Down Expand Up @@ -90,7 +90,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
return;
}

int config_port_count = config_setting_length(ports_array);
const int config_port_count = config_setting_length(ports_array);

if (config_port_count == 0) {
log_write(LOG_LEVEL_ERROR, "'%s' is empty.\n", NAME_TCP_RELAY_PORTS);
Expand Down Expand Up @@ -247,8 +247,8 @@ int get_general_config(const char *cfg_file_path, char **pid_file_path, char **k
tmp_motd = DEFAULT_MOTD;
}

size_t tmp_motd_length = strlen(tmp_motd) + 1;
size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
const size_t tmp_motd_length = strlen(tmp_motd) + 1;
const size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
*motd = (char *)malloc(motd_length);
snprintf(*motd, motd_length, "%s", tmp_motd);
}
Expand Down Expand Up @@ -302,7 +302,7 @@ static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string)
return nullptr;
}

size_t len = strlen(hex_string) / 2;
const size_t len = strlen(hex_string) / 2;
uint8_t *ret = (uint8_t *)malloc(len);

const char *pos = hex_string;
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/src/log_backend_syslog.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args)
va_list args2;

va_copy(args2, args);
int size = vsnprintf(nullptr, 0, format, args2);
const int size = vsnprintf(nullptr, 0, format, args2);
va_end(args2);

assert(size >= 0);
Expand Down
2 changes: 1 addition & 1 deletion other/bootstrap_node_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static int handle_info_request(void *object, const IP_Port *source, const uint8_
uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH];
data[0] = BOOTSTRAP_INFO_PACKET_ID;
memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version));
uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length;
const uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length;
memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length);

if (sendpacket(nc, source, data, len) == len) {
Expand Down
6 changes: 3 additions & 3 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, cons
*/
uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key)
{
IP_Port ipp_copy = ip_port_normalize(ip_port);
const IP_Port ipp_copy = ip_port_normalize(ip_port);

uint32_t used = 0;

Expand Down Expand Up @@ -1249,7 +1249,7 @@ static bool update_client_data(const Mono_Time *mono_time, Client_data *array, s
non_null()
static void returnedip_ports(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, const uint8_t *nodepublic_key)
{
IP_Port ipp_copy = ip_port_normalize(ip_port);
const IP_Port ipp_copy = ip_port_normalize(ip_port);

if (pk_equal(public_key, dht->self_public_key)) {
update_client_data(dht->mono_time, dht->close_clientlist, LCLIENT_LIST, &ipp_copy, nodepublic_key, true);
Expand Down Expand Up @@ -2293,7 +2293,7 @@ static void punch_holes(DHT *dht, const IP *ip, const uint16_t *port_list, uint1

uint16_t i;
for (i = 0; i < MAX_PUNCHING_PORTS; ++i) {
uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2;
const uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2;

Check warning on line 2296 in toxcore/DHT.c

View check run for this annotation

Codecov / codecov/patch

toxcore/DHT.c#L2296

Added line #L2296 was not covered by tests
const uint16_t port = 1024;
pinging.port = net_htons(port + it);
ping_send_request(dht->ping, &pinging, dht->friends_list[friend_num].public_key);
Expand Down
8 changes: 4 additions & 4 deletions toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,16 +139,16 @@ void getaddress(const Messenger *m, uint8_t *address)
non_null()
static bool send_online_packet(Messenger *m, int friendcon_id)
{
uint8_t packet = PACKET_ID_ONLINE;
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
const uint8_t packet[1] = {PACKET_ID_ONLINE};
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
sizeof(packet), false) != -1;
}

non_null()
static bool send_offline_packet(Messenger *m, int friendcon_id)
{
uint8_t packet = PACKET_ID_OFFLINE;
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
const uint8_t packet[1] = {PACKET_ID_OFFLINE};
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,

Check warning on line 151 in toxcore/Messenger.c

View check run for this annotation

Codecov / codecov/patch

toxcore/Messenger.c#L150-L151

Added lines #L150 - L151 were not covered by tests
sizeof(packet), false) != -1;
}

Expand Down
6 changes: 3 additions & 3 deletions toxcore/TCP_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ static int proxy_socks5_read_connection_response(const Logger *logger, const TCP
} else {
uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)];
const TCP_Connection *con = &tcp_conn->con;
int ret = read_tcp_packet(logger, con->mem, con->ns, con->sock, data, sizeof(data), &con->ip_port);
const int ret = read_tcp_packet(logger, con->mem, con->ns, con->sock, data, sizeof(data), &con->ip_port);

Check warning on line 288 in toxcore/TCP_client.c

View check run for this annotation

Codecov / codecov/patch

toxcore/TCP_client.c#L288

Added line #L288 was not covered by tests

if (ret == -1) {
return 0;
Expand Down Expand Up @@ -952,7 +952,7 @@ void do_tcp_connection(const Logger *logger, const Mono_Time *mono_time,

if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_CONNECTING) {
if (send_pending_data(logger, &tcp_connection->con) == 0) {
int ret = socks5_read_handshake_response(logger, tcp_connection);
const int ret = socks5_read_handshake_response(logger, tcp_connection);

if (ret == -1) {
tcp_connection->kill_at = 0;
Expand All @@ -968,7 +968,7 @@ void do_tcp_connection(const Logger *logger, const Mono_Time *mono_time,

if (tcp_connection->status == TCP_CLIENT_PROXY_SOCKS5_UNCONFIRMED) {
if (send_pending_data(logger, &tcp_connection->con) == 0) {
int ret = proxy_socks5_read_connection_response(logger, tcp_connection);
const int ret = proxy_socks5_read_connection_response(logger, tcp_connection);

if (ret == -1) {
tcp_connection->kill_at = 0;
Expand Down
4 changes: 2 additions & 2 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ int set_tcp_connection_to_status(const TCP_Connections *tcp_c, int connections_n

for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection > 0) {
unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
const unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;

Check warning on line 772 in toxcore/TCP_connection.c

View check run for this annotation

Codecov / codecov/patch

toxcore/TCP_connection.c#L772

Added line #L772 was not covered by tests
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

if (tcp_con == nullptr) {
Expand Down Expand Up @@ -928,7 +928,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
return -1;
}

IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
const IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE];
memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
kill_tcp_connection(tcp_con->connection);
Expand Down
4 changes: 2 additions & 2 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ static int handle_tcp_handshake(const Logger *logger, TCP_Secure_Connection *con
return -1;
}

IP_Port ipp = {{{0}}};
const IP_Port ipp = {{{0}}};

if (TCP_SERVER_HANDSHAKE_SIZE != net_send(con->con.ns, logger, con->con.sock, response, TCP_SERVER_HANDSHAKE_SIZE, &ipp)) {
crypto_memzero(shared_key, sizeof(shared_key));
Expand Down Expand Up @@ -765,7 +765,7 @@ static int handle_tcp_packet(TCP_Server *tcp_server, uint32_t con_id, const uint
return -1;
}

IP_Port source = con_id_to_ip_port(con_id, con->identifier);
const IP_Port source = con_id_to_ip_port(con_id, con->identifier);
onion_send_1(tcp_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), &source,
data + 1);
}
Expand Down
2 changes: 1 addition & 1 deletion toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ static bool add_to_closest(Group_c *g, const uint8_t *real_pk, const uint8_t *te
comp_val = calculate_comp_value(real_pk, g->real_pk);

for (unsigned int i = DESIRED_CLOSEST / 2; i < DESIRED_CLOSEST; ++i) {
uint64_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk);
const uint64_t comp = calculate_comp_value(g->closest_peers[i].real_pk, g->real_pk);

if (comp > comp_val && comp > comp_d) {
index = i;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/group_chats.c
Original file line number Diff line number Diff line change
Expand Up @@ -2978,7 +2978,7 @@ static int handle_gc_shared_state(const GC_Session *c, GC_Chat *chat, GC_Connect
return 0;
}

GC_SharedState old_shared_state = chat->shared_state;
const GC_SharedState old_shared_state = chat->shared_state;
GC_SharedState new_shared_state;

if (unpack_gc_shared_state(&new_shared_state, ss_data, ss_length) == 0) {
Expand Down
12 changes: 6 additions & 6 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ static int send_packet_to(Net_Crypto *c, int crypt_connection_id, const uint8_t
bool direct_send_attempt = false;

pthread_mutex_lock(conn->mutex);
IP_Port ip_port = return_ip_port_connection(c, crypt_connection_id);
const IP_Port ip_port = return_ip_port_connection(c, crypt_connection_id);

// TODO(irungentoo): on bad networks, direct connections might not last indefinitely.
if (!net_family_is_unspec(ip_port.ip.family)) {
Expand Down Expand Up @@ -1508,9 +1508,9 @@ static int send_kill_packet(Net_Crypto *c, int crypt_connection_id)
return -1;
}

uint8_t kill_packet = PACKET_ID_KILL;
const uint8_t kill_packet[1] = {PACKET_ID_KILL};
return send_data_packet_helper(c, crypt_connection_id, conn->recv_array.buffer_start, conn->send_array.buffer_end,
&kill_packet, sizeof(kill_packet));
kill_packet, sizeof(kill_packet));
}

non_null(1) nullable(3)
Expand Down Expand Up @@ -1677,7 +1677,7 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const
}

if (rtt_calc_time != 0) {
uint64_t rtt_time = current_time_monotonic(c->mono_time) - rtt_calc_time;
const uint64_t rtt_time = current_time_monotonic(c->mono_time) - rtt_calc_time;

if (rtt_time < conn->rtt_time) {
conn->rtt_time = rtt_time;
Expand Down Expand Up @@ -2817,8 +2817,8 @@ static void send_crypto_packets(Net_Crypto *c)
1000.0);
n_packets += conn->last_packets_left_requested_rem;

uint32_t num_packets = n_packets;
double rem = n_packets - (double)num_packets;
const uint32_t num_packets = n_packets;
const double rem = n_packets - (double)num_packets;
conn->packets_left_requested = num_packets;

conn->last_packets_left_requested_set = temp_time;
Expand Down
4 changes: 2 additions & 2 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,7 @@ Networking_Core *new_networking_ex(

const int res = net_setsockopt(ns, temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq));

int neterror = net_error();
const int neterror = net_error();
char *strerror = net_new_strerror(neterror);

if (res < 0) {
Expand Down Expand Up @@ -1336,7 +1336,7 @@ Networking_Core *new_networking_ex(
}

Ip_Ntoa ip_str;
int neterror = net_error();
const int neterror = net_error();
char *strerror = net_new_strerror(neterror);
LOGGER_ERROR(log, "failed to bind socket: %d, %s IP: %s port_from: %u port_to: %u",
neterror, strerror, net_ip_ntoa(ip, &ip_str), port_from, port_to);
Expand Down

0 comments on commit f70e588

Please sign in to comment.