Skip to content

Commit

Permalink
Improve static and const correctness.
Browse files Browse the repository at this point in the history
- Any non-externally-visible declarations should be `static`.
- Casting away the `const` qualifier from pointers-to-const is
  dangerous. All but one instance of this are now correct. The one
  instance where we can't keep `const` is one where toxav code actually
  writes to a chunk of memory marked as `const`. This code also assumes
  4 byte alignment of data packets. I don't know whether that is a valid
  assumption, but it's likely unportable, and *not* obviously correct.
- Replaced empty parameter lists with `(void)` to avoid passing
  parameters to it. Empty parameter lists are old style declarations for
  unknown number and type of arguments.
- Commented out (as `#if DHT_HARDENING` block) the hardening code that
  was never executed.
- Minor style fix: don't use `default` in enum-switches unless the number
  of enumerators in the default case is very large. In this case, it was
  2, so we want to list them both explicitly to be warned about missing
  one if we add one in the future.
- Removed the only two function declarations from nTox.h and put them
  into nTox.c. They are not used outside and nTox is not a library.
  • Loading branch information
iphydf committed Sep 6, 2016
1 parent 4e6c86d commit ad26560
Show file tree
Hide file tree
Showing 38 changed files with 416 additions and 391 deletions.
10 changes: 5 additions & 5 deletions auto_tests/TCP_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ struct sec_TCP_con {
uint8_t shared_key[crypto_box_BEFORENMBYTES];
};

struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
{
struct sec_TCP_con *sec_c = malloc(sizeof(struct sec_TCP_con));
sock_t sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP);
Expand Down Expand Up @@ -176,13 +176,13 @@ struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s)
return sec_c;
}

void kill_TCP_con(struct sec_TCP_con *con)
static void kill_TCP_con(struct sec_TCP_con *con)
{
kill_sock(con->sock);
free(con);
}

int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
{
uint8_t packet[sizeof(uint16_t) + length + crypto_box_MACBYTES];

Expand All @@ -200,7 +200,7 @@ int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t *data, u
return 0;
}

int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
static int read_packet_sec_TCP(struct sec_TCP_con *con, uint8_t *data, uint16_t length)
{
int len = recv(con->sock, data, length, 0);
ck_assert_msg(len == length, "wrong len %i\n", len);
Expand Down Expand Up @@ -731,7 +731,7 @@ START_TEST(test_tcp_connection2)
}
END_TEST

Suite *TCP_suite(void)
static Suite *TCP_suite(void)
{
Suite *s = suite_create("TCP");

Expand Down
2 changes: 1 addition & 1 deletion auto_tests/assoc_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ START_TEST(test_fillup)
}
END_TEST

Suite *Assoc_suite(void)
static Suite *Assoc_suite(void)
{
Suite *s = suite_create("Assoc");

Expand Down
16 changes: 8 additions & 8 deletions auto_tests/crypto_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include "helpers.h"

void rand_bytes(uint8_t *b, size_t blen)
static void rand_bytes(uint8_t *b, size_t blen)
{
size_t i;

Expand All @@ -23,27 +23,27 @@ void rand_bytes(uint8_t *b, size_t blen)

// These test vectors are from libsodium's test suite

unsigned char alicesk[32] = {
static const unsigned char alicesk[32] = {
0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d,
0x3c, 0x16, 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45,
0xdf, 0x4c, 0x2f, 0x87, 0xeb, 0xc0, 0x99, 0x2a,
0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, 0x2c, 0x2a
};

unsigned char bobpk[32] = {
static const unsigned char bobpk[32] = {
0xde, 0x9e, 0xdb, 0x7d, 0x7b, 0x7d, 0xc1, 0xb4,
0xd3, 0x5b, 0x61, 0xc2, 0xec, 0xe4, 0x35, 0x37,
0x3f, 0x83, 0x43, 0xc8, 0x5b, 0x78, 0x67, 0x4d,
0xad, 0xfc, 0x7e, 0x14, 0x6f, 0x88, 0x2b, 0x4f
};

unsigned char nonce[24] = {
static const unsigned char nonce[24] = {
0x69, 0x69, 0x6e, 0xe9, 0x55, 0xb6, 0x2b, 0x73,
0xcd, 0x62, 0xbd, 0xa8, 0x75, 0xfc, 0x73, 0xd6,
0x82, 0x19, 0xe0, 0x03, 0x6b, 0x7a, 0x0b, 0x37
};

unsigned char test_m[131] = {
static const unsigned char test_m[131] = {
0xbe, 0x07, 0x5f, 0xc5, 0x3c, 0x81, 0xf2, 0xd5,
0xcf, 0x14, 0x13, 0x16, 0xeb, 0xeb, 0x0c, 0x7b,
0x52, 0x28, 0xc5, 0x2a, 0x4c, 0x62, 0xcb, 0xd4,
Expand All @@ -63,7 +63,7 @@ unsigned char test_m[131] = {
0x5e, 0x07, 0x05
};

unsigned char test_c[147] = {
static const unsigned char test_c[147] = {
0xf3, 0xff, 0xc7, 0x70, 0x3f, 0x94, 0x00, 0xe5,
0x2a, 0x7d, 0xfb, 0x4b, 0x3d, 0x33, 0x05, 0xd9,
0x8e, 0x99, 0x3b, 0x9f, 0x48, 0x68, 0x12, 0x73,
Expand Down Expand Up @@ -271,7 +271,7 @@ START_TEST(test_large_data_symmetric)
}
END_TEST

void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num)
static void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num)
{
uint32_t num1, num2;
memcpy(&num1, nonce + (crypto_box_NONCEBYTES - sizeof(num1)), sizeof(num1));
Expand Down Expand Up @@ -323,7 +323,7 @@ START_TEST(test_increment_nonce)
}
END_TEST

Suite *crypto_suite(void)
static Suite *crypto_suite(void)
{
Suite *s = suite_create("Crypto");

Expand Down
66 changes: 34 additions & 32 deletions auto_tests/dht_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#include "helpers.h"


// These tests currently fail.
#if 0
#define swap(x,y) do \
{ unsigned char swap_temp[sizeof(x) == sizeof(y) ? (signed)sizeof(x) : -1]; \
memcpy(swap_temp,&y,sizeof(x)); \
Expand All @@ -18,31 +21,31 @@
} while(0)


void mark_bad(IPPTsPng *ipptp)
static void mark_bad(IPPTsPng *ipptp)
{
ipptp->timestamp = unix_time() - 2 * BAD_NODE_TIMEOUT;
ipptp->hardening.routes_requests_ok = 0;
ipptp->hardening.send_nodes_ok = 0;
ipptp->hardening.testing_requests = 0;
}

void mark_possible_bad(IPPTsPng *ipptp)
static void mark_possible_bad(IPPTsPng *ipptp)
{
ipptp->timestamp = unix_time();
ipptp->hardening.routes_requests_ok = 0;
ipptp->hardening.send_nodes_ok = 0;
ipptp->hardening.testing_requests = 0;
}

void mark_good(IPPTsPng *ipptp)
static void mark_good(IPPTsPng *ipptp)
{
ipptp->timestamp = unix_time();
ipptp->hardening.routes_requests_ok = (HARDENING_ALL_OK >> 0) & 1;
ipptp->hardening.send_nodes_ok = (HARDENING_ALL_OK >> 1) & 1;
ipptp->hardening.testing_requests = (HARDENING_ALL_OK >> 2) & 1;
}

void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6)
static void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6)
{
uint32_t i;

Expand All @@ -57,7 +60,7 @@ void mark_all_good(Client_data *list, uint32_t length, uint8_t ipv6)

/* Returns 1 if public_key has a furthest distance to comp_client_id
than all public_key's in the list */
uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t length, const uint8_t *public_key)
static uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t length, const uint8_t *public_key)
{
uint32_t i;

Expand All @@ -70,7 +73,7 @@ uint8_t is_furthest(const uint8_t *comp_client_id, Client_data *list, uint32_t l
return 1;
}

int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key)
static int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key)
{
int i;

Expand All @@ -83,10 +86,10 @@ int client_in_list(Client_data *list, uint32_t length, const uint8_t *public_key
return -1;
}

void test_addto_lists_update(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port)
static void test_addto_lists_update(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port)
{
int used, test, test1, test2, found;
IP_Port test_ipp;
Expand Down Expand Up @@ -157,10 +160,10 @@ void test_addto_lists_update(DHT *dht,
"Client IP_Port is incorrect");
}

void test_addto_lists_bad(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port)
static void test_addto_lists_bad(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port)
{
// check "bad" clients replacement
int used, test1, test2, test3;
Expand Down Expand Up @@ -200,11 +203,11 @@ void test_addto_lists_bad(DHT *dht,
ck_assert_msg(client_in_list(list, length, test_id3) >= 0, "Wrong bad client removed");
}

void test_addto_lists_possible_bad(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port,
const uint8_t *comp_client_id)
static void test_addto_lists_possible_bad(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port,
const uint8_t *comp_client_id)
{
// check "possibly bad" clients replacement
int used, test1, test2, test3;
Expand Down Expand Up @@ -265,11 +268,11 @@ void test_addto_lists_possible_bad(DHT *dht,
}
}

void test_addto_lists_good(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port,
const uint8_t *comp_client_id)
static void test_addto_lists_good(DHT *dht,
Client_data *list,
uint32_t length,
IP_Port *ip_port,
const uint8_t *comp_client_id)
{
uint8_t public_key[crypto_box_PUBLICKEYBYTES];
uint8_t ipv6 = ip_port->ip.family == AF_INET6 ? 1 : 0;
Expand All @@ -295,8 +298,6 @@ void test_addto_lists_good(DHT *dht,
ck_assert_msg(client_in_list(list, length, public_key) == -1, "Good client id is in the list");
}

// These tests currently fail.
#if 0
static void test_addto_lists(IP ip)
{
Networking_Core *net = new_networking(NULL, ip, TOX_PORT_DEFAULT);
Expand Down Expand Up @@ -382,7 +383,7 @@ END_TEST

#define DHT_LIST_LENGTH 128

void print_pk(uint8_t *public_key)
static void print_pk(uint8_t *public_key)
{
uint32_t j;

Expand All @@ -393,8 +394,9 @@ void print_pk(uint8_t *public_key)
printf("\n");
}

void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1], unsigned int length, const uint8_t *pk,
const uint8_t *cmp_pk)
static void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1],
unsigned int length, const uint8_t *pk,
const uint8_t *cmp_pk)
{
uint8_t p_b[crypto_box_PUBLICKEYBYTES];
unsigned int i;
Expand Down Expand Up @@ -423,7 +425,7 @@ void test_add_to_list(uint8_t cmp_list[][crypto_box_PUBLICKEYBYTES + 1], unsigne

#define NUM_DHT 100

void test_list_main()
static void test_list_main(void)
{
DHT *dhts[NUM_DHT];

Expand Down Expand Up @@ -555,7 +557,7 @@ START_TEST(test_list)
}
END_TEST

void ip_callback(void *data, int32_t number, IP_Port ip_port)
static void ip_callback(void *data, int32_t number, IP_Port ip_port)
{
}

Expand Down Expand Up @@ -646,7 +648,7 @@ START_TEST(test_DHT_test)
}
END_TEST

Suite *dht_suite(void)
static Suite *dht_suite(void)
{
Suite *s = suite_create("DHT");

Expand Down
17 changes: 8 additions & 9 deletions auto_tests/encryptsave_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
#include "../toxencryptsave/crypto_pwhash_scryptsalsa208sha256/crypto_pwhash_scryptsalsa208sha256.h"
#endif

unsigned char salt[32] = {0xB1, 0xC2, 0x09, 0xEE, 0x50, 0x6C, 0xF0, 0x20, 0xC4, 0xD6, 0xEB, 0xC0, 0x44, 0x51, 0x3B, 0x60, 0x4B, 0x39, 0x4A, 0xCF, 0x09, 0x53, 0x4F, 0xEA, 0x08, 0x41, 0xFA, 0xCA, 0x66, 0xD2, 0x68, 0x7F};
unsigned char known_key[crypto_box_BEFORENMBYTES] = {0x29, 0x36, 0x1c, 0x9e, 0x65, 0xbb, 0x46, 0x8b, 0xde, 0xa1, 0xac, 0xf, 0xd5, 0x11, 0x81, 0xc8, 0x29, 0x28, 0x17, 0x23, 0xa6, 0xc3, 0x6b, 0x77, 0x2e, 0xd7, 0xd3, 0x10, 0xeb, 0xd2, 0xf7, 0xc8};
const char *pw = "hunter2";
unsigned int pwlen = 7;
static unsigned char salt[32] = {0xB1, 0xC2, 0x09, 0xEE, 0x50, 0x6C, 0xF0, 0x20, 0xC4, 0xD6, 0xEB, 0xC0, 0x44, 0x51, 0x3B, 0x60, 0x4B, 0x39, 0x4A, 0xCF, 0x09, 0x53, 0x4F, 0xEA, 0x08, 0x41, 0xFA, 0xCA, 0x66, 0xD2, 0x68, 0x7F};
static unsigned char known_key[crypto_box_BEFORENMBYTES] = {0x29, 0x36, 0x1c, 0x9e, 0x65, 0xbb, 0x46, 0x8b, 0xde, 0xa1, 0xac, 0xf, 0xd5, 0x11, 0x81, 0xc8, 0x29, 0x28, 0x17, 0x23, 0xa6, 0xc3, 0x6b, 0x77, 0x2e, 0xd7, 0xd3, 0x10, 0xeb, 0xd2, 0xf7, 0xc8};
static const char *pw = "hunter2";
static unsigned int pwlen = 7;

unsigned char known_key2[crypto_box_BEFORENMBYTES] = {0x7a, 0xfa, 0x95, 0x45, 0x36, 0x8a, 0xa2, 0x5c, 0x40, 0xfd, 0xc0, 0xe2, 0x35, 0x8, 0x7, 0x88, 0xfa, 0xf9, 0x37, 0x86, 0xeb, 0xff, 0x50, 0x4f, 0x3, 0xe2, 0xf6, 0xd9, 0xef, 0x9, 0x17, 0x1};
static unsigned char known_key2[crypto_box_BEFORENMBYTES] = {0x7a, 0xfa, 0x95, 0x45, 0x36, 0x8a, 0xa2, 0x5c, 0x40, 0xfd, 0xc0, 0xe2, 0x35, 0x8, 0x7, 0x88, 0xfa, 0xf9, 0x37, 0x86, 0xeb, 0xff, 0x50, 0x4f, 0x3, 0xe2, 0xf6, 0xd9, 0xef, 0x9, 0x17, 0x1};
// same as above, except standard opslimit instead of extra ops limit for test_known_kdf, and hash pw before kdf for compat

/* cause I'm shameless */
void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
static void accept_friend_request(Tox *m, const uint8_t *public_key, const uint8_t *data, size_t length, void *userdata)
{
if (*((uint32_t *)userdata) != 974536) {
return;
Expand Down Expand Up @@ -63,7 +63,7 @@ START_TEST(test_save_friend)
tox_callback_friend_request(tox2, accept_friend_request);
uint8_t address[TOX_ADDRESS_SIZE];
tox_self_get_address(tox2, address);
uint32_t test = tox_friend_add(tox1, address, (uint8_t *)"Gentoo", 7, 0);
uint32_t test = tox_friend_add(tox1, address, (const uint8_t *)"Gentoo", 7, 0);
ck_assert_msg(test != UINT32_MAX, "Failed to add friend");

size_t size = tox_get_savedata_size(tox1);
Expand Down Expand Up @@ -186,7 +186,7 @@ START_TEST(test_keys)
}
END_TEST

Suite *encryptsave_suite(void)
static Suite *encryptsave_suite(void)
{
Suite *s = suite_create("encryptsave");

Expand All @@ -212,4 +212,3 @@ int main(int argc, char *argv[])

return number_failed;
}

Loading

0 comments on commit ad26560

Please sign in to comment.