Skip to content

Commit

Permalink
cleanup: Return boolean constants, not ints from bool functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 22, 2022
1 parent b7f95bb commit 6be655c
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 165 deletions.
2 changes: 1 addition & 1 deletion other/bootstrap_daemon/docker/tox-bootstrapd.sha256
Original file line number Diff line number Diff line change
@@ -1 +1 @@
65261078cb22504cf84f16d65cbdb0bd3e297bd11f4e801d4127499aab283e96 /usr/local/bin/tox-bootstrapd
30dec481315934168e3b8abf54fe4ed5814161230bd499e0f935a4df1922dd33 /usr/local/bin/tox-bootstrapd
1 change: 1 addition & 0 deletions testing/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ sh_test(
"-Wno-callback-names",
"-Wno-enum-names",
"-Wno-memcpy-structs",
"-Wno-type-check",
"+RTS",
"-N3",
],
Expand Down
14 changes: 3 additions & 11 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -1113,7 +1113,7 @@ static bool is_pk_in_client_list(const Client_data *list, unsigned int client_li
const uint32_t index = index_of_client_pk(list, client_list_length, public_key);

if (index == UINT32_MAX) {
return 0;
return false;
}

const IPPTsPng *assoc = net_family_is_ipv4(ip_port->ip.family)
Expand Down Expand Up @@ -1355,11 +1355,7 @@ bool dht_getnodes(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key, c
return false;
}

if (sendpacket(dht->net, ip_port, data, len) > 0) {
return true;
}

return false;
return sendpacket(dht->net, ip_port, data, len) > 0;
}

/** Send a send nodes response: message for IPv6 nodes */
Expand Down Expand Up @@ -1467,11 +1463,7 @@ static bool sent_getnode_to_node(DHT *dht, const uint8_t *public_key, const IP_P
return false;
}

if (!ipport_equal(&test.ip_port, node_ip_port) || !id_equal(test.public_key, public_key)) {
return false;
}

return true;
return ipport_equal(&test.ip_port, node_ip_port) && id_equal(test.public_key, public_key);
}

non_null()
Expand Down
12 changes: 2 additions & 10 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,7 @@ bool ip_is_local(const IP *ip)
}

/* localhost in IPv6 (::1) */
if (ip->ip.v6.uint64[0] == 0 && ip->ip.v6.uint32[2] == 0 && ip->ip.v6.uint32[3] == net_htonl(1)) {
return true;
}

return false;
return ip->ip.v6.uint64[0] == 0 && ip->ip.v6.uint32[2] == 0 && ip->ip.v6.uint32[3] == net_htonl(1);
}

non_null()
Expand Down Expand Up @@ -304,11 +300,7 @@ static bool ip4_is_lan(const IP4 *ip4)

/* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10)
* (shared address space to stack another layer of NAT) */
if ((ip4->uint8[0] == 100) && ((ip4->uint8[1] & 0xC0) == 0x40)) {
return true;
}

return false;
return (ip4->uint8[0] == 100) && ((ip4->uint8[1] & 0xC0) == 0x40);
}

bool ip_is_lan(const IP *ip)
Expand Down
16 changes: 4 additions & 12 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ static bool connections_number_is_valid(const TCP_Connections *tcp_c, int connec
return false;
}

if (tcp_c->connections[connections_number].status == TCP_CONN_NONE) {
return false;
}

return true;
return tcp_c->connections[connections_number].status != TCP_CONN_NONE;
}

/**
Expand All @@ -139,11 +135,7 @@ static bool tcp_connections_number_is_valid(const TCP_Connections *tcp_c, int tc
return false;
}

if (tcp_c->tcp_connections[tcp_connections_number].status == TCP_CONN_NONE) {
return false;
}

return true;
return tcp_c->tcp_connections[tcp_connections_number].status != TCP_CONN_NONE;
}

/** Create a new empty connection.
Expand Down Expand Up @@ -680,11 +672,11 @@ static bool tcp_connection_in_conn(const TCP_Connection_to *con_to, unsigned int
{
for (unsigned int i = 0; i < MAX_FRIEND_TCP_CONNECTIONS; ++i) {
if (con_to->connections[i].tcp_connection == (tcp_connections_number + 1)) {
return 1;
return true;
}
}

return 0;
return false;
}

/** return index on success.
Expand Down
13 changes: 8 additions & 5 deletions toxcore/crypto_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
#include "crypto_core.h"

#include <assert.h>
#include <stdlib.h>
#include <string.h>

Expand All @@ -34,7 +35,6 @@

//!TOKSTYLE-
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
#include <assert.h>
#include "../testing/fuzzing/fuzz_adapter.h"
#endif
//!TOKSTYLE+
Expand Down Expand Up @@ -178,10 +178,10 @@ uint32_t random_range_u32(uint32_t upper_bound)
bool public_key_valid(const uint8_t *public_key)
{
if (public_key[31] >= 128) { /* Last bit of key is always zero. */
return 0;
return false;
}

return 1;
return true;
}

int32_t encrypt_precompute(const uint8_t *public_key, const uint8_t *secret_key,
Expand Down Expand Up @@ -238,7 +238,8 @@ int32_t encrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
crypto_free(temp_plain, size_temp_plain);
crypto_free(temp_encrypted, size_temp_encrypted);
#endif
return length + crypto_box_MACBYTES;
assert(length < INT32_MAX - crypto_box_MACBYTES);
return (int32_t)(length + crypto_box_MACBYTES);
}

int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
Expand Down Expand Up @@ -287,7 +288,9 @@ int32_t decrypt_data_symmetric(const uint8_t *shared_key, const uint8_t *nonce,
crypto_free(temp_plain, size_temp_plain);
crypto_free(temp_encrypted, size_temp_encrypted);
#endif
return length - crypto_box_MACBYTES;
assert(length > crypto_box_MACBYTES);
assert(length < INT32_MAX);
return (int32_t)(length - crypto_box_MACBYTES);
}

int32_t encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uint8_t *nonce,
Expand Down
34 changes: 7 additions & 27 deletions toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,7 @@ static bool send_packet_group_peer(const Friend_Connections *fr_c, int friendcon
uint16_t group_num, const uint8_t *data, uint16_t length)
{
if (1 + sizeof(uint16_t) + length > MAX_CRYPTO_DATA_SIZE) {
return 0;
return false;
}

group_num = net_htons(group_num);
Expand All @@ -1463,7 +1463,7 @@ static bool send_lossy_group_peer(const Friend_Connections *fr_c, int friendcon_
uint16_t group_num, const uint8_t *data, uint16_t length)
{
if (1 + sizeof(uint16_t) + length > MAX_CRYPTO_DATA_SIZE) {
return 0;
return false;
}

group_num = net_htons(group_num);
Expand Down Expand Up @@ -1757,11 +1757,7 @@ static int send_message_group(const Group_Chats *g_c, uint32_t groupnumber, uint
non_null()
static bool group_ping_send(const Group_Chats *g_c, uint32_t groupnumber)
{
if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, nullptr, 0) > 0) {
return true;
}

return false;
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_PING_ID, nullptr, 0) > 0;
}

/** send a new_peer message
Expand All @@ -1778,11 +1774,7 @@ static bool group_new_peer_send(const Group_Chats *g_c, uint32_t groupnumber, ui
memcpy(packet + sizeof(uint16_t), real_pk, CRYPTO_PUBLIC_KEY_SIZE);
memcpy(packet + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE, temp_pk, CRYPTO_PUBLIC_KEY_SIZE);

if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_NEW_PEER_ID, packet, sizeof(packet)) > 0) {
return true;
}

return false;
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_NEW_PEER_ID, packet, sizeof(packet)) > 0;
}

/** send a kill_peer message
Expand All @@ -1796,11 +1788,7 @@ static bool group_kill_peer_send(const Group_Chats *g_c, uint32_t groupnumber, u
peer_num = net_htons(peer_num);
memcpy(packet, &peer_num, sizeof(uint16_t));

if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_KILL_PEER_ID, packet, sizeof(packet)) > 0) {
return true;
}

return false;
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_KILL_PEER_ID, packet, sizeof(packet)) > 0;
}

/** send a freeze_peer message
Expand All @@ -1814,11 +1802,7 @@ static bool group_freeze_peer_send(const Group_Chats *g_c, uint32_t groupnumber,
peer_num = net_htons(peer_num);
memcpy(packet, &peer_num, sizeof(uint16_t));

if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_FREEZE_PEER_ID, packet, sizeof(packet)) > 0) {
return true;
}

return false;
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_FREEZE_PEER_ID, packet, sizeof(packet)) > 0;
}

/** send a name message
Expand All @@ -1831,11 +1815,7 @@ static bool group_name_send(const Group_Chats *g_c, uint32_t groupnumber, const
return false;
}

if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_NAME_ID, nick, nick_len) > 0) {
return true;
}

return false;
return send_message_group(g_c, groupnumber, GROUP_MESSAGE_NAME_ID, nick, nick_len) > 0;
}

/** send message to announce leaving group
Expand Down
6 changes: 1 addition & 5 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,11 +188,7 @@ static bool crypt_connection_id_is_valid(const Net_Crypto *c, int crypt_connecti

const Crypto_Conn_State status = c->crypto_connections[crypt_connection_id].status;

if (status == CRYPTO_CONN_NO_CONNECTION || status == CRYPTO_CONN_FREE) {
return false;
}

return true;
return status != CRYPTO_CONN_NO_CONNECTION && status != CRYPTO_CONN_FREE;
}

/** cookie timeout in seconds */
Expand Down
2 changes: 1 addition & 1 deletion toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ non_null()
static bool path_exists(const Mono_Time *mono_time, const Onion_Client_Paths *onion_paths, uint32_t path_num)
{
if (path_timed_out(mono_time, onion_paths, path_num)) {
return 0;
return false;
}

return onion_paths->paths[path_num % NUMBER_ONION_PATHS].path_num == path_num;
Expand Down
Loading

0 comments on commit 6be655c

Please sign in to comment.