Make allocations based on var size, not struct size - it's safer

This commit is contained in:
2017-06-21 12:20:47 +03:00
parent a0915fc803
commit bee649a34e
4 changed files with 8 additions and 9 deletions

View File

@@ -64,7 +64,7 @@ int main(int count, char *strings[])
} }
printf("Connected with %s encryption\n", SSL_get_cipher(ssl)); printf("Connected with %s encryption\n", SSL_get_cipher(ssl));
show_certs(ssl); show_certs(ssl);
if (!(args = (struct job_args*)calloc(1, sizeof(struct job_args) * MAX_AGENT_JOBS))) { if (!(args = calloc(1, sizeof(*args) * MAX_AGENT_JOBS))) {
fprintf( stderr, fprintf( stderr,
"Failed to calloc() %d bytes for job_args! Exiting...\n", "Failed to calloc() %d bytes for job_args! Exiting...\n",
(int)sizeof(struct job_args) * MAX_AGENT_JOBS ); (int)sizeof(struct job_args) * MAX_AGENT_JOBS );
@@ -74,7 +74,7 @@ int main(int count, char *strings[])
SSL_CTX_free(ctx); SSL_CTX_free(ctx);
_exit(EXIT_FAILURE); _exit(EXIT_FAILURE);
} }
if (!(job_thread = (pthread_t*)calloc(1, sizeof(pthread_t) * MAX_AGENT_JOBS))) { if (!(job_thread = calloc(1, sizeof(*job_thread) * MAX_AGENT_JOBS))) {
fprintf( stderr, fprintf( stderr,
"Failed to calloc() %d bytes for job_threads! Exiting...\n", "Failed to calloc() %d bytes for job_threads! Exiting...\n",
(int)sizeof(pthread_t) * MAX_AGENT_JOBS ); (int)sizeof(pthread_t) * MAX_AGENT_JOBS );

View File

@@ -110,10 +110,9 @@ void *agent_pool(void *args)
struct pool_data *pool = args; struct pool_data *pool = args;
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_attr_t attr; pthread_attr_t attr;
pthread_t *agent_thread = pthread_t *agent_thread = malloc(pool->size * sizeof(*agent_thread));
(pthread_t *)malloc(pool->size * sizeof(pthread_t));
struct agent_args *agent_struct = struct agent_args *agent_struct =
malloc(pool->size * sizeof(struct agent_args)); malloc(pool->size * sizeof(*agent_struct));
int i; int i;
memset(agent_thread, 0, sizeof(pthread_t) * pool->size); memset(agent_thread, 0, sizeof(pthread_t) * pool->size);

View File

@@ -85,9 +85,9 @@ void *client_pool(void *args)
struct pool_data *pool = args; struct pool_data *pool = args;
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_attr_t attr; pthread_attr_t attr;
pthread_t *client_thread = (pthread_t *)malloc(pool->size * sizeof(pthread_t)); pthread_t *client_thread = malloc(pool->size * sizeof(*client_thread));
struct client_args *client_struct = struct client_args *client_struct =
(struct client_args *)malloc(pool->size * sizeof(struct client_args)); malloc(pool->size * sizeof(*client_struct));
int i; int i;
memset(client_thread, 0, sizeof(pthread_t) * pool->size); memset(client_thread, 0, sizeof(pthread_t) * pool->size);

View File

@@ -12,7 +12,7 @@ int start_msg_queue(void)
for (i = 0; i < total_queues; i++) { for (i = 0; i < total_queues; i++) {
if (slot[i] == NULL) { if (slot[i] == NULL) {
slot[i] = (struct msg_t *)calloc(100, sizeof(struct msg_t)); slot[i] = calloc(100, sizeof(**slot));
if (slot[i] == NULL) if (slot[i] == NULL)
return -1; return -1;
return i; return i;
@@ -28,7 +28,7 @@ void free_queue(int id)
int start_job_queue(int poolsize) int start_job_queue(int poolsize)
{ {
slot = (struct msg_t **)calloc(poolsize, sizeof(struct msg_t *)); slot = calloc(poolsize, sizeof(*slot));
if (slot == NULL) if (slot == NULL)
return -1; return -1;
total_queues = poolsize; total_queues = poolsize;