Skip to content

Commit

Permalink
refactor: Assign malloc return to a local variable first.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Jan 10, 2024
1 parent afc38f2 commit 0426624
Show file tree
Hide file tree
Showing 32 changed files with 218 additions and 153 deletions.
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 @@
bc830120a87517f830eb85494b769c523bd1696328938d46e9eac1eefea61d38 /usr/local/bin/tox-bootstrapd
d5061af781d04d17bf26174c129b6149e0c8a120ef41133a51a8a7cc5e571e37 /usr/local/bin/tox-bootstrapd
2 changes: 1 addition & 1 deletion toxav/msi.c
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ static MSICall *new_call(MSISession *session, uint32_t friend_number)
session->calls_tail = friend_number;
session->calls_head = friend_number;
} 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) {
free(rc);
Expand Down
2 changes: 1 addition & 1 deletion toxav/toxav.c
Original file line number Diff line number Diff line change
Expand Up @@ -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_head = friend_number;
} 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) {
pthread_mutex_destroy(call->toxav_call_mutex);
Expand Down
18 changes: 11 additions & 7 deletions toxcore/DHT.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,8 @@ int dht_create_packet(const Memory *mem, const Random *rng,
const uint8_t *plain, size_t plain_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 *encrypted = (uint8_t *)mem_balloc(mem, plain_length + CRYPTO_MAC_SIZE);

if (encrypted == nullptr) {
return -1;
Expand Down Expand Up @@ -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);

// Copy to loaded_clients_list
dht->loaded_nodes_list = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));
Node_format *nodes = (Node_format *)mem_valloc(dht->mem, MAX_SAVED_DHT_NODES, sizeof(Node_format));

if (dht->loaded_nodes_list == nullptr) {
if (nodes == nullptr) {
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);
dht->loaded_num_nodes = 0;
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) {
dht->loaded_num_nodes = num;
} else {
if (num < 0) {
// Unpack error happened, we ignore it.
dht->loaded_num_nodes = 0;
} else {

Check warning on line 2926 in toxcore/DHT.c

View check run for this annotation

Codecov / codecov/patch

toxcore/DHT.c#L2926

Added line #L2926 was not covered by tests
dht->loaded_num_nodes = num;
}

dht->loaded_nodes_list = nodes;

break;
}

Expand Down
33 changes: 18 additions & 15 deletions toxcore/LAN_discovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,38 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
return nullptr;
}

IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));
unsigned long ulOutBufLen = sizeof(IP_ADAPTER_INFO);
IP_ADAPTER_INFO *adapter_info = (IP_ADAPTER_INFO *)malloc(sizeof(IP_ADAPTER_INFO));

if (pAdapterInfo == nullptr) {
if (adapter_info == nullptr) {
free(broadcast);
return nullptr;
}

if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
free(pAdapterInfo);
pAdapterInfo = (IP_ADAPTER_INFO *)malloc(ulOutBufLen);
unsigned long out_buf_len = sizeof(IP_ADAPTER_INFO);

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);
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) {
IP_ADAPTER_INFO *pAdapter = pAdapterInfo;
IP_ADAPTER_INFO *adapter = adapter_info;

while (pAdapter != nullptr) {
while (adapter != nullptr) {
IP gateway = {0};
IP subnet_mask = {0};

if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask)
&& addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) {
if (addr_parse_ip(adapter->IpAddressList.IpMask.String, &subnet_mask)
&& addr_parse_ip(adapter->GatewayList.IpAddress.String, &gateway)) {
if (net_family_is_ipv4(gateway.family) && net_family_is_ipv4(subnet_mask.family)) {
IP *ip = &broadcast->ips[broadcast->count];
ip->family = net_family_ipv4();
Expand All @@ -106,12 +109,12 @@ static Broadcast_Info *fetch_broadcast_info(const Network *ns)
}
}

pAdapter = pAdapter->Next;
adapter = adapter->Next;
}
}

if (pAdapterInfo != nullptr) {
free(pAdapterInfo);
if (adapter_info != nullptr) {
free(adapter_info);
}

return broadcast;
Expand Down
14 changes: 8 additions & 6 deletions toxcore/TCP_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,19 @@ static bool add_priority(TCP_Connection *con, const uint8_t *packet, uint16_t si
return false;
}

new_list->next = nullptr;
new_list->size = size;
new_list->sent = sent;
new_list->data = (uint8_t *)mem_balloc(con->mem, size);
uint8_t *data = (uint8_t *)mem_balloc(con->mem, size);

if (new_list->data == nullptr) {
if (data == nullptr) {
mem_delete(con->mem, new_list);
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) {
p->next = new_list;
Expand Down
8 changes: 5 additions & 3 deletions toxcore/TCP_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -968,20 +968,22 @@ TCP_Server *new_tcp_server(const Logger *logger, const Memory *mem, const Random
temp->ns = ns;
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");
mem_delete(mem, temp);
return nullptr;
}

temp->socks_listening = socks_listening;

#ifdef TCP_SERVER_USE_EPOLL
temp->efd = epoll_create(8);

if (temp->efd == -1) {
LOGGER_ERROR(logger, "epoll initialisation failed");
mem_delete(mem, temp->socks_listening);
mem_delete(mem, socks_listening);
mem_delete(mem, temp);
return nullptr;
}
Expand Down
8 changes: 5 additions & 3 deletions toxcore/announce.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,15 @@ bool announce_store_data(Announcements *announce, const uint8_t *data_public_key
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?

Check warning on line 249 in toxcore/announce.c

View check run for this annotation

Codecov / codecov/patch

toxcore/announce.c#L249

Added line #L249 was not covered by tests
return false;
}

memcpy(entry->data, data, length);
memcpy(entry_data, data, length);
entry->data = entry_data;
}

entry->length = length;
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/conference_invite.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,14 @@ static bool tox_event_conference_invite_set_cookie(Tox_Event_Conference_Invite *
conference_invite->cookie_length = 0;
}

conference_invite->cookie = (uint8_t *)malloc(cookie_length);
uint8_t *cookie_copy = (uint8_t *)malloc(cookie_length);

Check warning on line 84 in toxcore/events/conference_invite.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_invite.c#L84

Added line #L84 was not covered by tests

if (conference_invite->cookie == nullptr) {
if (cookie_copy == nullptr) {

Check warning on line 86 in toxcore/events/conference_invite.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_invite.c#L86

Added line #L86 was not covered by tests
return false;
}

memcpy(conference_invite->cookie, cookie, cookie_length);
memcpy(cookie_copy, cookie, cookie_length);
conference_invite->cookie = cookie_copy;

Check warning on line 91 in toxcore/events/conference_invite.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_invite.c#L90-L91

Added lines #L90 - L91 were not covered by tests
conference_invite->cookie_length = cookie_length;
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/conference_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,14 @@ static bool tox_event_conference_message_set_message(Tox_Event_Conference_Messag
conference_message->message_length = 0;
}

conference_message->message = (uint8_t *)malloc(message_length);
uint8_t *message_copy = (uint8_t *)malloc(message_length);

Check warning on line 98 in toxcore/events/conference_message.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_message.c#L98

Added line #L98 was not covered by tests

if (conference_message->message == nullptr) {
if (message_copy == nullptr) {

Check warning on line 100 in toxcore/events/conference_message.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_message.c#L100

Added line #L100 was not covered by tests
return false;
}

memcpy(conference_message->message, message, message_length);
memcpy(message_copy, message, message_length);
conference_message->message = message_copy;

Check warning on line 105 in toxcore/events/conference_message.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_message.c#L104-L105

Added lines #L104 - L105 were not covered by tests
conference_message->message_length = message_length;
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/conference_peer_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (uint8_t *)malloc(name_length);
uint8_t *name_copy = (uint8_t *)malloc(name_length);

Check warning on line 84 in toxcore/events/conference_peer_name.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_peer_name.c#L84

Added line #L84 was not covered by tests

if (conference_peer_name->name == nullptr) {
if (name_copy == nullptr) {

Check warning on line 86 in toxcore/events/conference_peer_name.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_peer_name.c#L86

Added line #L86 was not covered by tests
return false;
}

memcpy(conference_peer_name->name, name, name_length);
memcpy(name_copy, name, name_length);
conference_peer_name->name = name_copy;

Check warning on line 91 in toxcore/events/conference_peer_name.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_peer_name.c#L90-L91

Added lines #L90 - L91 were not covered by tests
conference_peer_name->name_length = name_length;
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/conference_title.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (uint8_t *)malloc(title_length);
uint8_t *title_copy = (uint8_t *)malloc(title_length);

Check warning on line 83 in toxcore/events/conference_title.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_title.c#L83

Added line #L83 was not covered by tests

if (conference_title->title == nullptr) {
if (title_copy == nullptr) {

Check warning on line 85 in toxcore/events/conference_title.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_title.c#L85

Added line #L85 was not covered by tests
return false;
}

memcpy(conference_title->title, title, title_length);
memcpy(title_copy, title, title_length);
conference_title->title = title_copy;

Check warning on line 90 in toxcore/events/conference_title.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/conference_title.c#L89-L90

Added lines #L89 - L90 were not covered by tests
conference_title->title_length = title_length;
return true;
}
Expand Down
15 changes: 9 additions & 6 deletions toxcore/events/events_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@ Tox_Events_State *tox_events_alloc(void *user_data)
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.
state->events = nullptr;

Check warning on line 27 in toxcore/events/events_alloc.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/events_alloc.c#L27

Added line #L27 was not covered by tests
state->error = TOX_ERR_EVENTS_ITERATE_MALLOC;
} else {
*state->events = (Tox_Events) {
nullptr
};
return state;

Check warning on line 29 in toxcore/events/events_alloc.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/events_alloc.c#L29

Added line #L29 was not covered by tests
}

*events = (Tox_Events) {
nullptr
};
state->events = events;

return state;
}

Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/file_recv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (uint8_t *)malloc(filename_length);
uint8_t *filename_copy = (uint8_t *)malloc(filename_length);

Check warning on line 111 in toxcore/events/file_recv.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/file_recv.c#L111

Added line #L111 was not covered by tests

if (file_recv->filename == nullptr) {
if (filename_copy == nullptr) {

Check warning on line 113 in toxcore/events/file_recv.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/file_recv.c#L113

Added line #L113 was not covered by tests
return false;
}

memcpy(file_recv->filename, filename, filename_length);
memcpy(filename_copy, filename, filename_length);
file_recv->filename = filename_copy;

Check warning on line 118 in toxcore/events/file_recv.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/file_recv.c#L117-L118

Added lines #L117 - L118 were not covered by tests
file_recv->filename_length = filename_length;
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/file_recv_chunk.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (uint8_t *)malloc(data_length);
uint8_t *data_copy = (uint8_t *)malloc(data_length);

Check warning on line 97 in toxcore/events/file_recv_chunk.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/file_recv_chunk.c#L97

Added line #L97 was not covered by tests

if (file_recv_chunk->data == nullptr) {
if (data_copy == nullptr) {

Check warning on line 99 in toxcore/events/file_recv_chunk.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/file_recv_chunk.c#L99

Added line #L99 was not covered by tests
return false;
}

memcpy(file_recv_chunk->data, data, data_length);
memcpy(data_copy, data, data_length);
file_recv_chunk->data = data_copy;

Check warning on line 104 in toxcore/events/file_recv_chunk.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/file_recv_chunk.c#L103-L104

Added lines #L103 - L104 were not covered by tests
file_recv_chunk->data_length = data_length;
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/friend_lossless_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (uint8_t *)malloc(data_length);
uint8_t *data_copy = (uint8_t *)malloc(data_length);

Check warning on line 70 in toxcore/events/friend_lossless_packet.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/friend_lossless_packet.c#L70

Added line #L70 was not covered by tests

if (friend_lossless_packet->data == nullptr) {
if (data_copy == nullptr) {

Check warning on line 72 in toxcore/events/friend_lossless_packet.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/friend_lossless_packet.c#L72

Added line #L72 was not covered by tests
return false;
}

memcpy(friend_lossless_packet->data, data, data_length);
memcpy(data_copy, data, data_length);
friend_lossless_packet->data = data_copy;

Check warning on line 77 in toxcore/events/friend_lossless_packet.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/friend_lossless_packet.c#L76-L77

Added lines #L76 - L77 were not covered by tests
friend_lossless_packet->data_length = data_length;
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/friend_lossy_packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (uint8_t *)malloc(data_length);
uint8_t *data_copy = (uint8_t *)malloc(data_length);

Check warning on line 69 in toxcore/events/friend_lossy_packet.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/friend_lossy_packet.c#L69

Added line #L69 was not covered by tests

if (friend_lossy_packet->data == nullptr) {
if (data_copy == nullptr) {

Check warning on line 71 in toxcore/events/friend_lossy_packet.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/friend_lossy_packet.c#L71

Added line #L71 was not covered by tests
return false;
}

memcpy(friend_lossy_packet->data, data, data_length);
memcpy(data_copy, data, data_length);
friend_lossy_packet->data = data_copy;

Check warning on line 76 in toxcore/events/friend_lossy_packet.c

View check run for this annotation

Codecov / codecov/patch

toxcore/events/friend_lossy_packet.c#L75-L76

Added lines #L75 - L76 were not covered by tests
friend_lossy_packet->data_length = data_length;
return true;
}
Expand Down
7 changes: 4 additions & 3 deletions toxcore/events/friend_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (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;
}

memcpy(friend_message->message, message, message_length);
memcpy(message_copy, message, message_length);
friend_message->message = message_copy;
friend_message->message_length = message_length;
return true;
}
Expand Down
Loading

0 comments on commit 0426624

Please sign in to comment.