summaryrefslogtreecommitdiffstats
path: root/zix/hash.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2014-09-22 21:52:15 +0000
committerDavid Robillard <d@drobilla.net>2014-09-22 21:52:15 +0000
commit624b19b492df58075e64572bb0630693f447f2ce (patch)
treeba583a27d5abb161bd6ca294c93f74fc756a3613 /zix/hash.c
parentb5896fff67d150e6ba96cea7d3679f9958b787ea (diff)
downloadzix-624b19b492df58075e64572bb0630693f447f2ce.tar.gz
zix-624b19b492df58075e64572bb0630693f447f2ce.tar.bz2
zix-624b19b492df58075e64572bb0630693f447f2ce.zip
Gracefully handle memory allocation failure.
100% test coverage for hash.c. git-svn-id: http://svn.drobilla.net/zix/trunk@85 df6676b4-ccc9-40e5-b5d6-7c4628a128e3
Diffstat (limited to 'zix/hash.c')
-rw-r--r--zix/hash.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/zix/hash.c b/zix/hash.c
index b24ee78..5d2b18c 100644
--- a/zix/hash.c
+++ b/zix/hash.c
@@ -1,5 +1,5 @@
/*
- Copyright 2011 David Robillard <http://drobilla.net>
+ Copyright 2011-2014 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -58,13 +58,18 @@ zix_hash_new(ZixHashFunc hash_func,
size_t value_size)
{
ZixHash* hash = (ZixHash*)malloc(sizeof(ZixHash));
- hash->hash_func = hash_func;
- hash->equal_func = equal_func;
- hash->n_buckets = &sizes[0];
- hash->value_size = value_size;
- hash->count = 0;
- hash->buckets = (ZixHashEntry**)calloc(*hash->n_buckets,
- sizeof(ZixHashEntry*));
+ if (hash) {
+ hash->hash_func = hash_func;
+ hash->equal_func = equal_func;
+ hash->n_buckets = &sizes[0];
+ hash->value_size = value_size;
+ hash->count = 0;
+ if (!(hash->buckets = (ZixHashEntry**)calloc(*hash->n_buckets,
+ sizeof(ZixHashEntry*)))) {
+ free(hash);
+ return NULL;
+ }
+ }
return hash;
}