Skip to content

Commit 73484d8

Browse files
committed
cleanup: Use calloc instead of malloc for struct allocations.
`malloc` is used only for byte/int arrays. Also, we no longer allow plain `void *` allocations. Every allocation should have a real value type.
1 parent 9ab77e0 commit 73484d8

File tree

12 files changed

+19
-19
lines changed

12 files changed

+19
-19
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
35800dfd68ba005a6884d7fcf1d5a76614d7afa3d53a7bf12c6fd6398afa48fd /usr/local/bin/tox-bootstrapd
1+
6ca8302e5d61c8b40f4c9eb14e16d4ff1e283fe594b90b268a8ad022d0c28128 /usr/local/bin/tox-bootstrapd

toxcore/DHT.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2503,7 +2503,7 @@ void dht_save(const DHT *dht, uint8_t *data)
25032503
/* get right offset. we write the actual header later. */
25042504
data = state_write_section_header(data, DHT_STATE_COOKIE_TYPE, 0, 0);
25052505

2506-
Node_format *clients = (Node_format *)malloc(MAX_SAVED_DHT_NODES * sizeof(Node_format));
2506+
Node_format *clients = (Node_format *)calloc(MAX_SAVED_DHT_NODES, sizeof(Node_format));
25072507

25082508
if (clients == nullptr) {
25092509
LOGGER_ERROR(dht->log, "could not allocate %u nodes", MAX_SAVED_DHT_NODES);

toxcore/TCP_client.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,13 +374,13 @@ static int client_send_pending_data(TCP_Client_Connection *con)
374374
return -1;
375375
}
376376

377-
/** return 0 on failure (only if malloc fails)
377+
/** return 0 on failure (only if calloc fails)
378378
* return 1 on success
379379
*/
380380
static bool client_add_priority(TCP_Client_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent)
381381
{
382382
TCP_Priority_List *p = con->priority_queue_end;
383-
TCP_Priority_List *new_list = (TCP_Priority_List *)malloc(sizeof(TCP_Priority_List) + size);
383+
TCP_Priority_List *new_list = (TCP_Priority_List *)calloc(1, sizeof(TCP_Priority_List) + size);
384384

385385
if (!new_list) {
386386
return 0;

toxcore/TCP_server.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,13 +429,13 @@ static int send_pending_data(TCP_Secure_Connection *con)
429429
return -1;
430430
}
431431

432-
/** return 0 on failure (only if malloc fails)
432+
/** return 0 on failure (only if calloc fails)
433433
* return 1 on success
434434
*/
435435
static bool add_priority(TCP_Secure_Connection *con, const uint8_t *packet, uint16_t size, uint16_t sent)
436436
{
437437
TCP_Priority_List *p = con->priority_queue_end;
438-
TCP_Priority_List *new_list = (TCP_Priority_List *)malloc(sizeof(TCP_Priority_List) + size);
438+
TCP_Priority_List *new_list = (TCP_Priority_List *)calloc(1, sizeof(TCP_Priority_List) + size);
439439

440440
if (!new_list) {
441441
return 0;

toxcore/group.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3339,7 +3339,7 @@ static State_Load_Status load_conferences(Group_Chats *g_c, const uint8_t *data,
33393339
data += sizeof(uint32_t);
33403340

33413341
if (g->numfrozen > 0) {
3342-
g->frozen = (Group_Peer *)malloc(sizeof(Group_Peer) * g->numfrozen);
3342+
g->frozen = (Group_Peer *)calloc(g->numfrozen, sizeof(Group_Peer));
33433343

33443344
if (g->frozen == nullptr) {
33453345
return STATE_LOAD_STATUS_ERROR;

toxcore/mono_time.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ static uint64_t current_time_monotonic_default(Mono_Time *mono_time, void *user_
100100

101101
Mono_Time *mono_time_new(void)
102102
{
103-
Mono_Time *mono_time = (Mono_Time *)malloc(sizeof(Mono_Time));
103+
Mono_Time *mono_time = (Mono_Time *)calloc(1, sizeof(Mono_Time));
104104

105105
if (mono_time == nullptr) {
106106
return nullptr;
107107
}
108108

109-
mono_time->time_update_lock = (pthread_rwlock_t *)malloc(sizeof(pthread_rwlock_t));
109+
mono_time->time_update_lock = (pthread_rwlock_t *)calloc(1, sizeof(pthread_rwlock_t));
110110

111111
if (mono_time->time_update_lock == nullptr) {
112112
free(mono_time);

toxcore/net_crypto.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,7 @@ static int add_data_to_buffer(const Logger *log, Packets_Array *array, uint32_t
749749
return -1;
750750
}
751751

752-
Packet_Data *new_d = (Packet_Data *)malloc(sizeof(Packet_Data));
752+
Packet_Data *new_d = (Packet_Data *)calloc(1, sizeof(Packet_Data));
753753

754754
if (new_d == nullptr) {
755755
return -1;
@@ -802,7 +802,7 @@ static int64_t add_data_end_of_buffer(const Logger *log, Packets_Array *array, c
802802
return -1;
803803
}
804804

805-
Packet_Data *new_d = (Packet_Data *)malloc(sizeof(Packet_Data));
805+
Packet_Data *new_d = (Packet_Data *)calloc(1, sizeof(Packet_Data));
806806

807807
if (new_d == nullptr) {
808808
return -1;
@@ -1786,7 +1786,7 @@ static int create_crypto_connection(Net_Crypto *c)
17861786
c->crypto_connections[id].last_packets_left_rem = 0;
17871787
c->crypto_connections[id].packet_send_rate_requested = 0;
17881788
c->crypto_connections[id].last_packets_left_requested_rem = 0;
1789-
c->crypto_connections[id].mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t));
1789+
c->crypto_connections[id].mutex = (pthread_mutex_t *)calloc(1, sizeof(pthread_mutex_t));
17901790

17911791
if (c->crypto_connections[id].mutex == nullptr) {
17921792
pthread_mutex_unlock(&c->connections_mutex);

toxcore/network.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type)
13701370
return -1;
13711371
}
13721372

1373-
// Used to avoid malloc parameter overflow
1373+
// Used to avoid calloc parameter overflow
13741374
const size_t max_count = min_u64(SIZE_MAX, INT32_MAX) / sizeof(IP_Port);
13751375
int type = make_socktype(tox_type);
13761376
struct addrinfo *cur;
@@ -1395,7 +1395,7 @@ int32_t net_getipport(const char *node, IP_Port **res, int tox_type)
13951395
return 0;
13961396
}
13971397

1398-
*res = (IP_Port *)malloc(sizeof(IP_Port) * count);
1398+
*res = (IP_Port *)calloc(count, sizeof(IP_Port));
13991399

14001400
if (*res == nullptr) {
14011401
freeaddrinfo(infos);

toxcore/ping_array.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "util.h"
1717

1818
typedef struct Ping_Array_Entry {
19-
void *data;
19+
uint8_t *data;
2020
uint32_t length;
2121
uint64_t time;
2222
uint64_t ping_id;
@@ -112,7 +112,7 @@ uint64_t ping_array_add(Ping_Array *array, const Mono_Time *mono_time, const uin
112112
clear_entry(array, index);
113113
}
114114

115-
array->entries[index].data = malloc(length);
115+
array->entries[index].data = (uint8_t *)malloc(length);
116116

117117
if (array->entries[index].data == nullptr) {
118118
return 0;

toxcore/tox.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ Tox *tox_new(const struct Tox_Options *options, Tox_Err_New *error)
516516
}
517517

518518
if (tox_options_get_experimental_thread_safety(opts)) {
519-
tox->mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
519+
tox->mutex = (pthread_mutex_t *)calloc(1, sizeof(pthread_mutex_t));
520520

521521
if (tox->mutex == nullptr) {
522522
SET_ERROR_PARAMETER(error, TOX_ERR_NEW_MALLOC);

0 commit comments

Comments
 (0)