getdir errors: avoid unneeded memory leaks
authorMarkus Bröker <mbroeker@largo.dyndns.tv>
Mon, 21 Jun 2010 21:58:00 +0200
changeset 136 d82f65e902d0
parent 135 f837cf975e95
child 137 d8a0984b72fc
getdir errors: avoid unneeded memory leaks committer: Markus Bröker <mbroeker@largo.homelinux.org>
lsflib/src/getdir.c
lsflib/src/md5recursive.c
lsflib/src/md5sum.c
--- a/lsflib/src/getdir.c
+++ b/lsflib/src/getdir.c
@@ -48,29 +48,23 @@
     }
 
     while ((entry = readdir (directory))) {
-        list = (char *)malloc (strlen (root) + 1 + strlen (entry->d_name) + 1);
+        if ((!strcmp (entry->d_name, ".")) || (!strcmp (entry->d_name, "..")))
+            continue;
 
-        if (list == NULL) {
-            perror ("malloc");
-            return;
-        }
-
+        list = malloc (strlen (root) + 1 + strlen (entry->d_name) + 1);
         sprintf (list, "%s/%s", root, entry->d_name);
 
-        if (!strcmp (entry->d_name, "."))
-            continue;
-        if (!strcmp (entry->d_name, ".."))
-            continue;
         if (isDir (list)) {
             printf ("Directory: %s\n", list);
             if (recursive)
                 getdir (list, recursive);
+            free (list);
             continue;
         }
 
         printf ("%s\n", list);
+        free (list);
     }
 
-    free (list);
     closedir (directory);
 }
--- a/lsflib/src/md5recursive.c
+++ b/lsflib/src/md5recursive.c
@@ -30,26 +30,18 @@
     }
 
     while ((entry = readdir (directory))) {
-        list = (char *)malloc (strlen (root) + 1 + strlen (entry->d_name) + 1);
-
-        if (list == NULL) {
-            perror ("malloc");
-            return;
-        }
-
-        sprintf (list, "%s/%s", root, entry->d_name);
-
-        if (!strcmp (entry->d_name, "."))
+        if ((!strcmp (entry->d_name, ".")) || (!strcmp (entry->d_name, "..")))
             continue;
 
-        if (!strcmp (entry->d_name, ".."))
-            continue;
+        list = malloc (strlen (root) + 1 + strlen (entry->d_name) + 1);
+        sprintf (list, "%s/%s", root, entry->d_name);
 
         if (isDir (list)) {
             fprintf (stderr, "Directory: %s\n", list);
             if (recursive) {
                 md5recursive (list, recursive);
             }
+            free (list);
             continue;
         }
 
@@ -60,9 +52,9 @@
         for (i = 0; i < 16; i++)
             printf ("%02x", value[i]);
         printf ("  %s\n", list);
+        free (list);
         free (value);
     }
 
-    free (list);
     closedir (directory);
 }
--- a/lsflib/src/md5sum.c
+++ b/lsflib/src/md5sum.c
@@ -23,15 +23,11 @@
 
     if ((f = fopen (fname, "rb")) == NULL) {
         perror ("FOPEN");
-        value = malloc (1);
-        value = 0;
-        return value;
+        return NULL;
     }
 
     if ((md = EVP_get_digestbyname ("md5")) == NULL) {
-        value = malloc (1);
-        value = 0;
-        return value;
+        return NULL;
     }
 
     EVP_MD_CTX_init (&mdctx);