From 34b03028378025ad22c8d29f70e81109cee690c2 Mon Sep 17 00:00:00 2001 From: Jonathan McDowell Date: Mon, 31 May 2004 23:47:15 +0000 Subject: [PATCH] cscvs to tla changeset 39 Author: noodles Date: 2002/11/16 13:53:23 Fixed various memory leaks found by using ccmalloc. --- add.c | 1 + gpgwww.c | 8 +++++++- hash.c | 7 ++++++- keydb_db2.c | 2 ++ keydb_db3.c | 17 +++++++++++++++++ keydb_file.c | 4 ++++ keydb_pg.c | 9 ++++++++- lookup.c | 1 + maxpath.c | 1 + mem.c | 20 ++++++++++++++++++++ mem.h | 11 +++++++++++ onak-conf.c | 43 +++++++++++++++++++++++++++++++++++++++++-- onak-conf.h | 5 +++++ onak.c | 2 ++ stats.c | 34 ++++++++++++++++++++++++++-------- 15 files changed, 152 insertions(+), 13 deletions(-) diff --git a/add.c b/add.c index f65203c..b0a7fd2 100644 --- 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."); } diff --git a/gpgwww.c b/gpgwww.c index bb8a446..c225b82 100644 --- a/gpgwww.c +++ b/gpgwww.c @@ -60,10 +60,16 @@ int main(int argc, char *argv[]) dofindpath(from, to, true); destroyhash(); cleanupdb(); + cleanupconfig(); puts("
"); - puts("Produced by gpgwww " VERSION ", part of onak. Jonathan McDowell"); + puts("Produced by gpgwww " VERSION ", part of onak. " + "" + "Jonathan McDowell"); end_html(); + cleanupcgi(cgiparams); + cgiparams = NULL; + return EXIT_SUCCESS; } diff --git a/hash.c b/hash.c index f07017a..e131d03 100644 --- 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)); diff --git a/keydb_db2.c b/keydb_db2.c index 1303cd8..7490ff3 100644 --- a/keydb_db2.c +++ b/keydb_db2.c @@ -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); diff --git a/keydb_db3.c b/keydb_db3.c index fc203a0..6d98d38 100644 --- a/keydb_db3.c +++ b/keydb_db3.c @@ -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. */ diff --git a/keydb_file.c b/keydb_file.c index 4c1a6a3..a132ea7 100644 --- a/keydb_file.c +++ b/keydb_file.c @@ -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); diff --git a/keydb_pg.c b/keydb_pg.c index 35e5fae..f1cd009 100644 --- a/keydb_pg.c +++ b/keydb_pg.c @@ -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 " diff --git a/lookup.c b/lookup.c index 95ab6af..00bc416 100644 --- a/lookup.c +++ b/lookup.c @@ -148,6 +148,7 @@ int main(int argc, char *argv[]) puts("Unknown operation!"); } cleanupdb(); + cleanupconfig(); } puts("
"); puts("Produced by onak " VERSION " by Jonathan McDowell"); diff --git a/maxpath.c b/maxpath.c index ffa230f..509616e 100644 --- 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 1b9627c..0795bf3 100644 --- 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 3358cd1..103c417 100644 --- 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_ */ diff --git a/onak-conf.c b/onak-conf.c index 126bd56..93a9320 100644 --- a/onak-conf.c +++ b/onak-conf.c @@ -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; + } +} diff --git a/onak-conf.h b/onak-conf.h index d05697b..44cb99d 100644 --- a/onak-conf.h +++ b/onak-conf.h @@ -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 7a50d7a..5c07a5b 100644 --- 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 db8566b..3f897ca 100644 --- 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("" - "0x%08llX (" + " (%s)%s
\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) { -- 2.39.5