Skip to content

Commit

Permalink
cleanup: Remove more boolean conversions (and a bugfix).
Browse files Browse the repository at this point in the history
These were found by the new stronger type check in cimple. The one
bugfix is in `crypto_sha512_cmp`, which used to think `crypto_verify_32`
returns bool while actually it's -1/0/1.
  • Loading branch information
iphydf committed Feb 25, 2022
1 parent e230ad4 commit 6d6bd8c
Show file tree
Hide file tree
Showing 24 changed files with 334 additions and 328 deletions.
22 changes: 11 additions & 11 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ static void test_basic(void)
ck_assert_msg(packet_resp_plain[0] == TCP_PACKET_ROUTING_RESPONSE, "Server sent the wrong packet id: %u",
packet_resp_plain[0]);
ck_assert_msg(packet_resp_plain[1] == 0, "Server did not refuse the connection.");
ck_assert_msg(public_key_cmp(packet_resp_plain + 2, f_public_key) == 0, "Server sent the wrong public key.");
ck_assert_msg(id_equal(packet_resp_plain + 2, f_public_key), "Server sent the wrong public key.");

// Closing connections.
kill_sock(sock);
Expand Down Expand Up @@ -315,14 +315,14 @@ static void test_some(void)
ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
ck_assert_msg(public_key_cmp(data + 2, con3->public_key) == 0, "Key in response packet wrong.");
ck_assert_msg(id_equal(data + 2, con3->public_key), "Key in response packet wrong.");

// Connection 3
len = read_packet_sec_TCP(logger, con3, data, 2 + 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE);
ck_assert_msg(len == 1 + 1 + CRYPTO_PUBLIC_KEY_SIZE, "Wrong response packet length of %d.", len);
ck_assert_msg(data[0] == TCP_PACKET_ROUTING_RESPONSE, "Wrong response packet id of %d.", data[0]);
ck_assert_msg(data[1] == 16, "Server didn't refuse connection using wrong public key.");
ck_assert_msg(public_key_cmp(data + 2, con1->public_key) == 0, "Key in response packet wrong.");
ck_assert_msg(id_equal(data + 2, con1->public_key), "Key in response packet wrong.");

uint8_t test_packet[512] = {16, 17, 16, 86, 99, 127, 255, 189, 78}; // What is this packet????

Expand Down Expand Up @@ -458,7 +458,7 @@ static int oob_data_callback(void *object, const uint8_t *public_key, const uint
return 1;
}

if (public_key_cmp(public_key, oob_pubkey) != 0) {
if (!id_equal(public_key, oob_pubkey)) {
return 1;
}

Expand Down Expand Up @@ -563,7 +563,7 @@ static void test_client(void)
// All callback methods save data should have run during the above network prodding.
ck_assert_msg(oob_data_callback_good == 1, "OOB callback not called");
ck_assert_msg(response_callback_good == 1, "Response callback not called.");
ck_assert_msg(public_key_cmp(response_callback_public_key, f2_public_key) == 0, "Wrong public key.");
ck_assert_msg(id_equal(response_callback_public_key, f2_public_key), "Wrong public key.");
ck_assert_msg(status_callback_good == 1, "Status callback not called.");
ck_assert_msg(status_callback_status == 2, "Wrong status callback status.");
ck_assert_msg(status_callback_connection_id == response_callback_connection_id,
Expand Down Expand Up @@ -680,17 +680,17 @@ static void test_tcp_connection(void)
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");
ck_assert_msg(id_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");

TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key");
ck_assert_msg(id_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");

crypto_new_keypair(self_public_key, self_secret_key);
TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key");
ck_assert_msg(id_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");

IP_Port ip_port_tcp_s;

Expand Down Expand Up @@ -788,17 +788,17 @@ static void test_tcp_connection2(void)
uint8_t self_secret_key[CRYPTO_SECRET_KEY_SIZE];
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Server *tcp_s = new_TCP_server(logger, USE_IPV6, NUM_PORTS, ports, self_secret_key, nullptr);
ck_assert_msg(public_key_cmp(tcp_server_public_key(tcp_s), self_public_key) == 0, "Wrong public key");
ck_assert_msg(id_equal(tcp_server_public_key(tcp_s), self_public_key), "Wrong public key");

TCP_Proxy_Info proxy_info;
proxy_info.proxy_type = TCP_PROXY_NONE;
crypto_new_keypair(self_public_key, self_secret_key);
TCP_Connections *tc_1 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_1), self_public_key) == 0, "Wrong public key");
ck_assert_msg(id_equal(tcp_connections_public_key(tc_1), self_public_key), "Wrong public key");

crypto_new_keypair(self_public_key, self_secret_key);
TCP_Connections *tc_2 = new_tcp_connections(logger, mono_time, self_secret_key, &proxy_info);
ck_assert_msg(public_key_cmp(tcp_connections_public_key(tc_2), self_public_key) == 0, "Wrong public key");
ck_assert_msg(id_equal(tcp_connections_public_key(tc_2), self_public_key), "Wrong public key");

IP_Port ip_port_tcp_s;

Expand Down
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 @@
aea24cfae8db82511a298e2dbedb6634145732190ebcad361690cb6ca6560d7c /usr/local/bin/tox-bootstrapd
c6a650f9c475e394993d658e0259231bcb32071d5d66b9c7d25616d9cd8c2ea1 /usr/local/bin/tox-bootstrapd
4 changes: 2 additions & 2 deletions toxav/msi.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data
const uint8_t *it = data;
int size_constraint = length;

while (*it) {/* until end byte is hit */
while (*it != 0) {/* until end byte is hit */
switch (*it) {
case ID_REQUEST: {
if (!check_size(log, it, &size_constraint, 1) ||
Expand Down Expand Up @@ -435,7 +435,7 @@ static int msg_parse_in(const Logger *log, MSIMessage *dest, const uint8_t *data
}
}

if (dest->request.exists == false) {
if (!dest->request.exists) {
LOGGER_ERROR(log, "Invalid request field!");
return -1;
}
Expand Down
14 changes: 7 additions & 7 deletions toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, ui
Toxav_Err_Answer rc = TOXAV_ERR_ANSWER_OK;
ToxAVCall *call;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_ANSWER_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -612,7 +612,7 @@ static Toxav_Err_Call_Control call_control_handle(ToxAVCall *call, Toxav_Call_Co
}
static Toxav_Err_Call_Control call_control(ToxAV *av, uint32_t friend_number, Toxav_Call_Control control)
{
if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
return TOXAV_ERR_CALL_CONTROL_FRIEND_NOT_FOUND;
}

Expand Down Expand Up @@ -644,7 +644,7 @@ bool toxav_audio_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra
Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK;
ToxAVCall *call;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -716,7 +716,7 @@ bool toxav_video_set_bit_rate(ToxAV *av, uint32_t friend_number, uint32_t bit_ra
Toxav_Err_Bit_Rate_Set rc = TOXAV_ERR_BIT_RATE_SET_OK;
ToxAVCall *call;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_BIT_RATE_SET_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -802,7 +802,7 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc
Toxav_Err_Send_Frame rc = TOXAV_ERR_SEND_FRAME_OK;
ToxAVCall *call;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -929,7 +929,7 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u

int vpx_encode_flags = 0;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_SEND_FRAME_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down Expand Up @@ -1238,7 +1238,7 @@ static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *er
Toxav_Err_Call rc = TOXAV_ERR_CALL_OK;
ToxAVCall *call = nullptr;

if (m_friend_exists(av->m, friend_number) == 0) {
if (!m_friend_exists(av->m, friend_number)) {
rc = TOXAV_ERR_CALL_FRIEND_NOT_FOUND;
goto RETURN;
}
Expand Down
34 changes: 17 additions & 17 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -762,16 +762,16 @@ static void update_client(const Logger *log, const Mono_Time *mono_time, int ind
* return True(1) or False(0)
*/
non_null()
static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_time, Client_data *list, uint16_t length,
const uint8_t *public_key, const IP_Port *ip_port)
static bool client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_time, Client_data *list, uint16_t length,
const uint8_t *public_key, const IP_Port *ip_port)
{
const uint64_t temp_time = mono_time_get(mono_time);
uint32_t index = index_of_client_pk(list, length, public_key);

/* if public_key is in list, find it and maybe overwrite ip_port */
if (index != UINT32_MAX) {
update_client(log, mono_time, index, &list[index], ip_port);
return 1;
return true;
}

/* public_key not in list yet: see if we can find an identical ip_port, in
Expand All @@ -782,7 +782,7 @@ static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_ti
index = index_of_client_ip_port(list, length, ip_port);

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

IPPTsPng *assoc;
Expand All @@ -805,7 +805,7 @@ static int client_or_ip_port_in_list(const Logger *log, const Mono_Time *mono_ti
/* kill the other address, if it was set */
const IPPTsPng empty_ipptspng = {{{{0}}}};
*assoc = empty_ipptspng;
return 1;
return true;
}

bool add_to_list(Node_format *nodes_list, uint32_t length, const uint8_t *pk, const IP_Port *ip_port,
Expand Down Expand Up @@ -1069,11 +1069,11 @@ static bool replace_all(const DHT *dht,
*
* simulate is set to 1 if we want to check if a node can be added to the list without adding it.
*
* return -1 on failure.
* return 0 on success.
* return false on failure.
* return true on success.
*/
non_null()
static int add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port, bool simulate)
static bool add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port, bool simulate)
{
unsigned int index = bit_by_bit_cmp(public_key, dht->self_public_key);

Expand All @@ -1092,22 +1092,22 @@ static int add_to_close(DHT *dht, const uint8_t *public_key, const IP_Port *ip_p
}

if (simulate) {
return 0;
return true;
}

id_copy(client->public_key, public_key);
update_client_with_reset(dht->mono_time, client, ip_port);
return 0;
return true;
}

return -1;
return false;
}

/** Return 1 if node can be added to close list, 0 if it can't.
*/
bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, const IP_Port *ip_port)
{
return add_to_close(dht, public_key, ip_port, 1) == 0;
return add_to_close(dht, public_key, ip_port, true);
}

non_null()
Expand Down Expand Up @@ -1151,7 +1151,7 @@ static bool ping_node_from_getnodes_ok(DHT *dht, const uint8_t *public_key, cons
{
bool ret = false;

if (add_to_close(dht, public_key, ip_port, 1) == 0) {
if (add_to_close(dht, public_key, ip_port, true)) {
ret = true;
}

Expand Down Expand Up @@ -1225,7 +1225,7 @@ uint32_t addto_lists(DHT *dht, const IP_Port *ip_port, const uint8_t *public_key
public_key, &ipp_copy);

/* add_to_close should be called only if !in_list (don't extract to variable) */
if (in_close_list || add_to_close(dht, public_key, &ipp_copy, 0)) {
if (in_close_list || !add_to_close(dht, public_key, &ipp_copy, false)) {
++used;
}

Expand Down Expand Up @@ -1693,7 +1693,7 @@ int dht_getfriendip(const DHT *dht, const uint8_t *public_key, IP_Port *ip_port)
const Client_data *const client = &frnd->client_list[client_index];
const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr };

for (const IPPTsPng * const *it = assocs; *it; ++it) {
for (const IPPTsPng * const *it = assocs; *it != nullptr; ++it) {
const IPPTsPng *const assoc = *it;

if (!assoc_timeout(dht->cur_time, assoc)) {
Expand Down Expand Up @@ -1837,7 +1837,7 @@ static void do_Close(DHT *dht)

IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr };

for (IPPTsPng * const *it = assocs; *it; ++it) {
for (IPPTsPng * const *it = assocs; *it != nullptr; ++it) {
IPPTsPng *const assoc = *it;

if (assoc->timestamp != 0) {
Expand Down Expand Up @@ -1898,7 +1898,7 @@ int route_packet(const DHT *dht, const uint8_t *public_key, const uint8_t *packe
const Client_data *const client = &dht->close_clientlist[i];
const IPPTsPng *const assocs[] = { &client->assoc6, &client->assoc4, nullptr };

for (const IPPTsPng * const *it = assocs; *it; ++it) {
for (const IPPTsPng * const *it = assocs; *it != nullptr; ++it) {
const IPPTsPng *const assoc = *it;

if (ip_isset(&assoc->ip_port.ip)) {
Expand Down
Loading

0 comments on commit 6d6bd8c

Please sign in to comment.