Use calloc where appropriate, avoid memset when normal buffer initialization works (#1550)

Depends on https://github.com/coturn/coturn/pull/1547
This commit is contained in:
Michael Jones 2024-08-04 19:30:58 -05:00 committed by GitHub
parent c4da2a8ea4
commit 958f70d5c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 83 additions and 128 deletions

View File

@ -892,6 +892,38 @@ char *dirname(char *path) {
#endif
#if defined(WINDOWS)
/*!
* \brief convert wchar to char
*
* \param pszInBuf: input buffer of wchar_t
* \param nInSize: size of input wchar_t buffer
* \param pszOutBuf: output buffer of char
* \param pnOutSize: size of output char buffer
* \return
*/
static char *_WTA(__in wchar_t *pszInBuf, __in int nInSize, __out char **pszOutBuf, __out int *pnOutSize) {
if (!pszInBuf || !pszOutBuf || !*pszOutBuf || !pnOutSize || nInSize <= 0) {
return NULL;
}
*pnOutSize = WideCharToMultiByte((UINT)0, (DWORD)0, pszInBuf, nInSize, NULL, 0, NULL, NULL);
if (*pnOutSize == 0) {
return NULL;
}
// add 1 for explicit nul-terminator at end.
// if MultiByteToWideChar is provided a length for the input, it does not add space for a nul-terminator
// and we have to add space to the allocation ourselves.
(*pnOutSize)++;
*pszOutBuf = malloc(*pnOutSize * sizeof(char));
if (WideCharToMultiByte((UINT)0, (DWORD)0, pszInBuf, nInSize, *pszOutBuf, *pnOutSize, NULL, NULL) == 0) {
free(*pszOutBuf);
return NULL;
} else {
(*pszOutBuf)[*pnOutSize - 1] = '\0';
return *pszOutBuf;
}
}
int getdomainname(char *name, size_t len) {
DSROLE_PRIMARY_DOMAIN_INFO_BASIC *info;
DWORD dw;
@ -971,63 +1003,6 @@ int getdomainname(char *name, size_t len) {
return 0;
}
/*!
* \brief convert char to wchar
*
* \param pszInBuf: input buffer of wchar string
* \param nInSize: size of wchar string
* \param pszOutBuf: output buffer of char string
* \param pnOutSize: size of char string
* \return
*/
wchar_t *_ATW(__in char *pszInBuf, __in int nInSize, __out wchar_t **pszOutBuf, __out int *pnOutSize) {
if (!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0) {
return NULL;
}
// Get buffer size
*pnOutSize = MultiByteToWideChar((UINT)0, (DWORD)0, pszInBuf, nInSize, *pszOutBuf, 0);
if (*pnOutSize == 0) {
return NULL;
}
(*pnOutSize)++;
*pszOutBuf = malloc((*pnOutSize) * sizeof(wchar_t));
memset((void *)*pszOutBuf, 0, sizeof(wchar_t) * (*pnOutSize));
if (MultiByteToWideChar((UINT)0, (DWORD)0, pszInBuf, nInSize, *pszOutBuf, *pnOutSize) == 0) {
free(*pszOutBuf);
return NULL;
} else {
return *pszOutBuf;
}
}
/*!
* \brief convert wchar to char
*
* \param pszInBuf: input buffer of char string
* \param nInSize: size of char string
* \param pszOutBuf: output buffer of wchar string
* \param pnOutSize: size of wchar string
* \return
*/
char *_WTA(__in wchar_t *pszInBuf, __in int nInSize, __out char **pszOutBuf, __out int *pnOutSize) {
if (!pszInBuf || !pszOutBuf || !pnOutSize || nInSize <= 0) {
return NULL;
}
*pnOutSize = WideCharToMultiByte((UINT)0, (DWORD)0, pszInBuf, nInSize, *pszOutBuf, 0, NULL, NULL);
if (*pnOutSize == 0) {
return NULL;
}
(*pnOutSize)++;
*pszOutBuf = malloc(*pnOutSize * sizeof(char));
memset((void *)*pszOutBuf, 0, sizeof(char) * (*pnOutSize));
if (WideCharToMultiByte((UINT)0, (DWORD)0, pszInBuf, nInSize, *pszOutBuf, *pnOutSize, NULL, NULL) == 0) {
free(*pszOutBuf);
return NULL;
} else {
return *pszOutBuf;
}
}
#endif
//////////////////// Config file search //////////////////////
@ -1314,12 +1289,12 @@ unsigned long get_system_active_number_of_cpus(void) {
////////////////////// Base 64 ////////////////////////////
static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
static size_t mod_table[] = {0, 2, 1};
static const size_t mod_table[] = {0, 2, 1};
static const char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};
static const char *decoding_table = NULL;
char *base64_encode(const unsigned char *data, size_t input_length, size_t *output_length) {
@ -1330,8 +1305,7 @@ char *base64_encode(const unsigned char *data, size_t input_length, size_t *outp
return NULL;
}
size_t i, j;
for (i = 0, j = 0; i < input_length;) {
for (size_t i = 0, j = 0; i < input_length;) {
uint32_t octet_a = i < input_length ? data[i++] : 0;
uint32_t octet_b = i < input_length ? data[i++] : 0;
@ -1345,7 +1319,7 @@ char *base64_encode(const unsigned char *data, size_t input_length, size_t *outp
encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
}
for (i = 0; i < mod_table[input_length % 3]; i++) {
for (size_t i = 0; i < mod_table[input_length % 3]; i++) {
encoded_data[*output_length - 1 - i] = '=';
}
@ -1356,13 +1330,12 @@ char *base64_encode(const unsigned char *data, size_t input_length, size_t *outp
void build_base64_decoding_table(void) {
decoding_table = (char *)malloc(256);
memset(decoding_table, 0, 256);
char *table = (char *)calloc(256, sizeof(char));
int i;
for (i = 0; i < 64; i++) {
decoding_table[(unsigned char)encoding_table[i]] = (char)i;
for (char i = 0; i < 64; i++) {
table[(unsigned char)encoding_table[i]] = i;
}
decoding_table = table;
}
unsigned char *base64_decode(const char *data, size_t input_length, size_t *output_length) {

View File

@ -243,10 +243,6 @@ char *dirname(char *path);
#if defined(WINDOWS)
int getdomainname(char *name, size_t len);
// wchar convert to char
char *_WTA(__in wchar_t *pszInBufBuf, __in int nInSize, __out char **pszOutBuf, __out int *pnOutSize);
// char convert to wchar
wchar_t *_ATW(__in char *pszInBuf, __in int nInSize, __out wchar_t **pszOutBuf, __out int *pnOutSize);
#endif
////////////////// File search ////////////////////////

View File

@ -114,7 +114,7 @@ int turn_mutex_unlock(const turn_mutex *mutex) {
int turn_mutex_init(turn_mutex *mutex) {
if (mutex) {
mutex->data = MAGIC_CODE;
mutex->mutex = malloc(sizeof(pthread_mutex_t));
mutex->mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
pthread_mutex_init((pthread_mutex_t *)mutex->mutex, NULL);
return 0;
} else {
@ -132,8 +132,8 @@ int turn_mutex_init_recursive(turn_mutex *mutex) {
if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) < 0) {
perror("Cannot set type on mutex attr");
} else {
mutex->mutex = malloc(sizeof(pthread_mutex_t));
mutex->data = MAGIC_CODE;
mutex->mutex = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
if ((ret = pthread_mutex_init((pthread_mutex_t *)mutex->mutex, &attr)) < 0) {
perror("Cannot init mutex");
mutex->data = 0;

View File

@ -628,8 +628,8 @@ static void discoveryresult(const char *decision) {
int main(int argc, char **argv) {
int remote_port = DEFAULT_STUN_PORT;
char local_addr_string[256] = "\0";
char local2_addr_string[256] = "\0";
char local_addr_string[256] = {0};
char local2_addr_string[256] = {0};
int c = 0;
int mapping = 0;
int filtering = 0;
@ -651,8 +651,6 @@ int main(int argc, char **argv) {
set_no_stdout_log(1);
set_system_parameters(0);
memset(local_addr_string, 0, sizeof(local_addr_string));
memset(local2_addr_string, 0, sizeof(local2_addr_string));
addr_set_any(&remote_addr);
addr_set_any(&other_addr);
addr_set_any(&reflexive_addr);

View File

@ -129,7 +129,7 @@ static int encode_token(const char *server_name, const char *gcm_nonce, const ch
static int validate_decode_token(const char *server_name, const oauth_key key, const char *base64encoded_etoken,
oauth_token *dot) {
memset((dot), 0, sizeof(*dot));
memset(dot, 0, sizeof(*dot));
encoded_oauth_token etoken;
memset(&etoken, 0, sizeof(etoken));

View File

@ -32,6 +32,8 @@
#include "apputils.h"
#include "stun_buffer.h"
#include <limits.h> // for USHRT_MAX
/////////////// io handlers ///////////////////
static void udp_server_input_handler(evutil_socket_t fd, short what, void *arg) {
@ -117,17 +119,18 @@ static int udp_create_server_socket(server_type *const server, const char *const
}
static server_type *init_server(int verbose, const char *ifname, char **local_addresses, size_t las, int port) {
server_type *server = (server_type *)malloc(sizeof(server_type));
// Ports cannot be larger than unsigned 16 bits
// and since this function creates two ports next to each other
// the provided port must be smaller than max unsigned 16.
if ((uint16_t)port >= USHRT_MAX) {
return NULL;
}
server_type *server = (server_type *)calloc(1, sizeof(server_type));
if (!server) {
return server;
return NULL;
}
memset(server, 0, sizeof(server_type));
server->verbose = verbose;
server->event_base = turn_event_base_new();
while (las) {

View File

@ -92,8 +92,7 @@ static MONGO *get_mongodb_connection(void) {
mongoc_init();
mongoc_log_set_handler(&mongo_logger, NULL);
mydbconnection = (MONGO *)malloc(sizeof(MONGO));
memset(mydbconnection, 0, sizeof(MONGO));
mydbconnection = (MONGO *)calloc(1, sizeof(MONGO));
mydbconnection->uri = mongoc_uri_new(pud->userdb);

View File

@ -97,7 +97,7 @@ char *decryptPassword(char *in, const unsigned char *mykey) {
char *out;
unsigned char iv[8] = {0}; // changed
AES_KEY key;
unsigned char outdata[256]; // changed
unsigned char outdata[256] = {0}; // changed
AES_set_encrypt_key(mykey, 128, &key);
int newTotalSize = decodedTextSize(in);
int bytes_to_decode = strlen(in);
@ -105,7 +105,6 @@ char *decryptPassword(char *in, const unsigned char *mykey) {
char last[1024] = "";
struct ctr_state state;
init_ctr(&state, iv);
memset(outdata, '\0', sizeof(outdata));
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
CRYPTO_ctr128_encrypt(encryptedText, outdata, newTotalSize, &key, state.ivec, state.ecount, &state.num,
@ -121,8 +120,7 @@ char *decryptPassword(char *in, const unsigned char *mykey) {
}
static Myconninfo *MyconninfoParse(char *userdb, char **errmsg) {
Myconninfo *co = (Myconninfo *)malloc(sizeof(Myconninfo));
memset(co, 0, sizeof(Myconninfo));
Myconninfo *co = (Myconninfo *)calloc(1, sizeof(Myconninfo));
if (userdb) {
char *s0 = strdup(userdb);
char *s = s0;

View File

@ -73,8 +73,7 @@ static void RyconninfoFree(Ryconninfo *co) {
}
static Ryconninfo *RyconninfoParse(const char *userdb, char **errmsg) {
Ryconninfo *co = (Ryconninfo *)malloc(sizeof(Ryconninfo));
memset(co, 0, sizeof(Ryconninfo));
Ryconninfo *co = (Ryconninfo *)calloc(1, sizeof(Ryconninfo));
if (userdb) {
char *s0 = strdup(userdb);
char *s = s0;

View File

@ -480,15 +480,13 @@ static int create_new_connected_udp_socket(dtls_listener_relay_server_type *serv
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "Cannot bind udp server socket to device %s\n", (char *)(s->e->relay_ifname));
}
ioa_socket_handle ret = (ioa_socket *)malloc(sizeof(ioa_socket));
ioa_socket_handle ret = (ioa_socket *)calloc(1, sizeof(ioa_socket));
if (!ret) {
TURN_LOG_FUNC(TURN_LOG_LEVEL_ERROR, "%s: Cannot allocate new socket structure\n", __FUNCTION__);
socket_closesocket(udp_fd);
return -1;
}
memset(ret, 0, sizeof(ioa_socket));
ret->magic = SOCKET_MAGIC;
ret->fd = udp_fd;

View File

@ -236,11 +236,10 @@ redis_context_handle redisLibeventAttach(struct event_base *base, char *ip0, int
}
/* Create container for context and r/w events */
struct redisLibeventEvents *e = (struct redisLibeventEvents *)malloc(sizeof(struct redisLibeventEvents));
struct redisLibeventEvents *e = (struct redisLibeventEvents *)calloc(1, sizeof(struct redisLibeventEvents));
if (!e) {
return NULL;
}
memset(e, 0, sizeof(struct redisLibeventEvents));
e->allocated = 1;
e->context = ac;

View File

@ -331,8 +331,14 @@ struct str_buffer {
struct str_buffer *str_buffer_new(void) {
struct str_buffer *ret = (struct str_buffer *)calloc(sizeof(struct str_buffer), 1);
if (!ret) {
return NULL;
}
ret->buffer = (char *)malloc(1);
if (!(ret->buffer)) {
free(ret);
return NULL;
}
ret->buffer[0] = 0;
ret->capacity = 1;
return ret;

View File

@ -1699,9 +1699,9 @@ static const struct myoption admin_long_options[] = {
int init_ctr(struct ctr_state *state, const unsigned char iv[8]) {
state->num = 0;
memset(state->ecount, 0, 16);
memset(state->ivec + 8, 0, 8);
memset(state->ecount, 0, sizeof(state->ecount));
memcpy(state->ivec, iv, 8);
memset(state->ivec + 8, 0, sizeof(state->ivec) - 8);
return 1;
}
@ -1799,7 +1799,7 @@ unsigned char *base64decode(const void *b64_decode_this, int decode_this_many_by
int decoded_byte_index = 0; // Index where the next base64_decoded byte should be written.
while (0 < BIO_read(b64_bio, base64_decoded + decoded_byte_index, 1)) { // Read byte-by-byte.
decoded_byte_index++; // Increment the index until read of BIO decoded data is complete.
} // Once we're done reading decoded data, BIO_read returns -1 even though there's no error.
} // Once we're done reading decoded data, BIO_read returns -1 even though there's no error.
BIO_free_all(b64_bio); // Destroys all BIOs in chain, starting with b64 (i.e. the 1st one).
return base64_decoded; // Returns base-64 decoded data with trailing null terminator.
@ -1821,7 +1821,7 @@ int decodedTextSize(char *input) {
void decrypt_aes_128(char *in, const unsigned char *mykey) {
unsigned char iv[8] = {0};
AES_KEY key;
unsigned char outdata[256];
unsigned char outdata[256] = {0};
AES_set_encrypt_key(mykey, 128, &key);
int newTotalSize = decodedTextSize(in);
int bytes_to_decode = strlen(in);
@ -1829,7 +1829,6 @@ void decrypt_aes_128(char *in, const unsigned char *mykey) {
char last[1024] = "";
struct ctr_state state;
init_ctr(&state, iv);
memset(outdata, '\0', sizeof(outdata));
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
CRYPTO_ctr128_encrypt(encryptedText, outdata, newTotalSize, &key, state.ivec, state.ecount, &state.num,

View File

@ -3881,8 +3881,6 @@ struct _super_memory {
static void init_super_memory_region(super_memory_t *r) {
if (r) {
memset(r, 0, sizeof(super_memory_t));
r->super_memory = (char **)malloc(sizeof(char *));
r->super_memory[0] = (char *)calloc(1, TURN_SM_SIZE);
@ -3903,7 +3901,7 @@ static void init_super_memory_region(super_memory_t *r) {
void init_super_memory(void) { ; }
super_memory_t *new_super_memory_region(void) {
super_memory_t *r = (super_memory_t *)malloc(sizeof(super_memory_t));
super_memory_t *r = (super_memory_t *)calloc(1, sizeof(super_memory_t));
init_super_memory_region(r);
return r;
}

View File

@ -1422,8 +1422,7 @@ void setup_admin_thread(void) {
void admin_server_receive_message(struct bufferevent *bev, void *ptr) {
UNUSED_ARG(ptr);
struct turn_session_info *tsi = (struct turn_session_info *)malloc(sizeof(struct turn_session_info));
turn_session_info_init(tsi);
struct turn_session_info *tsi = (struct turn_session_info *)calloc(1, sizeof(struct turn_session_info));
int n = 0;
struct evbuffer *input = bufferevent_get_input(bev);
@ -1443,8 +1442,7 @@ void admin_server_receive_message(struct bufferevent *bev, void *ptr) {
if (tsi->valid) {
ur_map_put(adminserver.sessions, (ur_map_key_type)tsi->id, (ur_map_value_type)tsi);
tsi = (struct turn_session_info *)malloc(sizeof(struct turn_session_info));
turn_session_info_init(tsi);
tsi = (struct turn_session_info *)calloc(1, sizeof(struct turn_session_info));
} else {
turn_session_info_clean(tsi);
}

View File

@ -374,18 +374,12 @@ static inline ioa_socket_handle get_relay_socket_ss(ts_ur_super_session *ss, int
/////////// Session info ///////
void turn_session_info_init(struct turn_session_info *tsi) {
if (tsi) {
memset(tsi, 0, sizeof(struct turn_session_info));
}
}
void turn_session_info_clean(struct turn_session_info *tsi) {
if (tsi) {
if (tsi->extra_peers_data) {
free(tsi->extra_peers_data);
}
turn_session_info_init(tsi);
memset(tsi, 0, sizeof(struct turn_session_info));
}
}
@ -619,7 +613,7 @@ int turn_session_info_copy_from(struct turn_session_info *tsi, ts_ur_super_sessi
int report_turn_session_info(turn_turnserver *server, ts_ur_super_session *ss, int force_invalid) {
if (server && ss && server->send_turn_session_info) {
struct turn_session_info tsi;
turn_session_info_init(&tsi);
memset(&tsi, 0, sizeof(struct turn_session_info));
if (turn_session_info_copy_from(&tsi, ss) < 0) {
turn_session_info_clean(&tsi);
} else {
@ -3996,8 +3990,7 @@ static int handle_old_stun_command(turn_turnserver *server, ts_ur_super_session
{
size_t oldsz = strlen(get_version(server));
size_t newsz = (((oldsz) >> 2) + 1) << 2;
uint8_t software[120];
memset(software, 0, sizeof(software));
uint8_t software[120] = {0};
if (newsz > sizeof(software)) {
newsz = sizeof(software);
}
@ -4053,8 +4046,7 @@ static int handle_old_stun_command(turn_turnserver *server, ts_ur_super_session
{
size_t oldsz = strlen(get_version(server));
size_t newsz = (((oldsz) >> 2) + 1) << 2;
uint8_t software[120];
memset(software, 0, sizeof(software));
uint8_t software[120] = {0};
if (newsz > sizeof(software)) {
newsz = sizeof(software);
}

View File

@ -175,7 +175,6 @@ struct turn_session_info {
band_limit_t bps;
};
void turn_session_info_init(struct turn_session_info *tsi);
void turn_session_info_clean(struct turn_session_info *tsi);
void turn_session_info_add_peer(struct turn_session_info *tsi, ioa_addr *peer);