Skip to content

Commit f70e588

Browse files
committed
cleanup: Add more const where possible.
1 parent 511bfe3 commit f70e588

File tree

13 files changed

+33
-33
lines changed

13 files changed

+33
-33
lines changed

other/DHT_bootstrap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ static bool manage_keys(DHT *dht)
6161
if (keys_file != nullptr) {
6262
/* If file was opened successfully -- load keys,
6363
otherwise save new keys */
64-
size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
64+
const size_t read_size = fread(keys, sizeof(uint8_t), KEYS_SIZE, keys_file);
6565

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

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

139139
if (argvoffset < 0) {
140140
return 1;

other/bootstrap_daemon/src/config.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ static void parse_tcp_relay_ports_config(config_t *cfg, uint16_t **tcp_relay_por
4444
log_write(LOG_LEVEL_WARNING, "No '%s' setting in the configuration file.\n", NAME_TCP_RELAY_PORTS);
4545
log_write(LOG_LEVEL_WARNING, "Using default '%s':\n", NAME_TCP_RELAY_PORTS);
4646

47-
uint16_t default_ports[] = {DEFAULT_TCP_RELAY_PORTS};
47+
const uint16_t default_ports[] = {DEFAULT_TCP_RELAY_PORTS};
4848

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

93-
int config_port_count = config_setting_length(ports_array);
93+
const int config_port_count = config_setting_length(ports_array);
9494

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

250-
size_t tmp_motd_length = strlen(tmp_motd) + 1;
251-
size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
250+
const size_t tmp_motd_length = strlen(tmp_motd) + 1;
251+
const size_t motd_length = tmp_motd_length > MAX_MOTD_LENGTH ? MAX_MOTD_LENGTH : tmp_motd_length;
252252
*motd = (char *)malloc(motd_length);
253253
snprintf(*motd, motd_length, "%s", tmp_motd);
254254
}
@@ -302,7 +302,7 @@ static uint8_t *bootstrap_hex_string_to_bin(const char *hex_string)
302302
return nullptr;
303303
}
304304

305-
size_t len = strlen(hex_string) / 2;
305+
const size_t len = strlen(hex_string) / 2;
306306
uint8_t *ret = (uint8_t *)malloc(len);
307307

308308
const char *pos = hex_string;

other/bootstrap_daemon/src/log_backend_syslog.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void log_backend_syslog_write(LOG_LEVEL level, const char *format, va_list args)
5151
va_list args2;
5252

5353
va_copy(args2, args);
54-
int size = vsnprintf(nullptr, 0, format, args2);
54+
const int size = vsnprintf(nullptr, 0, format, args2);
5555
va_end(args2);
5656

5757
assert(size >= 0);

other/bootstrap_node_packets.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static int handle_info_request(void *object, const IP_Port *source, const uint8_
3535
uint8_t data[1 + sizeof(bootstrap_version) + MAX_MOTD_LENGTH];
3636
data[0] = BOOTSTRAP_INFO_PACKET_ID;
3737
memcpy(data + 1, &bootstrap_version, sizeof(bootstrap_version));
38-
uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length;
38+
const uint16_t len = 1 + sizeof(bootstrap_version) + bootstrap_motd_length;
3939
memcpy(data + 1 + sizeof(bootstrap_version), bootstrap_motd, bootstrap_motd_length);
4040

4141
if (sendpacket(nc, source, data, len) == len) {

toxcore/DHT.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, cons
11631163
*/
11641164
uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key)
11651165
{
1166-
IP_Port ipp_copy = ip_port_normalize(ip_port);
1166+
const IP_Port ipp_copy = ip_port_normalize(ip_port);
11671167

11681168
uint32_t used = 0;
11691169

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

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

22942294
uint16_t i;
22952295
for (i = 0; i < MAX_PUNCHING_PORTS; ++i) {
2296-
uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2;
2296+
const uint32_t it = i + dht->friends_list[friend_num].nat.punching_index2;
22972297
const uint16_t port = 1024;
22982298
pinging.port = net_htons(port + it);
22992299
ping_send_request(dht->ping, &pinging, dht->friends_list[friend_num].public_key);

toxcore/Messenger.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,16 @@ void getaddress(const Messenger *m, uint8_t *address)
139139
non_null()
140140
static bool send_online_packet(Messenger *m, int friendcon_id)
141141
{
142-
uint8_t packet = PACKET_ID_ONLINE;
143-
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
142+
const uint8_t packet[1] = {PACKET_ID_ONLINE};
143+
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
144144
sizeof(packet), false) != -1;
145145
}
146146

147147
non_null()
148148
static bool send_offline_packet(Messenger *m, int friendcon_id)
149149
{
150-
uint8_t packet = PACKET_ID_OFFLINE;
151-
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), &packet,
150+
const uint8_t packet[1] = {PACKET_ID_OFFLINE};
151+
return write_cryptpacket(m->net_crypto, friend_connection_crypt_connection_id(m->fr_c, friendcon_id), packet,
152152
sizeof(packet), false) != -1;
153153
}
154154

toxcore/TCP_client.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ static int proxy_socks5_read_connection_response(const Logger *logger, const TCP
285285
} else {
286286
uint8_t data[4 + sizeof(IP6) + sizeof(uint16_t)];
287287
const TCP_Connection *con = &tcp_conn->con;
288-
int ret = read_tcp_packet(logger, con->mem, con->ns, con->sock, data, sizeof(data), &con->ip_port);
288+
const int ret = read_tcp_packet(logger, con->mem, con->ns, con->sock, data, sizeof(data), &con->ip_port);
289289

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

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

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

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

973973
if (ret == -1) {
974974
tcp_connection->kill_at = 0;

toxcore/TCP_connection.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ int set_tcp_connection_to_status(const TCP_Connections *tcp_c, int connections_n
769769

770770
for (uint32_t i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
771771
if (con_to->connections[i].tcp_connection > 0) {
772-
unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
772+
const unsigned int tcp_connections_number = con_to->connections[i].tcp_connection - 1;
773773
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
774774

775775
if (tcp_con == nullptr) {
@@ -928,7 +928,7 @@ static int reconnect_tcp_relay_connection(TCP_Connections *tcp_c, int tcp_connec
928928
return -1;
929929
}
930930

931-
IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
931+
const IP_Port ip_port = tcp_con_ip_port(tcp_con->connection);
932932
uint8_t relay_pk[CRYPTO_PUBLIC_KEY_SIZE];
933933
memcpy(relay_pk, tcp_con_public_key(tcp_con->connection), CRYPTO_PUBLIC_KEY_SIZE);
934934
kill_tcp_connection(tcp_con->connection);

toxcore/TCP_server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static int handle_tcp_handshake(const Logger *logger, TCP_Secure_Connection *con
357357
return -1;
358358
}
359359

360-
IP_Port ipp = {{{0}}};
360+
const IP_Port ipp = {{{0}}};
361361

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

768-
IP_Port source = con_id_to_ip_port(con_id, con->identifier);
768+
const IP_Port source = con_id_to_ip_port(con_id, con->identifier);
769769
onion_send_1(tcp_server->onion, data + 1 + CRYPTO_NONCE_SIZE, length - (1 + CRYPTO_NONCE_SIZE), &source,
770770
data + 1);
771771
}

toxcore/group.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ static bool add_to_closest(Group_c *g, const uint8_t *real_pk, const uint8_t *te
489489
comp_val = calculate_comp_value(real_pk, g->real_pk);
490490

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

494494
if (comp > comp_val && comp > comp_d) {
495495
index = i;

0 commit comments

Comments
 (0)