]> the.earth.li Git - onak.git/commitdiff
cscvs to tla changeset 39
authorJonathan McDowell <noodles@earth.li>
Mon, 31 May 2004 23:47:15 +0000 (23:47 +0000)
committerJonathan McDowell <noodles@earth.li>
Mon, 31 May 2004 23:47:15 +0000 (23:47 +0000)
Author: noodles
Date: 2002/11/16 13:53:23
Fixed various memory leaks found by using ccmalloc.

15 files changed:
add.c
gpgwww.c
hash.c
keydb_db2.c
keydb_db3.c
keydb_file.c
keydb_pg.c
lookup.c
maxpath.c
mem.c
mem.h
onak-conf.c
onak-conf.h
onak.c
stats.c

diff --git a/add.c b/add.c
index f65203c3a06b906486686e8c131f8a2f093aeade..b0a7fd227c90f1853612aa08cb1b0d4a3c8d0fb5 100644 (file)
--- a/add.c
+++ b/add.c
@@ -68,6 +68,7 @@ int main(int argc, char *argv[])
                                keys = NULL;
                        }
                        cleanupdb();
+                       cleanupconfig();
                } else {
                        puts("No OpenPGP packets found in input.");
                }
index bb8a446cd4be840df0be7c8119ae56776478f3cc..c225b827c90e095176e4d473b0a1fec9d82d0cd7 100644 (file)
--- a/gpgwww.c
+++ b/gpgwww.c
@@ -60,10 +60,16 @@ int main(int argc, char *argv[])
        dofindpath(from, to, true);
        destroyhash();
        cleanupdb();
+       cleanupconfig();
 
        puts("<HR>");
-       puts("Produced by gpgwww " VERSION ", part of onak. <A HREF=\"mailto:noodles-onak@earth.li\">Jonathan McDowell</A>");
+       puts("Produced by gpgwww " VERSION ", part of onak. "
+               "<A HREF=\"mailto:noodles-onak@earth.li\">"
+               "Jonathan McDowell</A>");
        end_html();
 
+       cleanupcgi(cgiparams);
+       cgiparams = NULL;
+
        return EXIT_SUCCESS;
 }
diff --git a/hash.c b/hash.c
index f07017a669d7e65342ce81637d801d0548bb9f59..e131d032b576281d1ad98e478b04185e57654cf2 100644 (file)
--- a/hash.c
+++ b/hash.c
@@ -13,6 +13,7 @@
 #include "keydb.h"
 #include "keyid.h"
 #include "ll.h"
+#include "mem.h"
 #include "stats.h"
 
 /**
@@ -55,7 +56,7 @@ void destroyhash(void)
                 * TODO: The problem is the object has pointers that
                 * need freed too.
                 */
-               llfree(curll, free);
+               llfree(curll, free_statskey);
                hashtable[i] = NULL;
        }
        elements = 0;
@@ -138,6 +139,10 @@ struct ll *hash_getkeysigs(uint64_t keyid)
 {
        struct stats_key *key = NULL;
 
+       if (keyid == 0)  {
+               return NULL;
+       }
+
        key = findinhash(keyid);
        if (key == NULL) {
                key = malloc(sizeof(*key));
index 1303cd80824312d48f1f628a93056040199331b6..7490ff30b27ccad71edf7e9e9c002be7ffce528f 100644 (file)
@@ -183,6 +183,8 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
                fetchbuf.size = data.size;
                read_openpgp_stream(buffer_fetchchar, &fetchbuf, &packets);
                parse_keys(packets, publickey);
+               free_packet_list(packets);
+               packets = NULL;
        }
 
        return (!ret);
index fc203a08d6404515b90caf05439e578b8049c1e2..6d98d3881f0f253eaa63137ee31bb0bcf715b939 100644 (file)
@@ -217,6 +217,8 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
                read_openpgp_stream(buffer_fetchchar, &fetchbuf,
                                &packets);
                parse_keys(packets, publickey);
+               free_packet_list(packets);
+               packets = NULL;
                numkeys++;
        } else if (ret != DB_NOTFOUND) {
                dbconn->err(dbconn, ret, "Problem retrieving key");
@@ -286,6 +288,7 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
                                        llfind(keylist, data.data,
                                                worddb_cmp) != NULL) {
                                newkeylist = lladd(newkeylist, data.data);
+                               data.data = NULL;
                        } else {
                                free(data.data);
                                data.data = NULL;
@@ -298,7 +301,13 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
                llfree(keylist, free);
                keylist = newkeylist;
                newkeylist = NULL;
+               if (data.data != NULL) {
+                       free(data.data);
+                       data.data = NULL;
+               }
        }
+       llfree(wordlist, NULL);
+       wordlist = NULL;
        
        for (newkeylist = keylist; newkeylist != NULL;
                        newkeylist = newkeylist->next) {
@@ -402,6 +411,14 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                dbconn->err(dbconn, ret, "Problem storing key");
        }
 
+       free(storebuf.buffer);
+       storebuf.buffer = NULL;
+       storebuf.size = 0;
+       storebuf.offset = 0; 
+
+       free_packet_list(packets);
+       packets = NULL;
+
        /*
         * Walk through our uids storing the words into the db with the keyid.
         */
index 4c1a6a384b199018b37b80c9ef12b0f019b31a70..a132ea73c833d679aec4083da3d71ffdbfe1140e 100644 (file)
@@ -88,6 +88,8 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
        if (fd > -1) {
                read_openpgp_stream(file_fetchchar, &fd, &packets);
                parse_keys(packets, publickey);
+               free_packet_list(packets);
+               packets = NULL;
                close(fd);
        }
 
@@ -125,6 +127,8 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                
                write_openpgp_stream(file_putchar, &fd, packets);
                close(fd);
+               free_packet_list(packets);
+               packets = NULL;
        }
 
        return (fd > -1);
index 35e5fae723cbd7d0cd976f38114678336212ee2b..f1cd0092feb3669438a9b6726dedd2ac744ee2e2 100644 (file)
@@ -132,7 +132,8 @@ void endtrans(void)
  *     in and then parse_keys() to parse the packets into a publickey
  *     structure.
  */
-int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, bool intrans)
+int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey,
+               bool intrans)
 {
        struct openpgp_packet_list *packets = NULL;
        PGresult *result = NULL;
@@ -174,6 +175,8 @@ int fetch_key(uint64_t keyid, struct openpgp_publickey **publickey, bool intrans
                                                &packets);
                                parse_keys(packets, publickey);
                                lo_close(dbconn, fd);
+                               free_packet_list(packets);
+                               packets = NULL;
                        }
                }
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
@@ -238,6 +241,8 @@ int fetch_key_text(const char *search, struct openpgp_publickey **publickey)
                                                &packets);
                                parse_keys(packets, publickey);
                                lo_close(dbconn, fd);
+                               free_packet_list(packets);
+                               packets = NULL;
                        }
                }
        } else if (PQresultStatus(result) != PGRES_TUPLES_OK) {
@@ -308,6 +313,8 @@ int store_key(struct openpgp_publickey *publickey, bool intrans, bool update)
                write_openpgp_stream(keydb_putchar, &fd, packets);
                lo_close(dbconn, fd);
        }
+       free_packet_list(packets);
+       packets = NULL;
 
        snprintf(statement, 1023, 
                        "INSERT INTO onak_keys (keyid, keydata) VALUES "
index 95ab6af01f2f54454474e87c4143bcdfb29f4e6f..00bc4165c7a1e59b88af27840aa8d370e85bb503 100644 (file)
--- a/lookup.c
+++ b/lookup.c
@@ -148,6 +148,7 @@ int main(int argc, char *argv[])
                        puts("Unknown operation!");
                }
                cleanupdb();
+               cleanupconfig();
        }
        puts("<hr>");
        puts("Produced by onak " VERSION " by Jonathan McDowell");
index ffa230fd2c1e557ed83fa8d269b48376a28a472e..509616e3ab340e727c0d6c554a0c6be8fd91c989 100644 (file)
--- a/maxpath.c
+++ b/maxpath.c
@@ -64,6 +64,7 @@ int main(int argc, char *argv[])
        findmaxpath(30);
        destroyhash();
        cleanupdb();
+       cleanupconfig();
        
        return EXIT_SUCCESS;
 }
diff --git a/mem.c b/mem.c
index 1b9627cc145da0a1253295202e3580c5cb1a936f..0795bf3b66f93ab42d0632f6e0c22ffa1e25a43b 100644 (file)
--- a/mem.c
+++ b/mem.c
@@ -13,6 +13,7 @@
 #include "keystructs.h"
 #include "ll.h"
 #include "mem.h"
+#include "stats.h"
 
 /**
  *     packet_dup - duplicate an OpenPGP packet.
@@ -171,3 +172,22 @@ void free_publickey(struct openpgp_publickey *key) {
                key = nextkey;
        }
 }
+
+/**
+ *     free_statskey - free an stats key structure.
+ *     @key: The key to free.
+ *
+ *     Takes a stats key and frees the memory used by it and the linked list
+ *     of sigs under it. Doesn't recurse into the list as it's assumed all the
+ *     objects referenced also exist in the hash.
+ */
+void free_statskey(struct stats_key *key)
+{
+       if (key != NULL) {
+               if (key->sigs != NULL) {
+                       llfree(key->sigs, NULL);
+                       key->sigs = NULL;
+               }
+               free(key);
+       }
+}
diff --git a/mem.h b/mem.h
index 3358cd18c7ea77c5e366722452b7396d63c5ce19..103c417e8e333e37926ff3afb1d6077e8104ac9e 100644 (file)
--- a/mem.h
+++ b/mem.h
@@ -10,6 +10,7 @@
 #define __MEM_H_
 
 #include "keystructs.h"
+#include "stats.h"
 
 /**
  *     packet_dup - duplicate an OpenPGP packet.
@@ -73,4 +74,14 @@ void free_signedpacket_list(
  */
 void free_publickey(struct openpgp_publickey *key);
 
+/**
+ *     free_statskey - free an stats key structure.
+ *     @key: The key to free.
+ *
+ *     Takes a stats key and frees the memory used by it and the linked list
+ *     of sigs under it. Doesn't recurse into the list as it's assumed all the
+ *     objects referenced also exist in the hash.
+ */
+void free_statskey(struct stats_key *key);
+
 #endif /* __MEM_H_ */
index 126bd565d4409515bf2f084333e8f27244791283..93a9320b4a3e165db6dbdd3e8ca1691b255d01db 100644 (file)
@@ -30,13 +30,13 @@ struct onak_config config = {
        /*
         * Options for directory backends.
         */
-       "/home/noodles/onak-db",        /* db_dir */
+       NULL,                   /* db_dir */
 
        /*
         * Options for the Postgres backend.
         */
        NULL,                   /* pg_dbhost */
-       "noodles",              /* pg_dbname */
+       NULL,                   /* pg_dbname */
        NULL,                   /* pg_dbuser */
        NULL,                   /* pg_dbpass */
 };
@@ -124,3 +124,42 @@ void readconfig(void) {
                fprintf(stderr, "Couldn't open config file; using defaults.\n");
        }
 }
+
+void cleanupconfig(void) {
+       if (config.thissite != NULL) {
+               free(config.thissite);
+               config.thissite = NULL;
+       }
+       if (config.adminemail != NULL) {
+               free(config.adminemail);
+               config.adminemail = NULL;
+       }
+       if (config.mta != NULL) {
+               free(config.mta);
+               config.mta = NULL;
+       }
+       if (config.db_dir != NULL) {
+               free(config.db_dir);
+               config.db_dir = NULL;
+       }
+       if (config.pg_dbhost != NULL) {
+               free(config.pg_dbhost);
+               config.pg_dbhost = NULL;
+       }
+       if (config.pg_dbname != NULL) {
+               free(config.pg_dbname);
+               config.pg_dbname = NULL;
+       }
+       if (config.pg_dbuser != NULL) {
+               free(config.pg_dbuser);
+               config.pg_dbuser = NULL;
+       }
+       if (config.pg_dbpass != NULL) {
+               free(config.pg_dbpass);
+               config.pg_dbpass = NULL;
+       }
+       if (config.syncsites != NULL) {
+               llfree(config.syncsites, free);
+               config.syncsites = NULL;
+       }
+}
index d05697bdf35c3a45ea8fe6b6ff7f43e9fd949e3a..44cb99d644c7dd4bec19154e61d8921703056b71 100644 (file)
@@ -65,4 +65,9 @@ extern struct onak_config config;
  */
 void readconfig(void);
 
+/*
+ *     cleanupconfig - clean up the config when we're shutting down.
+ */
+void cleanupconfig(void);
+
 #endif /* __ONAK_CONF_H_ */
diff --git a/onak.c b/onak.c
index 7a50d7a728feca5b019291eef9050c870be1b838..5c07a5b45e29a9182ad62ed9fe54330443912195 100644 (file)
--- a/onak.c
+++ b/onak.c
@@ -194,5 +194,7 @@ int main(int argc, char *argv[])
                usage();
        }
 
+       cleanupconfig();
+
        return rc;
 }
diff --git a/stats.c b/stats.c
index db8566bc859335b773a0f2d33fb06d9bfd8ce3f4..3f897ca905e5a4f1d95b633f2bc2be6c9b584342 100644 (file)
--- a/stats.c
+++ b/stats.c
@@ -56,6 +56,7 @@ void initcolour(bool parent)
 unsigned long findpath(struct stats_key *have, struct stats_key *want)
 {
        struct ll *keys = NULL;
+       struct ll *oldkeys = NULL;
        struct ll *sigs = NULL;
        struct ll *nextkeys = NULL;
        long curdegree = 0;
@@ -63,6 +64,7 @@ unsigned long findpath(struct stats_key *have, struct stats_key *want)
        
        curdegree = 1;
        keys = lladd(NULL, want);
+       oldkeys = keys;
 
        while (keys != NULL && have->colour == 0) {
                sigs = hash_getkeysigs(((struct stats_key *)
@@ -86,10 +88,20 @@ unsigned long findpath(struct stats_key *have, struct stats_key *want)
                keys = keys->next;
                if (keys == NULL) {
                        keys = nextkeys;
+                       llfree(oldkeys, NULL);
+                       oldkeys = keys;
                        nextkeys = NULL;
                        curdegree++;
                }
        }
+       if (oldkeys != NULL) {
+               llfree(oldkeys, NULL);
+               oldkeys = NULL;
+       }
+       if (nextkeys != NULL) {
+               llfree(nextkeys, NULL);
+               nextkeys = NULL;
+       }
 
        return count;
 }
@@ -107,23 +119,24 @@ unsigned long findpath(struct stats_key *have, struct stats_key *want)
 void dofindpath(uint64_t have, uint64_t want, bool html)
 {
        struct stats_key *keyinfoa, *keyinfob, *curkey;
+       uint64_t fullhave, fullwant;
        int rec;
        char *uid;
 
-       have = getfullkeyid(have);
-       want = getfullkeyid(want);
+       fullhave = getfullkeyid(have);
+       fullwant = getfullkeyid(want);
 
        /*
         * Make sure the keys we have and want are in the cache.
         */
-       hash_getkeysigs(have);
-       hash_getkeysigs(want);
+       hash_getkeysigs(fullhave);
+       hash_getkeysigs(fullwant);
 
-       if ((keyinfoa = findinhash(have)) == NULL) {
+       if ((keyinfoa = findinhash(fullhave)) == NULL) {
                printf("Couldn't find key 0x%llX.\n", have);
                return;
        }
-       if ((keyinfob = findinhash(want)) == NULL) {
+       if ((keyinfob = findinhash(fullwant)) == NULL) {
                printf("Couldn't find key 0x%llX.\n", want);
                return;
        }
@@ -161,12 +174,13 @@ void dofindpath(uint64_t have, uint64_t want, bool html)
                                         " signs");
                        } else if (html && uid != NULL) {
                                printf("<a href=\"lookup?op=get&search=%llX\">"
-                                       "0x%08llX</a> (<a href=\"lookup?op=vindex"
+                                       "0x%08llX</a>"
+                                       " (<a href=\"lookup?op=vindex"
                                        "&search=0x%llX\">%s</a>)%s<BR>\n",
                                        curkey->keyid & 0xFFFFFFFF,
                                        curkey->keyid & 0xFFFFFFFF,
                                        curkey->keyid & 0xFFFFFFFF,
-                                       txt2html(keyid2uid(curkey->keyid)),
+                                       txt2html(uid),
                                        (curkey->keyid == want) ? "" :
                                         " signs");
                        } else {
@@ -177,6 +191,10 @@ void dofindpath(uint64_t have, uint64_t want, bool html)
                                        (curkey->keyid == want) ? "" :
                                         " signs");
                        }
+                       if (uid != NULL) {
+                               free(uid);
+                               uid = NULL;
+                       }
                        curkey = findinhash(curkey->parent);
                }
                if (html) {