Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: make some non-const pointers const #1929

Merged
merged 1 commit into from
Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions toxav/groupav.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static Group_AV *new_group_av(const Logger *log, Tox *tox, Group_Chats *g_c, aud

static void group_av_peer_new(void *object, uint32_t groupnumber, uint32_t friendgroupnumber)
{
Group_AV *group_av = (Group_AV *)object;
const Group_AV *group_av = (const Group_AV *)object;
Group_Peer_AV *peer_av = (Group_Peer_AV *)calloc(1, sizeof(Group_Peer_AV));

if (!peer_av) {
Expand Down Expand Up @@ -570,7 +570,7 @@ int join_av_groupchat(const Logger *log, Tox *tox, Group_Chats *g_c, uint32_t fr
* return 0 on success.
* return -1 on failure.
*/
static int send_audio_packet(Group_Chats *g_c, uint32_t groupnumber, uint8_t *packet, uint16_t length)
static int send_audio_packet(const Group_Chats *g_c, uint32_t groupnumber, const uint8_t *packet, uint16_t length)
{
if (length == 0 || length > MAX_CRYPTO_DATA_SIZE - 1 - sizeof(uint16_t)) {
return -1;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static void fetch_broadcast_info(uint16_t port)
continue;
}

struct sockaddr_in *sock4 = (struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr;
const struct sockaddr_in *sock4 = (const struct sockaddr_in *)(void *)&i_faces[i].ifr_broadaddr;

if (count >= MAX_INTERFACES) {
break;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/Messenger.c
Original file line number Diff line number Diff line change
Expand Up @@ -2257,7 +2257,7 @@ static int m_handle_packet(void *object, int i, const uint8_t *temp, uint16_t le
memcpy(ft->id, data + 1 + sizeof(uint32_t) + sizeof(uint64_t), FILE_ID_LENGTH);

VLA(uint8_t, filename_terminated, filename_length + 1);
uint8_t *filename = nullptr;
const uint8_t *filename = nullptr;

if (filename_length) {
/* Force NULL terminate file name. */
Expand Down
28 changes: 14 additions & 14 deletions toxcore/TCP_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ uint32_t tcp_connected_relays_count(const TCP_Connections *tcp_c)
int send_packet_tcp_connection(const TCP_Connections *tcp_c, int connections_number, const uint8_t *packet,
uint16_t length)
{
TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
const TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);

if (!con_to) {
return -1;
Expand Down Expand Up @@ -489,7 +489,7 @@ void set_onion_packet_tcp_connection_callback(TCP_Connections *tcp_c, tcp_onion_
static int find_tcp_connection_to(const TCP_Connections *tcp_c, const uint8_t *public_key)
{
for (unsigned int i = 0; i < tcp_c->connections_length; ++i) {
TCP_Connection_to *con_to = get_connection(tcp_c, i);
const TCP_Connection_to *con_to = get_connection(tcp_c, i);

if (con_to) {
if (public_key_cmp(con_to->public_key, public_key) == 0) {
Expand All @@ -509,7 +509,7 @@ static int find_tcp_connection_to(const TCP_Connections *tcp_c, const uint8_t *p
static int find_tcp_connection_relay(const TCP_Connections *tcp_c, const uint8_t *relay_pk)
{
for (uint32_t i = 0; i < tcp_c->tcp_connections_length; ++i) {
TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
const TCP_con *tcp_con = get_tcp_connection(tcp_c, i);

if (tcp_con) {
if (tcp_con->status == TCP_CONN_SLEEPING) {
Expand Down Expand Up @@ -562,7 +562,7 @@ int new_tcp_connection_to(TCP_Connections *tcp_c, const uint8_t *public_key, int
*/
int kill_tcp_connection_to(TCP_Connections *tcp_c, int connections_number)
{
TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);
const TCP_Connection_to *con_to = get_connection(tcp_c, connections_number);

if (!con_to) {
return -1;
Expand Down Expand Up @@ -959,8 +959,8 @@ static int tcp_response_callback(void *object, uint8_t connection_id, const uint

static int tcp_status_callback(void *object, uint32_t number, uint8_t connection_id, uint8_t status)
{
TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object;
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con);
const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object;
const TCP_Connections *tcp_c = (const TCP_Connections *)tcp_con_custom_object(tcp_client_con);

unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con);
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
Expand Down Expand Up @@ -1002,17 +1002,17 @@ static int tcp_conn_data_callback(void *object, uint32_t number, uint8_t connect
return -1;
}

TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object;
const TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object;
JFreegman marked this conversation as resolved.
Show resolved Hide resolved
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con);

unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con);
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
const TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

if (!tcp_con) {
return -1;
}

TCP_Connection_to *con_to = get_connection(tcp_c, number);
const TCP_Connection_to *con_to = get_connection(tcp_c, number);

if (!con_to) {
return -1;
Expand All @@ -1032,11 +1032,11 @@ static int tcp_conn_oob_callback(void *object, const uint8_t *public_key, const
return -1;
}

TCP_Client_Connection *tcp_client_con = (TCP_Client_Connection *)object;
const TCP_Client_Connection *tcp_client_con = (const TCP_Client_Connection *)object;
TCP_Connections *tcp_c = (TCP_Connections *)tcp_con_custom_object(tcp_client_con);

unsigned int tcp_connections_number = tcp_con_custom_uint(tcp_client_con);
TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
const TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

if (!tcp_con) {
return -1;
Expand Down Expand Up @@ -1253,7 +1253,7 @@ int add_tcp_relay_connection(TCP_Connections *tcp_c, int connections_number, IP_

tcp_connections_number = add_tcp_relay_instance(tcp_c, ip_port, relay_pk);

TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);
const TCP_con *tcp_con = get_tcp_connection(tcp_c, tcp_connections_number);

if (!tcp_con) {
return -1;
Expand Down Expand Up @@ -1292,7 +1292,7 @@ uint32_t tcp_copy_connected_relays(const TCP_Connections *tcp_c, Node_format *tc
uint32_t copied = 0;

for (uint32_t i = 0; (i < tcp_c->tcp_connections_length) && (copied < max_num); ++i) {
TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length);
const TCP_con *tcp_con = get_tcp_connection(tcp_c, (i + r) % tcp_c->tcp_connections_length);

if (!tcp_con) {
continue;
Expand Down Expand Up @@ -1473,7 +1473,7 @@ static void kill_nonused_tcp(TCP_Connections *tcp_c)
uint32_t kill_count = 0;

for (uint32_t i = 0; i < tcp_c->tcp_connections_length && kill_count < max_kill_count; ++i) {
TCP_con *tcp_con = get_tcp_connection(tcp_c, i);
const TCP_con *tcp_con = get_tcp_connection(tcp_c, i);

if (tcp_con == nullptr) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ static int handle_TCP_oob_send(TCP_Server *tcp_server, uint32_t con_id, const ui
return -1;
}

TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id];
const TCP_Secure_Connection *con = &tcp_server->accepted_connection_array[con_id];

int other_index = get_TCP_connection_index(tcp_server, public_key);

Expand Down
4 changes: 2 additions & 2 deletions toxcore/friend_connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ Friend_Conn *get_conn(const Friend_Connections *fr_c, int friendcon_id)
int getfriend_conn_id_pk(const Friend_Connections *fr_c, const uint8_t *real_pk)
{
for (uint32_t i = 0; i < fr_c->num_cons; ++i) {
Friend_Conn *friend_con = get_conn(fr_c, i);
const Friend_Conn *friend_con = get_conn(fr_c, i);

if (friend_con) {
if (public_key_cmp(friend_con->real_public_key, real_pk) == 0) {
Expand Down Expand Up @@ -502,7 +502,7 @@ static int handle_lossy_packet(void *object, int number, const uint8_t *data, ui
return -1;
}

Friend_Connections *const fr_c = (Friend_Connections *)object;
const Friend_Connections *const fr_c = (const Friend_Connections *)object;
const Friend_Conn *friend_con = get_conn(fr_c, number);

if (!friend_con) {
Expand Down
10 changes: 5 additions & 5 deletions toxcore/group.c
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ static bool settitle(Group_Chats *g_c, uint32_t groupnumber, int peer_index, con
/** Check if the group has no online connection, and freeze all peers if so */
static void check_disconnected(Group_Chats *g_c, uint32_t groupnumber, void *userdata)
{
Group_c *g = get_group_c(g_c, groupnumber);
const Group_c *g = get_group_c(g_c, groupnumber);

if (!g) {
return;
Expand Down Expand Up @@ -1432,7 +1432,7 @@ static unsigned int send_lossy_group_peer(const Friend_Connections *fr_c, int fr
*/
int invite_friend(const Group_Chats *g_c, uint32_t friendnumber, uint32_t groupnumber)
{
Group_c *g = get_group_c(g_c, groupnumber);
const Group_c *g = get_group_c(g_c, groupnumber);

if (!g) {
return -1;
Expand Down Expand Up @@ -1922,7 +1922,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con

return;
} else {
Group_c *g = get_group_c(g_c, groupnumber);
const Group_c *g = get_group_c(g_c, groupnumber);

if (g && g->status == GROUPCHAT_STATUS_CONNECTED) {
send_invite_response(g_c, groupnumber, friendnumber, invite_data, invite_length);
Expand Down Expand Up @@ -2067,7 +2067,7 @@ static int send_packet_online(const Friend_Connections *fr_c, int friendcon_id,

static bool ping_groupchat(const Group_Chats *g_c, uint32_t groupnumber);

static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_t *data, uint16_t length)
static int handle_packet_online(const Group_Chats *g_c, int friendcon_id, const uint8_t *data, uint16_t length)
{
if (length != ONLINE_PACKET_DATA_SIZE) {
return -1;
Expand Down Expand Up @@ -2886,7 +2886,7 @@ static bool type_uses_lossy(uint8_t type)

static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uint16_t length, void *userdata)
{
Group_Chats *g_c = (Group_Chats *)object;
const Group_Chats *g_c = (const Group_Chats *)object;

if (data[0] != PACKET_ID_LOSSY_CONFERENCE) {
return -1;
Expand Down
14 changes: 7 additions & 7 deletions toxcore/net_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ static int tcp_data_callback(void *object, int crypt_connection_id, const uint8_
return -1;
}

Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

if (conn == nullptr) {
return -1;
Expand Down Expand Up @@ -2224,7 +2224,7 @@ static int tcp_oob_callback(void *object, const uint8_t *public_key, unsigned in
*/
int add_tcp_relay_peer(Net_Crypto *c, int crypt_connection_id, IP_Port ip_port, const uint8_t *public_key)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

if (conn == nullptr) {
return -1;
Expand Down Expand Up @@ -2306,7 +2306,7 @@ static void do_tcp(Net_Crypto *c, void *userdata)
pthread_mutex_unlock(&c->tcp_mutex);

for (uint32_t i = 0; i < c->crypto_connections_length; ++i) {
Crypto_Connection *conn = get_crypto_connection(c, i);
const Crypto_Connection *conn = get_crypto_connection(c, i);

if (conn == nullptr) {
continue;
Expand Down Expand Up @@ -2756,7 +2756,7 @@ bool max_speed_reached(Net_Crypto *c, int crypt_connection_id)
*/
uint32_t crypto_num_free_sendqueue_slots(const Net_Crypto *c, int crypt_connection_id)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

if (conn == nullptr) {
return 0;
Expand Down Expand Up @@ -2836,7 +2836,7 @@ int64_t write_cryptpacket(Net_Crypto *c, int crypt_connection_id, const uint8_t
*/
int cryptpacket_received(const Net_Crypto *c, int crypt_connection_id, uint32_t packet_number)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

if (conn == nullptr) {
return -1;
Expand Down Expand Up @@ -2926,7 +2926,7 @@ int crypto_kill(Net_Crypto *c, int crypt_connection_id)
bool crypto_connection_status(const Net_Crypto *c, int crypt_connection_id, bool *direct_connected,
unsigned int *online_tcp_relays)
{
Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);
const Crypto_Connection *conn = get_crypto_connection(c, crypt_connection_id);

if (conn == nullptr) {
return false;
Expand Down Expand Up @@ -3031,7 +3031,7 @@ Net_Crypto *new_net_crypto(const Logger *log, Mono_Time *mono_time, DHT *dht, co
static void kill_timedout(Net_Crypto *c, void *userdata)
{
for (uint32_t i = 0; i < c->crypto_connections_length; ++i) {
Crypto_Connection *conn = get_crypto_connection(c, i);
const Crypto_Connection *conn = get_crypto_connection(c, i);

if (conn == nullptr) {
continue;
Expand Down
16 changes: 8 additions & 8 deletions toxcore/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ static int receivepacket(const Logger *log, Socket sock, IP_Port *ip_port, uint8
*length = (uint32_t)fail_or_len;

if (addr.ss_family == AF_INET) {
struct sockaddr_in *addr_in = (struct sockaddr_in *)&addr;
const struct sockaddr_in *addr_in = (const struct sockaddr_in *)&addr;

const Family *const family = make_tox_family(addr_in->sin_family);
assert(family != nullptr);
Expand All @@ -675,7 +675,7 @@ static int receivepacket(const Logger *log, Socket sock, IP_Port *ip_port, uint8
get_ip4(&ip_port->ip.ip.v4, &addr_in->sin_addr);
ip_port->port = addr_in->sin_port;
} else if (addr.ss_family == AF_INET6) {
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&addr;
const struct sockaddr_in6 *addr_in6 = (const struct sockaddr_in6 *)&addr;
const Family *const family = make_tox_family(addr_in6->sin6_family);
assert(family != nullptr);

Expand Down Expand Up @@ -1342,12 +1342,12 @@ int addr_resolve(const char *address, IP *to, IP *extra)
switch (walker->ai_family) {
case AF_INET: {
if (walker->ai_family == family) { /* AF_INET requested, done */
struct sockaddr_in *addr = (struct sockaddr_in *)(void *)walker->ai_addr;
const struct sockaddr_in *addr = (const struct sockaddr_in *)(void *)walker->ai_addr;
get_ip4(&to->ip.v4, &addr->sin_addr);
result = TOX_ADDR_RESOLVE_INET;
done = true;
} else if (!(result & TOX_ADDR_RESOLVE_INET)) { /* AF_UNSPEC requested, store away */
struct sockaddr_in *addr = (struct sockaddr_in *)(void *)walker->ai_addr;
const struct sockaddr_in *addr = (const struct sockaddr_in *)(void *)walker->ai_addr;
get_ip4(&ip4.ip.v4, &addr->sin_addr);
result |= TOX_ADDR_RESOLVE_INET;
}
Expand All @@ -1358,14 +1358,14 @@ int addr_resolve(const char *address, IP *to, IP *extra)
case AF_INET6: {
if (walker->ai_family == family) { /* AF_INET6 requested, done */
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)walker->ai_addr;
const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)walker->ai_addr;
get_ip6(&to->ip.v6, &addr->sin6_addr);
result = TOX_ADDR_RESOLVE_INET6;
done = true;
}
} else if (!(result & TOX_ADDR_RESOLVE_INET6)) { /* AF_UNSPEC requested, store away */
if (walker->ai_addrlen == sizeof(struct sockaddr_in6)) {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)walker->ai_addr;
const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(void *)walker->ai_addr;
get_ip6(&ip6.ip.v6, &addr->sin6_addr);
result |= TOX_ADDR_RESOLVE_INET6;
}
Expand Down Expand Up @@ -1495,10 +1495,10 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type)
}

if (cur->ai_family == AF_INET) {
struct sockaddr_in *addr = (struct sockaddr_in *)(void *)cur->ai_addr;
const struct sockaddr_in *addr = (const struct sockaddr_in *)(const void *)cur->ai_addr;
memcpy(&ip_port->ip.ip.v4, &addr->sin_addr, sizeof(IP4));
} else if (cur->ai_family == AF_INET6) {
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)cur->ai_addr;
const struct sockaddr_in6 *addr = (const struct sockaddr_in6 *)(const void *)cur->ai_addr;
memcpy(&ip_port->ip.ip.v6, &addr->sin6_addr, sizeof(IP6));
} else {
continue;
Expand Down
4 changes: 2 additions & 2 deletions toxcore/onion_announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t *

int index;

uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE;
const uint8_t *data_public_key = plain + ONION_PING_ID_SIZE + CRYPTO_PUBLIC_KEY_SIZE;

if (onion_ping_id_eq(ping_id1, plain)
|| onion_ping_id_eq(ping_id2, plain)) {
Expand Down Expand Up @@ -461,7 +461,7 @@ static int handle_announce_request(void *object, IP_Port source, const uint8_t *

static int handle_data_request(void *object, IP_Port source, const uint8_t *packet, uint16_t length, void *userdata)
{
Onion_Announce *onion_a = (Onion_Announce *)object;
const Onion_Announce *onion_a = (const Onion_Announce *)object;

if (length <= DATA_REQUEST_MIN_SIZE_RECV) {
return 1;
Expand Down
6 changes: 3 additions & 3 deletions toxcore/onion_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ static int client_ping_nodes(Onion_Client *onion_c, uint32_t num, const Node_for
return 0;
}

Onion_Node *list_nodes = nullptr;
const Onion_Node *list_nodes = nullptr;
const uint8_t *reference_id = nullptr;
unsigned int list_length;

Expand Down Expand Up @@ -1037,7 +1037,7 @@ int send_onion_data(Onion_Client *onion_c, int friend_num, const uint8_t *data,
unsigned int good_nodes[MAX_ONION_CLIENTS];
unsigned int num_good = 0;
unsigned int num_nodes = 0;
Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list;
const Onion_Node *list_nodes = onion_c->friends_list[friend_num].clients_list;

for (unsigned int i = 0; i < MAX_ONION_CLIENTS; ++i) {
if (onion_node_timed_out(&list_nodes[i], onion_c->mono_time)) {
Expand Down Expand Up @@ -1709,7 +1709,7 @@ static void do_announce(Onion_Client *onion_c)

if (count != MAX_ONION_CLIENTS_ANNOUNCE) {
uint16_t num_nodes;
Node_format *path_nodes;
const Node_format *path_nodes;

if (random_u08() % 2 == 0 || onion_c->path_nodes_index == 0) {
num_nodes = min_u16(onion_c->path_nodes_index_bs, MAX_PATH_NODES);
Expand Down