From 6be655c56c08707954a94285d7a07b3c4a3aeab3 Mon Sep 17 00:00:00 2001 From: iphydf Date: Tue, 22 Feb 2022 16:44:39 +0000 Subject: [PATCH] cleanup: Return boolean constants, not ints from bool functions. --- .../docker/tox-bootstrapd.sha256 | 2 +- testing/BUILD.bazel | 1 + toxcore/DHT.c | 14 +- toxcore/LAN_discovery.c | 12 +- toxcore/TCP_connection.c | 16 +- toxcore/crypto_core.c | 13 +- toxcore/group.c | 34 +---- toxcore/net_crypto.c | 6 +- toxcore/onion_client.c | 2 +- toxcore/tox.c | 142 +++++++++--------- toxencryptsave/toxencryptsave.c | 32 ++-- 11 files changed, 109 insertions(+), 165 deletions(-) diff --git a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 index 5af8a296..761bd1ef 100644 --- a/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 +++ b/other/bootstrap_daemon/docker/tox-bootstrapd.sha256 @@ -1 +1 @@ -65261078cb22504cf84f16d65cbdb0bd3e297bd11f4e801d4127499aab283e96 /usr/local/bin/tox-bootstrapd +30dec481315934168e3b8abf54fe4ed5814161230bd499e0f935a4df1922dd33 /usr/local/bin/tox-bootstrapd diff --git a/testing/BUILD.bazel b/testing/BUILD.bazel index 10b2559a..ff8bcc01 100644 --- a/testing/BUILD.bazel +++ b/testing/BUILD.bazel @@ -16,6 +16,7 @@ sh_test( "-Wno-callback-names", "-Wno-enum-names", "-Wno-memcpy-structs", + "-Wno-type-check", "+RTS", "-N3", ], diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 9a8888d9..0bda38d7 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -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) @@ -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 */ @@ -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() diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 01afd44b..01c2adb2 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -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() @@ -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) diff --git a/toxcore/TCP_connection.c b/toxcore/TCP_connection.c index d1866cc1..e4ce872d 100644 --- a/toxcore/TCP_connection.c +++ b/toxcore/TCP_connection.c @@ -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; } /** @@ -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. @@ -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. diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index d1cb65ac..7bcf383e 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -10,6 +10,7 @@ */ #include "crypto_core.h" +#include #include #include @@ -34,7 +35,6 @@ //!TOKSTYLE- #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION -#include #include "../testing/fuzzing/fuzz_adapter.h" #endif //!TOKSTYLE+ @@ -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, @@ -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, @@ -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, diff --git a/toxcore/group.c b/toxcore/group.c index b52e740e..2a63851e 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -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); @@ -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); @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index 5fc94fb8..665099cd 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -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 */ diff --git a/toxcore/onion_client.c b/toxcore/onion_client.c index cdd33b7c..a4f62eab 100644 --- a/toxcore/onion_client.c +++ b/toxcore/onion_client.c @@ -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; diff --git a/toxcore/tox.c b/toxcore/tox.c index 70778f66..a52287f1 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -765,12 +765,12 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub if (host == nullptr || public_key == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_NULL); - return 0; + return false; } if (port == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); - return 0; + return false; } IP_Port *root; @@ -781,7 +781,7 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub LOGGER_DEBUG(tox->m->log, "could not resolve bootstrap node '%s'", host); net_freeipport(root); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); - return 0; + return false; } lock(tox); @@ -803,12 +803,12 @@ bool tox_bootstrap(Tox *tox, const char *host, uint16_t port, const uint8_t *pub if (count > 0) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); - return 1; + return true; } LOGGER_DEBUG(tox->m->log, "bootstrap node '%s' resolved to 0 IP_Ports", host); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); - return 0; + return false; } bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t *public_key, @@ -818,12 +818,12 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t if (host == nullptr || public_key == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_NULL); - return 0; + return false; } if (port == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_PORT); - return 0; + return false; } IP_Port *root; @@ -833,7 +833,7 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t if (count == -1) { net_freeipport(root); SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); - return 0; + return false; } lock(tox); @@ -851,11 +851,11 @@ bool tox_add_tcp_relay(Tox *tox, const char *host, uint16_t port, const uint8_t if (count > 0) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); - return 1; + return true; } SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); - return 0; + return false; } Tox_Connection tox_self_get_connection_status(const Tox *tox) @@ -970,7 +970,7 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set if (name == nullptr && length != 0) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_NULL); - return 0; + return false; } lock(tox); @@ -980,12 +980,12 @@ bool tox_self_set_name(Tox *tox, const uint8_t *name, size_t length, Tox_Err_Set send_name_all_groups(tox->m->conferences_object); SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); unlock(tox); - return 1; + return true; } SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG); unlock(tox); - return 0; + return false; } size_t tox_self_get_name_size(const Tox *tox) @@ -1014,7 +1014,7 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t if (status_message == nullptr && length != 0) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_NULL); - return 0; + return false; } lock(tox); @@ -1022,12 +1022,12 @@ bool tox_self_set_status_message(Tox *tox, const uint8_t *status_message, size_t if (m_set_statusmessage(tox->m, status_message, length) == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_OK); unlock(tox); - return 1; + return true; } SET_ERROR_PARAMETER(error, TOX_ERR_SET_INFO_TOO_LONG); unlock(tox); - return 0; + return false; } size_t tox_self_get_status_message_size(const Tox *tox) @@ -1171,11 +1171,11 @@ bool tox_friend_delete(Tox *tox, uint32_t friend_number, Tox_Err_Friend_Delete * // TODO(irungentoo): handle if realloc fails? if (ret == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_DELETE_FRIEND_NOT_FOUND); - return 0; + return false; } SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_DELETE_OK); - return 1; + return true; } uint32_t tox_friend_by_public_key(const Tox *tox, const uint8_t *public_key, Tox_Err_Friend_By_Public_Key *error) @@ -1206,7 +1206,7 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t * assert(tox != nullptr); if (public_key == nullptr) { - return 0; + return false; } lock(tox); @@ -1214,12 +1214,12 @@ bool tox_friend_get_public_key(const Tox *tox, uint32_t friend_number, uint8_t * if (get_real_pk(tox->m, friend_number, public_key) == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_FRIEND_NOT_FOUND); unlock(tox); - return 0; + return false; } SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_GET_PUBLIC_KEY_OK); unlock(tox); - return 1; + return true; } bool tox_friend_exists(const Tox *tox, uint32_t friend_number) @@ -1290,7 +1290,7 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name, if (name == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_NULL); - return 0; + return false; } lock(tox); @@ -1299,11 +1299,11 @@ bool tox_friend_get_name(const Tox *tox, uint32_t friend_number, uint8_t *name, if (ret == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND); - return 0; + return false; } SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK); - return 1; + return true; } void tox_callback_friend_name(Tox *tox, tox_friend_name_cb *callback) @@ -1414,7 +1414,7 @@ bool tox_friend_get_typing(const Tox *tox, uint32_t friend_number, Tox_Err_Frien if (ret == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_FRIEND_NOT_FOUND); - return 0; + return false; } SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_QUERY_OK); @@ -1435,12 +1435,12 @@ bool tox_self_set_typing(Tox *tox, uint32_t friend_number, bool typing, Tox_Err_ if (m_set_usertyping(tox->m, friend_number, typing) == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_FRIEND_NOT_FOUND); unlock(tox); - return 0; + return false; } SET_ERROR_PARAMETER(error, TOX_ERR_SET_TYPING_OK); unlock(tox); - return 1; + return true; } non_null(1) nullable(3) @@ -1529,11 +1529,11 @@ void tox_callback_friend_message(Tox *tox, tox_friend_message_cb *callback) bool tox_hash(uint8_t *hash, const uint8_t *data, size_t length) { if (hash == nullptr || (data == nullptr && length != 0)) { - return 0; + return false; } crypto_sha256(hash, data, length); - return 1; + return true; } bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, Tox_File_Control control, @@ -1546,56 +1546,56 @@ bool tox_file_control(Tox *tox, uint32_t friend_number, uint32_t file_number, To if (ret == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_OK); - return 1; + return true; } switch (ret) { case -1: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_FRIEND_NOT_FOUND); - return 0; + return false; } case -2: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_FRIEND_NOT_CONNECTED); - return 0; + return false; } case -3: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_NOT_FOUND); - return 0; + return false; } case -4: { /* can't happen (this code is returned if `control` is invalid type) */ LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); - return 0; + return false; } case -5: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_ALREADY_PAUSED); - return 0; + return false; } case -6: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_DENIED); - return 0; + return false; } case -7: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_NOT_PAUSED); - return 0; + return false; } case -8: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_CONTROL_SENDQ); - return 0; + return false; } } /* can't happen */ LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); - return 0; + return false; } bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint64_t position, @@ -1608,46 +1608,46 @@ bool tox_file_seek(Tox *tox, uint32_t friend_number, uint32_t file_number, uint6 if (ret == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_OK); - return 1; + return true; } switch (ret) { case -1: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_FRIEND_NOT_FOUND); - return 0; + return false; } case -2: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_FRIEND_NOT_CONNECTED); - return 0; + return false; } case -3: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_NOT_FOUND); - return 0; + return false; } case -4: // fall-through case -5: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_DENIED); - return 0; + return false; } case -6: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_INVALID_POSITION); - return 0; + return false; } case -8: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEEK_SENDQ); - return 0; + return false; } } /* can't happen */ LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); - return 0; + return false; } void tox_callback_file_recv_control(Tox *tox, tox_file_recv_control_cb *callback) @@ -1663,7 +1663,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_ if (file_id == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_NULL); - return 0; + return false; } lock(tox); @@ -1672,7 +1672,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_ if (ret == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_OK); - return 1; + return true; } if (ret == -1) { @@ -1681,7 +1681,7 @@ bool tox_file_get_file_id(const Tox *tox, uint32_t friend_number, uint32_t file_ SET_ERROR_PARAMETER(error, TOX_ERR_FILE_GET_NOT_FOUND); } - return 0; + return false; } uint32_t tox_file_send(Tox *tox, uint32_t friend_number, uint32_t kind, uint64_t file_size, const uint8_t *file_id, @@ -1749,50 +1749,50 @@ bool tox_file_send_chunk(Tox *tox, uint32_t friend_number, uint32_t file_number, if (ret == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_OK); - return 1; + return true; } switch (ret) { case -1: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_FOUND); - return 0; + return false; } case -2: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_FRIEND_NOT_CONNECTED); - return 0; + return false; } case -3: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_NOT_FOUND); - return 0; + return false; } case -4: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_NOT_TRANSFERRING); - return 0; + return false; } case -5: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_INVALID_LENGTH); - return 0; + return false; } case -6: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_SENDQ); - return 0; + return false; } case -7: { SET_ERROR_PARAMETER(error, TOX_ERR_FILE_SEND_CHUNK_WRONG_POSITION); - return 0; + return false; } } /* can't happen */ LOGGER_FATAL(tox->m->log, "impossible return value: %d", ret); - return 0; + return false; } void tox_callback_file_chunk_request(Tox *tox, tox_file_chunk_request_cb *callback) @@ -1995,7 +1995,7 @@ bool tox_conference_peer_number_is_ours(const Tox *tox, uint32_t conference_numb } SET_ERROR_PARAMETER(error, TOX_ERR_CONFERENCE_PEER_QUERY_OK); - return ret; + return ret != 0; } uint32_t tox_conference_offline_peer_count(const Tox *tox, uint32_t conference_number, @@ -2468,17 +2468,17 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_ if (data == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_NULL); - return 0; + return false; } if (length == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY); - return 0; + return false; } if (data[0] < PACKET_ID_RANGE_LOSSY_START || data[0] > PACKET_ID_RANGE_LOSSY_END) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_INVALID); - return 0; + return false; } lock(tox); @@ -2487,11 +2487,7 @@ bool tox_friend_send_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_ set_custom_packet_error(ret, error); - if (ret == 0) { - return 1; - } - - return 0; + return ret == 0; } void tox_callback_friend_lossy_packet(Tox *tox, tox_friend_lossy_packet_cb *callback) @@ -2520,12 +2516,12 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin if (data == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_NULL); - return 0; + return false; } if (length == 0) { SET_ERROR_PARAMETER(error, TOX_ERR_FRIEND_CUSTOM_PACKET_EMPTY); - return 0; + return false; } lock(tox); @@ -2534,11 +2530,7 @@ bool tox_friend_send_lossless_packet(Tox *tox, uint32_t friend_number, const uin set_custom_packet_error(ret, error); - if (ret == 0) { - return 1; - } - - return 0; + return ret == 0; } void tox_callback_friend_lossless_packet(Tox *tox, tox_friend_lossless_packet_cb *callback) diff --git a/toxencryptsave/toxencryptsave.c b/toxencryptsave/toxencryptsave.c index f75b854b..91b6dbb2 100644 --- a/toxencryptsave/toxencryptsave.c +++ b/toxencryptsave/toxencryptsave.c @@ -166,7 +166,7 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz { if (plaintext_len == 0 || plaintext == nullptr || key == nullptr || ciphertext == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_NULL); - return 0; + return false; } // the output data consists of, in order: @@ -193,11 +193,11 @@ bool tox_pass_key_encrypt(const Tox_Pass_Key *key, const uint8_t *plaintext, siz if (encrypt_data_symmetric(key->key, nonce, plaintext, plaintext_len, ciphertext) != plaintext_len + crypto_box_MACBYTES) { SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_FAILED); - return 0; + return false; } SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_OK); - return 1; + return true; } /* Encrypts the given data with the given passphrase. The output array must be @@ -219,7 +219,7 @@ bool tox_pass_encrypt(const uint8_t *plaintext, size_t plaintext_len, const uint SET_ERROR_PARAMETER(error, TOX_ERR_ENCRYPTION_KEY_DERIVATION_FAILED); } - return 0; + return false; } bool result = tox_pass_key_encrypt(key, plaintext, plaintext_len, ciphertext, error); @@ -239,17 +239,17 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, si { if (length <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH); - return 0; + return false; } if (ciphertext == nullptr || key == nullptr || plaintext == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL); - return 0; + return false; } if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT); - return 0; + return false; } ciphertext += TOX_ENC_SAVE_MAGIC_LENGTH; @@ -265,11 +265,11 @@ bool tox_pass_key_decrypt(const Tox_Pass_Key *key, const uint8_t *ciphertext, si if (decrypt_data_symmetric(key->key, nonce, ciphertext, decrypt_length + crypto_box_MACBYTES, plaintext) != decrypt_length) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_FAILED); - return 0; + return false; } SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_OK); - return 1; + return true; } /* Decrypts the given data with the given passphrase. The output array must be @@ -285,17 +285,17 @@ bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const ui { if (ciphertext_len <= TOX_PASS_ENCRYPTION_EXTRA_LENGTH) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_INVALID_LENGTH); - return 0; + return false; } if (ciphertext == nullptr || passphrase == nullptr || plaintext == nullptr) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_NULL); - return 0; + return false; } if (memcmp(ciphertext, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) != 0) { SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_BAD_FORMAT); - return 0; + return false; } uint8_t salt[crypto_pwhash_scryptsalsa208sha256_SALTBYTES]; @@ -307,7 +307,7 @@ bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const ui if (key == nullptr) { /* out of memory most likely */ SET_ERROR_PARAMETER(error, TOX_ERR_DECRYPTION_KEY_DERIVATION_FAILED); - return 0; + return false; } bool result = tox_pass_key_decrypt(key, ciphertext, ciphertext_len, plaintext, error); @@ -319,9 +319,5 @@ bool tox_pass_decrypt(const uint8_t *ciphertext, size_t ciphertext_len, const ui */ bool tox_is_data_encrypted(const uint8_t *data) { - if (memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0) { - return 1; - } - - return 0; + return memcmp(data, TOX_ENC_SAVE_MAGIC_NUMBER, TOX_ENC_SAVE_MAGIC_LENGTH) == 0; }