From: Raphaƫl Gertz Date: Tue, 5 Jul 2016 18:44:49 +0000 (+0200) Subject: Add missing check on malloc allocation X-Git-Tag: v0.1 X-Git-Url: https://git.rapsys.eu/carabistouilles/commitdiff_plain/refs/heads/master?ds=sidebyside Add missing check on malloc allocation --- 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;