summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorDimitri Staessens <dimitri@ouroboros.rocks>2019-10-16 20:15:56 +0200
committerSander Vrijders <sander@ouroboros.rocks>2019-10-28 19:48:49 +0100
commit80eb05876f302ef713556637a54bf82c00506139 (patch)
tree6e675f6a07c6df3e4ba2455611638a5d41ae77c2 /src/lib
parentc0ea8b9c98e81f513ae3eaa2f2bf7f47fe528582 (diff)
downloadouroboros-80eb05876f302ef713556637a54bf82c00506139.tar.gz
ouroboros-80eb05876f302ef713556637a54bf82c00506139.zip
lib: Clean up fail path in constructors
The node construction path is revised using gotos to avoid repetition. Signed-off-by: Dimitri Staessens <dimitri@ouroboros.rocks> Signed-off-by: Sander Vrijders <sander@ouroboros.rocks>
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/btree.c32
1 files changed, 17 insertions, 15 deletions
diff --git a/src/lib/btree.c b/src/lib/btree.c
index 0b5a80d0..c8c71bfd 100644
--- a/src/lib/btree.c
+++ b/src/lib/btree.c
@@ -83,22 +83,17 @@ static struct btnode * btnode_create(size_t k)
node = malloc(sizeof(*node));
if (node == NULL)
- return NULL;
+ goto fail_node;
assert(k > 0);
node->keyvals = malloc(sizeof(*node->keyvals) * k);
- if (node->keyvals == NULL) {
- free(node);
- return NULL;
- }
+ if (node->keyvals == NULL)
+ goto fail_keyvals;
node->children = malloc(sizeof(*node->children) * (k + 1));
- if (node->children == NULL) {
- free(node->keyvals);
- free(node);
- return NULL;
- }
+ if (node->children == NULL)
+ goto fail_children;
for (i = 0; i < k; ++i) {
node->children[i] = NULL;
@@ -111,6 +106,13 @@ static struct btnode * btnode_create(size_t k)
node->leaf = true;
return node;
+
+ fail_children:
+ free(node->keyvals);
+ fail_keyvals:
+ free(node);
+ fail_node:
+ return NULL;
}
static void btnode_destroy(struct btnode * node)
@@ -366,14 +368,14 @@ static int btnode_delete(struct btnode * node,
struct btree * btree_create(size_t k)
{
- struct btree * tree = malloc(sizeof(*tree));
- if (tree == NULL)
+ struct btree * tree;
+
+ if (k < 1 || k > BTREE_MAX_ORDER)
return NULL;
- if (k < 1 || k > BTREE_MAX_ORDER) {
- free(tree);
+ tree = malloc(sizeof(*tree));
+ if (tree == NULL)
return NULL;
- }
tree->k = k;
tree->root = NULL;