mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
refactor: Assign malloc return to a local variable first.
This commit is contained in:
parent
afc38f2458
commit
0426624dcb
|
@ -1 +1 @@
|
||||||
bc830120a87517f830eb85494b769c523bd1696328938d46e9eac1eefea61d38 /usr/local/bin/tox-bootstrapd
|
d5061af781d04d17bf26174c129b6149e0c8a120ef41133a51a8a7cc5e571e37 /usr/local/bin/tox-bootstrapd
|
||||||
|
|
|
@ -587,7 +587,7 @@ static MSICall *new_call(MSISession *session, uint32_t friend_number)
|
||||||
session->calls_tail = friend_number;
|
session->calls_tail = friend_number;
|
||||||
session->calls_head = friend_number;
|
session->calls_head = friend_number;
|
||||||
} else if (session->calls_tail < friend_number) { /* Appending */
|
} else if (session->calls_tail < friend_number) { /* Appending */
|
||||||
MSICall **tmp = (MSICall **)realloc(session->calls, sizeof(MSICall *) * (friend_number + 1));
|
MSICall **tmp = (MSICall **)realloc(session->calls, (friend_number + 1) * sizeof(MSICall *));
|
||||||
|
|
||||||
if (tmp == nullptr) {
|
if (tmp == nullptr) {
|
||||||
free(rc);
|
free(rc);
|
||||||
|
|
|
@ -1284,7 +1284,7 @@ static ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, Toxav_Err_Call *er
|
||||||
av->calls_tail = friend_number;
|
av->calls_tail = friend_number;
|
||||||
av->calls_head = friend_number;
|
av->calls_head = friend_number;
|
||||||
} else if (av->calls_tail < friend_number) { /* Appending */
|
} else if (av->calls_tail < friend_number) { /* Appending */
|
||||||
ToxAVCall **tmp = (ToxAVCall **)realloc(av->calls, sizeof(ToxAVCall *) * (friend_number + 1));
|
ToxAVCall **tmp = (ToxAVCall **)realloc(av->calls, (friend_number + 1) * sizeof(ToxAVCall *));
|
||||||
|
|
||||||
if (tmp == nullptr) {
|
if (tmp == nullptr) {
|
||||||
pthread_mutex_destroy(call->toxav_call_mutex);
|
pthread_mutex_destroy(call->toxav_call_mutex);
|
||||||
|
|
|
@ -448,8 +448,8 @@ int dht_create_packet(const Memory *mem, const Random *rng,
|
||||||
const uint8_t *plain, size_t plain_length,
|
const uint8_t *plain, size_t plain_length,
|
||||||
uint8_t *packet, size_t length)
|
uint8_t *packet, size_t length)
|
||||||
{
|
{
|
||||||
uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE);
|
|
||||||
uint8_t nonce[CRYPTO_NONCE_SIZE];
|
uint8_t nonce[CRYPTO_NONCE_SIZE];
|
||||||
|
uint8_t *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE);
|
||||||
|
|
||||||
if (encrypted == nullptr) {
|
if (encrypted == nullptr) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -2908,23 +2908,27 @@ static State_Load_Status dht_load_state_callback(void *outer, const uint8_t *dat
|
||||||
}
|
}
|
||||||
|
|
||||||
mem_delete(dht->mem, dht->loaded_nodes_list);
|
mem_delete(dht->mem, dht->loaded_nodes_list);
|
||||||
// Copy to loaded_clients_list
|
|
||||||
dht->loaded_nodes_list = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
|
|
||||||
|
|
||||||
if (dht->loaded_nodes_list == nullptr) {
|
// Copy to loaded_clients_list
|
||||||
|
Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
|
||||||
|
|
||||||
|
if (nodes == nullptr) {
|
||||||
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
|
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
|
||||||
dht->loaded_num_nodes = 0;
|
dht->loaded_num_nodes = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int num = unpack_nodes(dht->loaded_nodes_list, MAX_SAVED_DHT_NODES, nullptr, data, length, false);
|
const int num = unpack_nodes(nodes, MAX_SAVED_DHT_NODES, nullptr, data, length, false);
|
||||||
|
|
||||||
if (num > 0) {
|
if (num < 0) {
|
||||||
dht->loaded_num_nodes = num;
|
// Unpack error happened, we ignore it.
|
||||||
} else {
|
|
||||||
dht->loaded_num_nodes = 0;
|
dht->loaded_num_nodes = 0;
|
||||||
|
} else {
|
||||||
|
dht->loaded_num_nodes = num;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dht->loaded_nodes_list = nodes;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -62,35 +62,38 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
|
IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
|
||||||
unsigned long ulOutBufLen = sizeof(IP_ADAPTER_INFO);
|
|
||||||
|
|
||||||
if (pAdapterInfo == nullptr) {
|
if (adapter_info == nullptr) {
|
||||||
free(broadcast);
|
free(broadcast);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
|
unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO);
|
||||||
free(pAdapterInfo);
|
|
||||||
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
|
|
||||||
|
|
||||||
if (pAdapterInfo == nullptr) {
|
if (GetAdaptersInfo(adapter_info, &out_buf_len) == ERROR_BUFFER_OVERFLOW) {
|
||||||
|
free(adapter_info);
|
||||||
|
IP_ADAPTER_INFO *new_adapter_info = (IP_ADAPTER_INFO *)malloc(out_buf_len);
|
||||||
|
|
||||||
|
if (new_adapter_info == nullptr) {
|
||||||
free(broadcast);
|
free(broadcast);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
adapter_info = new_adapter_info;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen);
|
const int ret = GetAdaptersInfo(adapter_info, &out_buf_len);
|
||||||
|
|
||||||
if (ret == NO_ERROR) {
|
if (ret == NO_ERROR) {
|
||||||
IP_ADAPTER_INFO *pAdapter = pAdapterInfo;
|
IP_ADAPTER_INFO *adapter = adapter_info;
|
||||||
|
|
||||||
while (pAdapter != nullptr) {
|
while (adapter != nullptr) {
|
||||||
IP gateway = {0};
|
IP gateway = {0};
|
||||||
IP subnet_mask = {0};
|
IP subnet_mask = {0};
|
||||||
|
|
||||||
if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask)
|
if (addr_parse_ip(adapter->IpAddressList.IpMask.String, &subnet_mask)
|
||||||
&& addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) {
|
&& addr_parse_ip(adapter->GatewayList.IpAddress.String, &gateway)) {
|
||||||
if (net_family_is_ipv4(gateway.family) && net_family_is_ipv4(subnet_mask.family)) {
|
if (net_family_is_ipv4(gateway.family) && net_family_is_ipv4(subnet_mask.family)) {
|
||||||
IP *ip = &broadcast->ips[broadcast->count];
|
IP *ip = &broadcast->ips[broadcast->count];
|
||||||
ip->family = net_family_ipv4();
|
ip->family = net_family_ipv4();
|
||||||
|
@ -106,12 +109,12 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pAdapter = pAdapter->Next;
|
adapter = adapter->Next;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pAdapterInfo != nullptr) {
|
if (adapter_info != nullptr) {
|
||||||
free(pAdapterInfo);
|
free(adapter_info);
|
||||||
}
|
}
|
||||||
|
|
||||||
return broadcast;
|
return broadcast;
|
||||||
|
|
|
@ -105,17 +105,19 @@ static bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t si
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
new_list->next = nullptr;
|
uint8_t *data = (uint8_t *)mem_balloc(con->mem, size);
|
||||||
new_list->size = size;
|
|
||||||
new_list->sent = sent;
|
|
||||||
new_list->data = (uint8_t *)mem_balloc(con->mem, size);
|
|
||||||
|
|
||||||
if (new_list->data == nullptr) {
|
if (data == nullptr) {
|
||||||
mem_delete(con->mem, new_list);
|
mem_delete(con->mem, new_list);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(new_list->data, packet, size);
|
memcpy(data, packet, size);
|
||||||
|
new_list->data = data;
|
||||||
|
new_list->size = size;
|
||||||
|
|
||||||
|
new_list->next = nullptr;
|
||||||
|
new_list->sent = sent;
|
||||||
|
|
||||||
if (p != nullptr) {
|
if (p != nullptr) {
|
||||||
p->next = new_list;
|
p->next = new_list;
|
||||||
|
|
|
@ -968,20 +968,22 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
|
||||||
temp->ns = ns;
|
temp->ns = ns;
|
||||||
temp->rng = rng;
|
temp->rng = rng;
|
||||||
|
|
||||||
temp->socks_listening = (Socket *)mem_valloc(mem, num_sockets, sizeof(Socket));
|
Socket *socks_listening = (Socket *)mem_valloc(mem, num_sockets, sizeof(Socket));
|
||||||
|
|
||||||
if (temp->socks_listening == nullptr) {
|
if (socks_listening == nullptr) {
|
||||||
LOGGER_ERROR(logger, "socket allocation failed");
|
LOGGER_ERROR(logger, "socket allocation failed");
|
||||||
mem_delete(mem, temp);
|
mem_delete(mem, temp);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
temp->socks_listening = socks_listening;
|
||||||
|
|
||||||
#ifdef TCP_SERVER_USE_EPOLL
|
#ifdef TCP_SERVER_USE_EPOLL
|
||||||
temp->efd = epoll_create(8);
|
temp->efd = epoll_create(8);
|
||||||
|
|
||||||
if (temp->efd == -1) {
|
if (temp->efd == -1) {
|
||||||
LOGGER_ERROR(logger, "epoll initialisation failed");
|
LOGGER_ERROR(logger, "epoll initialisation failed");
|
||||||
mem_delete(mem, temp->socks_listening);
|
mem_delete(mem, socks_listening);
|
||||||
mem_delete(mem, temp);
|
mem_delete(mem, temp);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
|
@ -243,13 +243,15 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key
|
||||||
free(entry->data);
|
free(entry->data);
|
||||||
}
|
}
|
||||||
|
|
||||||
entry->data = (uint8_t *)malloc(length);
|
uint8_t *entry_data = (uint8_t *)malloc(length);
|
||||||
|
|
||||||
if (entry->data == nullptr) {
|
if (entry_data == nullptr) {
|
||||||
|
entry->data = nullptr; // TODO(iphydf): Is this necessary?
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(entry->data, data, length);
|
memcpy(entry_data, data, length);
|
||||||
|
entry->data = entry_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
entry->length = length;
|
entry->length = length;
|
||||||
|
|
|
@ -81,13 +81,14 @@ static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *
|
||||||
conference_invite->cookie_length = 0;
|
conference_invite->cookie_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
conference_invite->cookie = (uint8_t *)malloc(cookie_length);
|
uint8_t *cookie_copy = (uint8_t *)malloc(cookie_length);
|
||||||
|
|
||||||
if (conference_invite->cookie == nullptr) {
|
if (cookie_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(conference_invite->cookie, cookie, cookie_length);
|
memcpy(cookie_copy, cookie, cookie_length);
|
||||||
|
conference_invite->cookie = cookie_copy;
|
||||||
conference_invite->cookie_length = cookie_length;
|
conference_invite->cookie_length = cookie_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -95,13 +95,14 @@ static bool tox_event_conference_message_set_message(Tox_Event_Conference_Messag
|
||||||
conference_message->message_length = 0;
|
conference_message->message_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
conference_message->message = (uint8_t *)malloc(message_length);
|
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||||
|
|
||||||
if (conference_message->message == nullptr) {
|
if (message_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(conference_message->message, message, message_length);
|
memcpy(message_copy, message, message_length);
|
||||||
|
conference_message->message = message_copy;
|
||||||
conference_message->message_length = message_length;
|
conference_message->message_length = message_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -81,13 +81,14 @@ static bool tox_event_conference_peer_name_set_name(Tox_Event_Conference_Peer_Na
|
||||||
conference_peer_name->name_length = 0;
|
conference_peer_name->name_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
conference_peer_name->name = (uint8_t *)malloc(name_length);
|
uint8_t *name_copy = (uint8_t *)malloc(name_length);
|
||||||
|
|
||||||
if (conference_peer_name->name == nullptr) {
|
if (name_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(conference_peer_name->name, name, name_length);
|
memcpy(name_copy, name, name_length);
|
||||||
|
conference_peer_name->name = name_copy;
|
||||||
conference_peer_name->name_length = name_length;
|
conference_peer_name->name_length = name_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,13 +80,14 @@ static bool tox_event_conference_title_set_title(Tox_Event_Conference_Title *con
|
||||||
conference_title->title_length = 0;
|
conference_title->title_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
conference_title->title = (uint8_t *)malloc(title_length);
|
uint8_t *title_copy = (uint8_t *)malloc(title_length);
|
||||||
|
|
||||||
if (conference_title->title == nullptr) {
|
if (title_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(conference_title->title, title, title_length);
|
memcpy(title_copy, title, title_length);
|
||||||
|
conference_title->title = title_copy;
|
||||||
conference_title->title_length = title_length;
|
conference_title->title_length = title_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,16 +20,19 @@ Tox_Events_State *tox_events_alloc(void *user_data)
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
state->events = (Tox_Events *)calloc(1, sizeof(Tox_Events));
|
Tox_Events *events = (Tox_Events *)calloc(1, sizeof(Tox_Events));
|
||||||
|
|
||||||
if (state->events == nullptr) {
|
if (events == nullptr) {
|
||||||
// It's still null => allocation failed.
|
// It's still null => allocation failed.
|
||||||
|
state->events = nullptr;
|
||||||
state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
|
state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
|
||||||
} else {
|
return state;
|
||||||
*state->events = (Tox_Events) {
|
}
|
||||||
|
|
||||||
|
*events = (Tox_Events) {
|
||||||
nullptr
|
nullptr
|
||||||
};
|
};
|
||||||
}
|
state->events = events;
|
||||||
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
|
@ -108,13 +108,14 @@ static bool tox_event_file_recv_set_filename(Tox_Event_File_Recv *file_recv, con
|
||||||
file_recv->filename_length = 0;
|
file_recv->filename_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_recv->filename = (uint8_t *)malloc(filename_length);
|
uint8_t *filename_copy = (uint8_t *)malloc(filename_length);
|
||||||
|
|
||||||
if (file_recv->filename == nullptr) {
|
if (filename_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(file_recv->filename, filename, filename_length);
|
memcpy(filename_copy, filename, filename_length);
|
||||||
|
file_recv->filename = filename_copy;
|
||||||
file_recv->filename_length = filename_length;
|
file_recv->filename_length = filename_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -94,13 +94,14 @@ static bool tox_event_file_recv_chunk_set_data(Tox_Event_File_Recv_Chunk *file_r
|
||||||
file_recv_chunk->data_length = 0;
|
file_recv_chunk->data_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
file_recv_chunk->data = (uint8_t *)malloc(data_length);
|
uint8_t *data_copy = (uint8_t *)malloc(data_length);
|
||||||
|
|
||||||
if (file_recv_chunk->data == nullptr) {
|
if (data_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(file_recv_chunk->data, data, data_length);
|
memcpy(data_copy, data, data_length);
|
||||||
|
file_recv_chunk->data = data_copy;
|
||||||
file_recv_chunk->data_length = data_length;
|
file_recv_chunk->data_length = data_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,13 +67,14 @@ static bool tox_event_friend_lossless_packet_set_data(Tox_Event_Friend_Lossless_
|
||||||
friend_lossless_packet->data_length = 0;
|
friend_lossless_packet->data_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend_lossless_packet->data = (uint8_t *)malloc(data_length);
|
uint8_t *data_copy = (uint8_t *)malloc(data_length);
|
||||||
|
|
||||||
if (friend_lossless_packet->data == nullptr) {
|
if (data_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(friend_lossless_packet->data, data, data_length);
|
memcpy(data_copy, data, data_length);
|
||||||
|
friend_lossless_packet->data = data_copy;
|
||||||
friend_lossless_packet->data_length = data_length;
|
friend_lossless_packet->data_length = data_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,13 +66,14 @@ static bool tox_event_friend_lossy_packet_set_data(Tox_Event_Friend_Lossy_Packet
|
||||||
friend_lossy_packet->data_length = 0;
|
friend_lossy_packet->data_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend_lossy_packet->data = (uint8_t *)malloc(data_length);
|
uint8_t *data_copy = (uint8_t *)malloc(data_length);
|
||||||
|
|
||||||
if (friend_lossy_packet->data == nullptr) {
|
if (data_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(friend_lossy_packet->data, data, data_length);
|
memcpy(data_copy, data, data_length);
|
||||||
|
friend_lossy_packet->data = data_copy;
|
||||||
friend_lossy_packet->data_length = data_length;
|
friend_lossy_packet->data_length = data_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,13 +80,14 @@ static bool tox_event_friend_message_set_message(Tox_Event_Friend_Message *frien
|
||||||
friend_message->message_length = 0;
|
friend_message->message_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend_message->message = (uint8_t *)malloc(message_length);
|
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||||
|
|
||||||
if (friend_message->message == nullptr) {
|
if (message_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(friend_message->message, message, message_length);
|
memcpy(message_copy, message, message_length);
|
||||||
|
friend_message->message = message_copy;
|
||||||
friend_message->message_length = message_length;
|
friend_message->message_length = message_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,13 +66,14 @@ static bool tox_event_friend_name_set_name(Tox_Event_Friend_Name *friend_name, c
|
||||||
friend_name->name_length = 0;
|
friend_name->name_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend_name->name = (uint8_t *)malloc(name_length);
|
uint8_t *name_copy = (uint8_t *)malloc(name_length);
|
||||||
|
|
||||||
if (friend_name->name == nullptr) {
|
if (name_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(friend_name->name, name, name_length);
|
memcpy(name_copy, name, name_length);
|
||||||
|
friend_name->name = name_copy;
|
||||||
friend_name->name_length = name_length;
|
friend_name->name_length = name_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,13 +67,14 @@ static bool tox_event_friend_request_set_message(Tox_Event_Friend_Request *frien
|
||||||
friend_request->message_length = 0;
|
friend_request->message_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend_request->message = (uint8_t *)malloc(message_length);
|
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||||
|
|
||||||
if (friend_request->message == nullptr) {
|
if (message_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(friend_request->message, message, message_length);
|
memcpy(message_copy, message, message_length);
|
||||||
|
friend_request->message = message_copy;
|
||||||
friend_request->message_length = message_length;
|
friend_request->message_length = message_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -66,13 +66,14 @@ static bool tox_event_friend_status_message_set_message(Tox_Event_Friend_Status_
|
||||||
friend_status_message->message_length = 0;
|
friend_status_message->message_length = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
friend_status_message->message = (uint8_t *)malloc(message_length);
|
uint8_t *message_copy = (uint8_t *)malloc(message_length);
|
||||||
|
|
||||||
if (friend_status_message->message == nullptr) {
|
if (message_copy == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(friend_status_message->message, message, message_length);
|
memcpy(message_copy, message, message_length);
|
||||||
|
friend_status_message->message = message_copy;
|
||||||
friend_status_message->message_length = message_length;
|
friend_status_message->message_length = message_length;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -684,7 +684,7 @@ static bool delete_frozen(Group_c *g, uint32_t frozen_index)
|
||||||
g->frozen[frozen_index] = g->frozen[g->numfrozen];
|
g->frozen[frozen_index] = g->frozen[g->numfrozen];
|
||||||
}
|
}
|
||||||
|
|
||||||
Group_Peer *const frozen_temp = (Group_Peer *)realloc(g->frozen, sizeof(Group_Peer) * g->numfrozen);
|
Group_Peer *const frozen_temp = (Group_Peer *)realloc(g->frozen, g->numfrozen * sizeof(Group_Peer));
|
||||||
|
|
||||||
if (frozen_temp == nullptr) {
|
if (frozen_temp == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -725,7 +725,7 @@ static int note_peer_active(Group_Chats *g_c, uint32_t groupnumber, uint16_t pee
|
||||||
|
|
||||||
/* Now thaw the peer */
|
/* Now thaw the peer */
|
||||||
|
|
||||||
Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers + 1));
|
Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer));
|
||||||
|
|
||||||
if (temp == nullptr) {
|
if (temp == nullptr) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -838,7 +838,7 @@ static int addpeer(Group_Chats *g_c, uint32_t groupnumber, const uint8_t *real_p
|
||||||
|
|
||||||
delete_any_peer_with_pk(g_c, groupnumber, real_pk, userdata);
|
delete_any_peer_with_pk(g_c, groupnumber, real_pk, userdata);
|
||||||
|
|
||||||
Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * (g->numpeers + 1));
|
Group_Peer *temp = (Group_Peer *)realloc(g->group, (g->numpeers + 1) * sizeof(Group_Peer));
|
||||||
|
|
||||||
if (temp == nullptr) {
|
if (temp == nullptr) {
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -936,7 +936,7 @@ static bool delpeer(Group_Chats *g_c, uint32_t groupnumber, int peer_index, void
|
||||||
g->group[peer_index] = g->group[g->numpeers];
|
g->group[peer_index] = g->group[g->numpeers];
|
||||||
}
|
}
|
||||||
|
|
||||||
Group_Peer *temp = (Group_Peer *)realloc(g->group, sizeof(Group_Peer) * g->numpeers);
|
Group_Peer *temp = (Group_Peer *)realloc(g->group, g->numpeers * sizeof(Group_Peer));
|
||||||
|
|
||||||
if (temp == nullptr) {
|
if (temp == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -995,7 +995,7 @@ static bool delete_old_frozen(Group_c *g)
|
||||||
|
|
||||||
qsort(g->frozen, g->numfrozen, sizeof(Group_Peer), cmp_frozen);
|
qsort(g->frozen, g->numfrozen, sizeof(Group_Peer), cmp_frozen);
|
||||||
|
|
||||||
Group_Peer *temp = (Group_Peer *)realloc(g->frozen, sizeof(Group_Peer) * g->maxfrozen);
|
Group_Peer *temp = (Group_Peer *)realloc(g->frozen, g->maxfrozen * sizeof(Group_Peer));
|
||||||
|
|
||||||
if (temp == nullptr) {
|
if (temp == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -1020,7 +1020,7 @@ static bool freeze_peer(Group_Chats *g_c, uint32_t groupnumber, int peer_index,
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Group_Peer *temp = (Group_Peer *)realloc(g->frozen, sizeof(Group_Peer) * (g->numfrozen + 1));
|
Group_Peer *temp = (Group_Peer *)realloc(g->frozen, (g->numfrozen + 1) * sizeof(Group_Peer));
|
||||||
|
|
||||||
if (temp == nullptr) {
|
if (temp == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -339,18 +339,12 @@ int gca_unpack_announces_list(const Logger *log, const uint8_t *data, uint16_t l
|
||||||
return announces_count;
|
return announces_count;
|
||||||
}
|
}
|
||||||
|
|
||||||
GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list,
|
non_null()
|
||||||
|
static GC_Announces *gca_new_announces(
|
||||||
|
GC_Announces_List *gc_announces_list,
|
||||||
const GC_Public_Announce *public_announce)
|
const GC_Public_Announce *public_announce)
|
||||||
{
|
{
|
||||||
if (gc_announces_list == nullptr || public_announce == nullptr) {
|
GC_Announces *announces = (GC_Announces *)calloc(1, sizeof(GC_Announces));
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key);
|
|
||||||
|
|
||||||
// No entry for this chat_id exists so we create one
|
|
||||||
if (announces == nullptr) {
|
|
||||||
announces = (GC_Announces *)calloc(1, sizeof(GC_Announces));
|
|
||||||
|
|
||||||
if (announces == nullptr) {
|
if (announces == nullptr) {
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -366,6 +360,26 @@ GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List
|
||||||
announces->next_announce = gc_announces_list->root_announces;
|
announces->next_announce = gc_announces_list->root_announces;
|
||||||
gc_announces_list->root_announces = announces;
|
gc_announces_list->root_announces = announces;
|
||||||
memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE);
|
memcpy(announces->chat_id, public_announce->chat_public_key, CHAT_ID_SIZE);
|
||||||
|
|
||||||
|
return announces;
|
||||||
|
}
|
||||||
|
|
||||||
|
GC_Peer_Announce *gca_add_announce(const Mono_Time *mono_time, GC_Announces_List *gc_announces_list,
|
||||||
|
const GC_Public_Announce *public_announce)
|
||||||
|
{
|
||||||
|
if (gc_announces_list == nullptr || public_announce == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
GC_Announces *announces = get_announces_by_chat_id(gc_announces_list, public_announce->chat_public_key);
|
||||||
|
|
||||||
|
// No entry for this chat_id exists so we create one
|
||||||
|
if (announces == nullptr) {
|
||||||
|
announces = gca_new_announces(gc_announces_list, public_announce);
|
||||||
|
|
||||||
|
if (announces == nullptr) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint64_t cur_time = mono_time_get(mono_time);
|
const uint64_t cur_time = mono_time_get(mono_time);
|
||||||
|
@ -396,8 +410,7 @@ bool gca_is_valid_announce(const GC_Announce *announce)
|
||||||
|
|
||||||
GC_Announces_List *new_gca_list(void)
|
GC_Announces_List *new_gca_list(void)
|
||||||
{
|
{
|
||||||
GC_Announces_List *announces_list = (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List));
|
return (GC_Announces_List *)calloc(1, sizeof(GC_Announces_List));
|
||||||
return announces_list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void kill_gca(GC_Announces_List *announces_list)
|
void kill_gca(GC_Announces_List *announces_list)
|
||||||
|
|
|
@ -92,23 +92,27 @@ non_null(1, 2) nullable(3)
|
||||||
static bool create_array_entry(const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry, const uint8_t *data,
|
static bool create_array_entry(const Mono_Time *mono_time, GC_Message_Array_Entry *array_entry, const uint8_t *data,
|
||||||
uint16_t length, uint8_t packet_type, uint64_t message_id)
|
uint16_t length, uint8_t packet_type, uint64_t message_id)
|
||||||
{
|
{
|
||||||
if (length > 0) {
|
if (length == 0) {
|
||||||
|
array_entry->data = nullptr;
|
||||||
|
array_entry->data_length = 0;
|
||||||
|
} else {
|
||||||
if (data == nullptr) {
|
if (data == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
array_entry->data = (uint8_t *)malloc(sizeof(uint8_t) * length);
|
uint8_t *entry_data = (uint8_t *)malloc(length);
|
||||||
|
|
||||||
if (array_entry->data == nullptr) {
|
if (entry_data == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(array_entry->data, data, length);
|
memcpy(entry_data, data, length);
|
||||||
|
array_entry->data = entry_data;
|
||||||
|
array_entry->data_length = length;
|
||||||
}
|
}
|
||||||
|
|
||||||
const uint64_t tm = mono_time_get(mono_time);
|
const uint64_t tm = mono_time_get(mono_time);
|
||||||
|
|
||||||
array_entry->data_length = length;
|
|
||||||
array_entry->packet_type = packet_type;
|
array_entry->packet_type = packet_type;
|
||||||
array_entry->message_id = message_id;
|
array_entry->message_id = message_id;
|
||||||
array_entry->time_added = tm;
|
array_entry->time_added = tm;
|
||||||
|
@ -392,10 +396,9 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(*payload == nullptr);
|
uint8_t *tmp_payload = (uint8_t *)malloc(packet_length);
|
||||||
*payload = (uint8_t *)malloc(packet_length);
|
|
||||||
|
|
||||||
if (*payload == nullptr) {
|
if (tmp_payload == nullptr) {
|
||||||
LOGGER_ERROR(log, "Failed to allocate %u bytes for payload buffer", packet_length);
|
LOGGER_ERROR(log, "Failed to allocate %u bytes for payload buffer", packet_length);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -409,12 +412,15 @@ static uint16_t reassemble_packet(const Logger *log, GC_Connection *gconn, uint8
|
||||||
entry = &gconn->recv_array[i];
|
entry = &gconn->recv_array[i];
|
||||||
|
|
||||||
assert(processed + entry->data_length <= packet_length);
|
assert(processed + entry->data_length <= packet_length);
|
||||||
memcpy(*payload + processed, entry->data, entry->data_length);
|
memcpy(tmp_payload + processed, entry->data, entry->data_length);
|
||||||
processed += entry->data_length;
|
processed += entry->data_length;
|
||||||
|
|
||||||
clear_array_entry(entry);
|
clear_array_entry(entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(*payload == nullptr);
|
||||||
|
*payload = tmp_payload;
|
||||||
|
|
||||||
return processed;
|
return processed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -59,14 +59,16 @@ int mod_list_unpack(Moderation *moderation, const uint8_t *data, uint16_t length
|
||||||
uint16_t unpacked_len = 0;
|
uint16_t unpacked_len = 0;
|
||||||
|
|
||||||
for (uint16_t i = 0; i < num_mods; ++i) {
|
for (uint16_t i = 0; i < num_mods; ++i) {
|
||||||
tmp_list[i] = (uint8_t *)malloc(sizeof(uint8_t) * MOD_LIST_ENTRY_SIZE);
|
uint8_t *entry = (uint8_t *)malloc(MOD_LIST_ENTRY_SIZE);
|
||||||
|
|
||||||
if (tmp_list[i] == nullptr) {
|
if (entry == nullptr) {
|
||||||
free_uint8_t_pointer_array(moderation->mem, tmp_list, i);
|
free_uint8_t_pointer_array(moderation->mem, tmp_list, i);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(tmp_list[i], &data[i * MOD_LIST_ENTRY_SIZE], MOD_LIST_ENTRY_SIZE);
|
memcpy(entry, &data[i * MOD_LIST_ENTRY_SIZE], MOD_LIST_ENTRY_SIZE);
|
||||||
|
tmp_list[i] = entry;
|
||||||
|
|
||||||
unpacked_len += MOD_LIST_ENTRY_SIZE;
|
unpacked_len += MOD_LIST_ENTRY_SIZE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -208,13 +210,15 @@ bool mod_list_add_entry(Moderation *moderation, const uint8_t *mod_data)
|
||||||
|
|
||||||
moderation->mod_list = tmp_list;
|
moderation->mod_list = tmp_list;
|
||||||
|
|
||||||
tmp_list[moderation->num_mods] = (uint8_t *)malloc(sizeof(uint8_t) * MOD_LIST_ENTRY_SIZE);
|
uint8_t *entry = (uint8_t *)malloc(MOD_LIST_ENTRY_SIZE);
|
||||||
|
|
||||||
if (tmp_list[moderation->num_mods] == nullptr) {
|
if (entry == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(tmp_list[moderation->num_mods], mod_data, MOD_LIST_ENTRY_SIZE);
|
memcpy(entry, mod_data, MOD_LIST_ENTRY_SIZE);
|
||||||
|
|
||||||
|
tmp_list[moderation->num_mods] = entry;
|
||||||
++moderation->num_mods;
|
++moderation->num_mods;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
|
@ -123,7 +123,7 @@ static bool resize(BS_List *list, uint32_t new_size)
|
||||||
|
|
||||||
list->data = data;
|
list->data = data;
|
||||||
|
|
||||||
int *ids = (int *)realloc(list->ids, sizeof(int) * new_size);
|
int *ids = (int *)realloc(list->ids, new_size * sizeof(int));
|
||||||
|
|
||||||
if (ids == nullptr) {
|
if (ids == nullptr) {
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -119,18 +119,20 @@ Mono_Time *mono_time_new(const Memory *mem, mono_time_current_time_cb *current_t
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef ESP_PLATFORM
|
#ifndef ESP_PLATFORM
|
||||||
mono_time->time_update_lock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t));
|
pthread_rwlock_t *rwlock = (pthread_rwlock_t *)mem_alloc(mem, sizeof(pthread_rwlock_t));
|
||||||
|
|
||||||
if (mono_time->time_update_lock == nullptr) {
|
if (rwlock == nullptr) {
|
||||||
mem_delete(mem, mono_time);
|
mem_delete(mem, mono_time);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pthread_rwlock_init(mono_time->time_update_lock, nullptr) != 0) {
|
if (pthread_rwlock_init(rwlock, nullptr) != 0) {
|
||||||
mem_delete(mem, mono_time->time_update_lock);
|
mem_delete(mem, rwlock);
|
||||||
mem_delete(mem, mono_time);
|
mem_delete(mem, mono_time);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mono_time->time_update_lock = rwlock;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mono_time_set_current_time_callback(mono_time, current_time_callback, user_data);
|
mono_time_set_current_time_callback(mono_time, current_time_callback, user_data);
|
||||||
|
|
|
@ -1868,25 +1868,26 @@ static int create_crypto_connection(Net_Crypto *c)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (id != -1) {
|
if (id != -1) {
|
||||||
|
pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t));
|
||||||
|
|
||||||
|
if (mutex == nullptr) {
|
||||||
|
pthread_mutex_unlock(&c->connections_mutex);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pthread_mutex_init(mutex, nullptr) != 0) {
|
||||||
|
mem_delete(c->mem, mutex);
|
||||||
|
pthread_mutex_unlock(&c->connections_mutex);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
// Memsetting float/double to 0 is non-portable, so we explicitly set them to 0
|
// Memsetting float/double to 0 is non-portable, so we explicitly set them to 0
|
||||||
c->crypto_connections[id].packet_recv_rate = 0;
|
c->crypto_connections[id].packet_recv_rate = 0.0;
|
||||||
c->crypto_connections[id].packet_send_rate = 0;
|
c->crypto_connections[id].packet_send_rate = 0.0;
|
||||||
c->crypto_connections[id].last_packets_left_rem = 0;
|
c->crypto_connections[id].last_packets_left_rem = 0.0;
|
||||||
c->crypto_connections[id].packet_send_rate_requested = 0;
|
c->crypto_connections[id].packet_send_rate_requested = 0.0;
|
||||||
c->crypto_connections[id].last_packets_left_requested_rem = 0;
|
c->crypto_connections[id].last_packets_left_requested_rem = 0.0;
|
||||||
c->crypto_connections[id].mutex = (pthread_mutex_t *)mem_alloc(c->mem, sizeof(pthread_mutex_t));
|
c->crypto_connections[id].mutex = mutex;
|
||||||
|
|
||||||
if (c->crypto_connections[id].mutex == nullptr) {
|
|
||||||
pthread_mutex_unlock(&c->connections_mutex);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pthread_mutex_init(c->crypto_connections[id].mutex, nullptr) != 0) {
|
|
||||||
mem_delete(c->mem, c->crypto_connections[id].mutex);
|
|
||||||
pthread_mutex_unlock(&c->connections_mutex);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
c->crypto_connections[id].status = CRYPTO_CONN_NO_CONNECTION;
|
c->crypto_connections[id].status = CRYPTO_CONN_NO_CONNECTION;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2022,13 +2023,14 @@ non_null(1, 2, 3) nullable(5)
|
||||||
static int handle_new_connection_handshake(Net_Crypto *c, const IP_Port *source, const uint8_t *data, uint16_t length,
|
static int handle_new_connection_handshake(Net_Crypto *c, const IP_Port *source, const uint8_t *data, uint16_t length,
|
||||||
void *userdata)
|
void *userdata)
|
||||||
{
|
{
|
||||||
New_Connection n_c;
|
uint8_t *cookie = (uint8_t *)mem_balloc(c->mem, COOKIE_LENGTH);
|
||||||
n_c.cookie = (uint8_t *)mem_balloc(c->mem, COOKIE_LENGTH);
|
|
||||||
|
|
||||||
if (n_c.cookie == nullptr) {
|
if (cookie == nullptr) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
New_Connection n_c = {{{{0}}}};
|
||||||
|
n_c.cookie = cookie;
|
||||||
n_c.source = *source;
|
n_c.source = *source;
|
||||||
n_c.cookie_length = COOKIE_LENGTH;
|
n_c.cookie_length = COOKIE_LENGTH;
|
||||||
|
|
||||||
|
|
|
@ -1796,12 +1796,14 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to
|
||||||
|
|
||||||
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
|
||||||
if ((true)) {
|
if ((true)) {
|
||||||
*res = (IP_Port *)mem_alloc(mem, sizeof(IP_Port));
|
IP_Port *ip_port = (IP_Port *)mem_alloc(mem, sizeof(IP_Port));
|
||||||
assert(*res != nullptr);
|
if (ip_port == nullptr) {
|
||||||
IP_Port *ip_port = *res;
|
abort();
|
||||||
|
}
|
||||||
ip_port->ip.ip.v4.uint32 = net_htonl(0x7F000003); // 127.0.0.3
|
ip_port->ip.ip.v4.uint32 = net_htonl(0x7F000003); // 127.0.0.3
|
||||||
ip_port->ip.family = *make_tox_family(AF_INET);
|
ip_port->ip.family = *make_tox_family(AF_INET);
|
||||||
|
|
||||||
|
*res = ip_port;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1838,14 +1840,15 @@ int32_t net_getipport(const Memory *mem, const char *node, IP_Port **res, int to
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
*res = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port));
|
IP_Port *ip_port = (IP_Port *)mem_valloc(mem, count, sizeof(IP_Port));
|
||||||
|
|
||||||
if (*res == nullptr) {
|
if (ip_port == nullptr) {
|
||||||
freeaddrinfo(infos);
|
freeaddrinfo(infos);
|
||||||
|
*res = nullptr;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
IP_Port *ip_port = *res;
|
*res = ip_port;
|
||||||
|
|
||||||
for (struct addrinfo *cur = infos; cur != nullptr; cur = cur->ai_next) {
|
for (struct addrinfo *cur = infos; cur != nullptr; cur = cur->ai_next) {
|
||||||
if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) {
|
if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) {
|
||||||
|
|
|
@ -49,14 +49,15 @@ Ping_Array *ping_array_new(const Memory *mem, uint32_t size, uint32_t timeout)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
empty_array->mem = mem;
|
Ping_Array_Entry *entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry));
|
||||||
empty_array->entries = (Ping_Array_Entry *)mem_valloc(mem, size, sizeof(Ping_Array_Entry));
|
|
||||||
|
|
||||||
if (empty_array->entries == nullptr) {
|
if (entries == nullptr) {
|
||||||
mem_delete(mem, empty_array);
|
mem_delete(mem, empty_array);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
empty_array->mem = mem;
|
||||||
|
empty_array->entries = entries;
|
||||||
empty_array->last_deleted = 0;
|
empty_array->last_deleted = 0;
|
||||||
empty_array->last_added = 0;
|
empty_array->last_added = 0;
|
||||||
empty_array->total_size = size;
|
empty_array->total_size = size;
|
||||||
|
@ -115,13 +116,16 @@ uint64_t ping_array_add(Ping_Array *array, const Mono_Time *mono_time, const Ran
|
||||||
clear_entry(array, index);
|
clear_entry(array, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
array->entries[index].data = (uint8_t *)mem_balloc(array->mem, length);
|
uint8_t *entry_data = (uint8_t *)mem_balloc(array->mem, length);
|
||||||
|
|
||||||
if (array->entries[index].data == nullptr) {
|
if (entry_data == nullptr) {
|
||||||
|
array->entries[index].data = nullptr;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(array->entries[index].data, data, length);
|
memcpy(entry_data, data, length);
|
||||||
|
|
||||||
|
array->entries[index].data = entry_data;
|
||||||
array->entries[index].length = length;
|
array->entries[index].length = length;
|
||||||
array->entries[index].ping_time = mono_time_get(mono_time);
|
array->entries[index].ping_time = mono_time_get(mono_time);
|
||||||
++array->last_added;
|
++array->last_added;
|
||||||
|
|
|
@ -68,16 +68,19 @@ Shared_Key_Cache *shared_key_cache_new(const Logger *log, const Mono_Time *mono_
|
||||||
res->mem = mem;
|
res->mem = mem;
|
||||||
res->log = log;
|
res->log = log;
|
||||||
res->keys_per_slot = keys_per_slot;
|
res->keys_per_slot = keys_per_slot;
|
||||||
|
|
||||||
// We take one byte from the public key for each bucket and store keys_per_slot elements there
|
// We take one byte from the public key for each bucket and store keys_per_slot elements there
|
||||||
const size_t cache_size = 256 * keys_per_slot;
|
const size_t cache_size = 256 * keys_per_slot;
|
||||||
res->keys = (Shared_Key *)mem_valloc(mem, cache_size, sizeof(Shared_Key));
|
Shared_Key *keys = (Shared_Key *)mem_valloc(mem, cache_size, sizeof(Shared_Key));
|
||||||
|
|
||||||
if (res->keys == nullptr) {
|
if (keys == nullptr) {
|
||||||
mem_delete(mem, res);
|
mem_delete(mem, res);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
crypto_memlock(res->keys, cache_size * sizeof(Shared_Key));
|
crypto_memlock(keys, cache_size * sizeof(Shared_Key));
|
||||||
|
|
||||||
|
res->keys = keys;
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -796,21 +796,21 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tox_options_get_experimental_thread_safety(opts)) {
|
if (tox_options_get_experimental_thread_safety(opts)) {
|
||||||
tox->mutex = (pthread_mutex_t *)mem_alloc(sys->mem, sizeof(pthread_mutex_t));
|
pthread_mutex_t *mutex = (pthread_mutex_t *)mem_alloc(sys->mem, sizeof(pthread_mutex_t));
|
||||||
|
|
||||||
if (tox->mutex == nullptr) {
|
if (mutex == nullptr) {
|
||||||
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
|
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);
|
||||||
mem_delete(sys->mem, tox);
|
mem_delete(sys->mem, tox);
|
||||||
tox_options_free(default_options);
|
tox_options_free(default_options);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pthread_mutexattr_t attr;
|
pthread_mutexattr_t attr;
|
||||||
|
|
||||||
pthread_mutexattr_init(&attr);
|
pthread_mutexattr_init(&attr);
|
||||||
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
||||||
pthread_mutex_init(tox->mutex, &attr);
|
pthread_mutex_init(mutex, &attr);
|
||||||
|
|
||||||
|
tox->mutex = mutex;
|
||||||
} else {
|
} else {
|
||||||
tox->mutex = nullptr;
|
tox->mutex = nullptr;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user