Skip to content

Commit

Permalink
xmalloc: Add and use xcalloc
Browse files Browse the repository at this point in the history
Add a wrapper for calloc which checks for integer overflows in the
calculation of the size to allocate.

Use xcalloc to allocate an array of objects instead of calculating the
size ourselves, which might cause an integer overflow.

Signed-off-by: Tobias Klauser <[email protected]>
  • Loading branch information
tklauser committed Jun 25, 2014
1 parent 6424dd9 commit 46b0ace
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion astraceroute.c
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ static int __process_time(struct ctx *ctx, int fd, int fd_cap, int ttl,
return -EIO;
}

tmp = xmalloc(sizeof(struct timeval) * good);
tmp = xcalloc(good, sizeof(struct timeval));
for (i = j = 0; i < array_size(probes); ++i) {
if (probes[i].tv_sec == 0 && probes[i].tv_usec == 0)
continue;
Expand Down
3 changes: 2 additions & 1 deletion cpusched.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ static int cleanup_cpusched_batch(void *ptr)
void init_cpusched(unsigned int cpus)
{
rwlock_init(&map_lock);
cpu_work_map = xzmalloc((cpu_len = cpus) * sizeof(*cpu_work_map));
cpu_len = cpus;
cpu_work_map = xcalloc(cpu_len, sizeof(*cpu_work_map));
init_hash(&mapper);
}

Expand Down
4 changes: 2 additions & 2 deletions curvetun_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ int server_main(char *home, char *dev, char *port, int udp, int ipv4, int log)

set_nonblocking(lfd);

events = xzmalloc(MAX_EPOLL_SIZE * sizeof(*events));
events = xcalloc(MAX_EPOLL_SIZE, sizeof(*events));
for (i = 0; i < MAX_EPOLL_SIZE; ++i)
events[i].data.fd = -1;

Expand All @@ -661,7 +661,7 @@ int server_main(char *home, char *dev, char *port, int udp, int ipv4, int log)
if (!ispow2(threads))
syslog_panic("Thread number not power of two!\n");

threadpool = xzmalloc(sizeof(*threadpool) * threads);
threadpool = xcalloc(threads, sizeof(*threadpool));
thread_spawn_or_panic(cpus, efd[1], refd[1], tunfd, ipv4, udp);

init_cpusched(threads);
Expand Down
2 changes: 1 addition & 1 deletion hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static void grow_hash_table(struct hash_table *table)
struct hash_table_entry *old_array = table->array, *new_array;

new_size = alloc_nr(old_size);
new_array = xzmalloc(sizeof(struct hash_table_entry) * new_size);
new_array = xcalloc(new_size, sizeof(struct hash_table_entry));

table->size = new_size;
table->array = new_array;
Expand Down
4 changes: 2 additions & 2 deletions ifpps.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ static inline int padding_from_num(int n)
}

#define STATS_ALLOC1(member) \
do { stats->member = xzmalloc(cpus * sizeof(*(stats->member))); } while (0)
do { stats->member = xcalloc(cpus, sizeof(*(stats->member))); } while (0)

static void stats_alloc(struct ifstat *stats, unsigned int cpus)
{
Expand Down Expand Up @@ -1425,7 +1425,7 @@ int main(int argc, char **argv)
stats_alloc(&stats_new, cpus);
stats_alloc(&stats_delta, cpus);

cpu_hits = xzmalloc(cpus * sizeof(*cpu_hits));
cpu_hits = xcalloc(cpus, sizeof(*cpu_hits));

if (promisc)
ifflags = device_enter_promiscuous_mode(ifname);
Expand Down
15 changes: 15 additions & 0 deletions xmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ void *xmalloc(size_t size)
return ptr;
}

void *xcalloc(size_t nmemb, size_t size)
{
void *ptr;

if (unlikely(nmemb == 0 || size == 0))
panic("xcalloc: zero size\n");

ptr = calloc(nmemb, size);
if (unlikely(ptr == NULL))
panic("xcalloc: out of memory (allocating %zu members of "
"%zu bytes)\n", nmemb, size);

return ptr;
}

void *xzmalloc(size_t size)
{
void *ptr = xmalloc(size);
Expand Down
1 change: 1 addition & 0 deletions xmalloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "die.h"

extern void *xmalloc(size_t size) __hidden;
extern void *xcalloc(size_t nmemb, size_t size) __hidden;
extern void *xzmalloc(size_t size) __hidden;
extern void *xmallocz(size_t size) __hidden;
extern void *xmalloc_aligned(size_t size, size_t alignment) __hidden;
Expand Down

0 comments on commit 46b0ace

Please sign in to comment.