src/database.c
changeset 20 b94039112f28
parent 0 586472add385
child 33 56571d34d754
--- a/src/database.c
+++ b/src/database.c
@@ -66,7 +66,7 @@
     GDBM_FILE dbf;
     datum content;
     datum key;
-    static char msg[513];
+    static char msg[513] = { 0 };
 
     if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL)
         return "db_lookup error";
@@ -75,9 +75,10 @@
     key.dsize = strlen (entry) + 1;
 
     content = gdbm_fetch (dbf, key);
-    if (content.dptr != NULL)
+    if (content.dptr != NULL) {
         snprintf (msg, 512, "%s", content.dptr);
-    else
+        free (content.dptr);
+    } else
         snprintf (msg, 512, "I haven't heard anything about %s.", entry);
 
     gdbm_close (dbf);
@@ -87,9 +88,9 @@
 char *db_elements (char *file_name)
 {
     GDBM_FILE dbf;
-    datum key;
+    datum key, nextkey;
     int count;
-    static char msg[81];
+    static char msg[81] = { 0 };
 
     if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL)
         return "db_lookup error";
@@ -98,7 +99,9 @@
 
     count = 0;
     while (key.dptr != NULL) {
-        key = gdbm_nextkey (dbf, key);
+        nextkey = gdbm_nextkey (dbf, key);
+        free (key.dptr);
+        key = nextkey;
         count++;
     }
 
@@ -111,8 +114,8 @@
 {
     GDBM_FILE dbf;
     datum content;
-    datum key;
-    static char msg[81];
+    datum key, nextkey;
+    static char msg[81] = { 0 };
     int count;
 
     if ((dbf = gdbm_open (file_name, 512, GDBM_READER, 0, 0)) == NULL)
@@ -121,11 +124,14 @@
     key = gdbm_firstkey (dbf);
 
     count = 0;
-    while (key.dptr) {
+    while (key.dptr != NULL) {
         content = gdbm_fetch (dbf, key);
         printf ("%11s: %s\n", key.dptr, content.dptr);
-        free (content.dptr);
-        key = gdbm_nextkey (dbf, key);
+        if (content.dptr != NULL)
+            free (content.dptr);
+        nextkey = gdbm_nextkey (dbf, key);
+        free (key.dptr);
+        key = nextkey;
         count++;
     }
 
@@ -137,7 +143,7 @@
 char *db_vaccuum (char *file_name)
 {
     GDBM_FILE dbf;
-    static char msg[81];
+    static char msg[81] = { 0 };
 
     if ((dbf = gdbm_open (file_name, 512, GDBM_WRITER, 0, 0)) == NULL)
         return "db_vaccuum error";