]> Raphaƫl G. Git Repositories - carabistouilles/blobdiff - analyse.c
Add missing check on malloc allocation
[carabistouilles] / analyse.c
index 1adcdd980569f72940c60c473a46c7373c148571..2c947ede4b5410a5bc36fc734d4d21d19d232d97 100644 (file)
--- a/analyse.c
+++ b/analyse.c
@@ -50,9 +50,12 @@ struct leaf {
        unsigned count;
 };
 
-/* Lookup leaf prototype */
+/* Lookup leaf function prototype */
 struct leaf *lookupLeaf(void **, const char *, unsigned);
 
+/* Freeing node function prototype */
+void freeTree(void *, unsigned);
+
 /* Main function */
 int main(int argc, char **argv) {
 
@@ -140,6 +143,9 @@ int main(int argc, char **argv) {
        /* Read until end of file */
        } while (ret != 0);
 
+       /* Free tree */
+       freeTree(tree, 0);
+
        /* Close file */
        if (close(fd) == -1) {
                perror("Close failed");
@@ -153,6 +159,36 @@ int main(int argc, char **argv) {
        exit(EXIT_SUCCESS);
 }
 
+/* Recursive freeing of node function */
+void freeTree(void *base, unsigned index) {
+       void *current = base, *next;
+       if (base != NULL) {
+               /* Branch level */
+               if (index < 8) {
+                       /* Free horizontaly */
+                       do {
+                               /* Free children */
+                               if (((struct branch *)current)->child != NULL) {
+                                       freeTree(((struct branch *)current)->child, index + 1);
+                               }
+                               /* Free current and switch to next */
+                               next = ((struct branch *)current)->next;
+                               free(current);
+                               current = next;
+                       } while (current != NULL);
+               /* Leaf level */
+               } else {
+                       /* Free horizontaly */
+                       do {
+                               /* Free current and switch to next */
+                               next = ((struct leaf *)current)->next;
+                               free(current);
+                               current = next;
+                       } while (current != NULL);
+               }
+       }
+}
+
 /* Recursive lookup and populate tree function */
 struct leaf *lookupLeaf(void **base, const char *key, unsigned index) {
        /* Return leaf */
@@ -162,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;
@@ -188,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;
@@ -210,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;