From 4a0a8dc1138cb9cc48d187c70709469433f325a0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Rapha=C3=ABl=20Gertz?= Date: Tue, 5 Jul 2016 20:44:49 +0200 Subject: [PATCH] Add missing check on malloc allocation --- analyse.c | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/analyse.c b/analyse.c index 08b8e03..2c947ed 100644 --- a/analyse.c +++ b/analyse.c @@ -198,14 +198,20 @@ struct leaf *lookupLeaf(void **base, const char *key, unsigned index) { if (*base == NULL) { /* Branch level */ if (index < 8) { - *base = malloc(sizeof(struct branch)); + if ((*base = malloc(sizeof(struct branch))) == NULL) { + perror("Malloc failed"); + exit(EXIT_FAILURE); + } ((struct branch *)*base)->key = key[index] - ZERO_OFFSET; ((struct branch *)*base)->next = NULL; ((struct branch *)*base)->child = NULL; ret = lookupLeaf(&(((struct branch *)*base)->child), key, index + 1); /* Leaf level */ } else { - *base = malloc(sizeof(struct leaf)); + if ((*base = malloc(sizeof(struct leaf))) == NULL) { + perror("Malloc failed"); + exit(EXIT_FAILURE); + } ((struct leaf *)*base)->key = key[index] - ZERO_OFFSET; ((struct leaf *)*base)->next = NULL; ((struct leaf *)*base)->count = 0; @@ -224,7 +230,10 @@ struct leaf *lookupLeaf(void **base, const char *key, unsigned index) { /* Backup current in prev */ prev = current; /* Create new branch with key */ - current = malloc(sizeof(struct branch)); + if ((current = malloc(sizeof(struct branch))) == NULL) { + perror("Malloc failed"); + exit(EXIT_FAILURE); + } ((struct branch *)current)->key = key[index] - ZERO_OFFSET; ((struct branch *)current)->next = NULL; ((struct branch *)current)->child = NULL; @@ -246,7 +255,10 @@ struct leaf *lookupLeaf(void **base, const char *key, unsigned index) { /* Backup current in prev */ prev = current; /* Create new leaf with key */ - current = malloc(sizeof(struct leaf)); + if ((current = malloc(sizeof(struct leaf))) == NULL) { + perror("Malloc failed"); + exit(EXIT_FAILURE); + } ((struct leaf *)current)->key = key[index] - ZERO_OFFSET; ((struct leaf *)current)->next = NULL; ((struct leaf *)current)->count = 0; -- 2.41.0