Add missing check on malloc allocation master v0.1
authorRaphaël Gertz <git@rapsys.eu>
Tue, 5 Jul 2016 18:44:49 +0000 (20:44 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Tue, 5 Jul 2016 18:44:49 +0000 (20:44 +0200)
analyse.c

index 08b8e03..2c947ed 100644 (file)
--- 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;