Fix memory leaks found by valgrind.
authorGuus Sliepen <guus@tinc-vpn.org>
Tue, 9 Oct 2012 14:27:28 +0000 (16:27 +0200)
committerGuus Sliepen <guus@tinc-vpn.org>
Tue, 9 Oct 2012 14:27:28 +0000 (16:27 +0200)
src/net_setup.c
src/net_socket.c
src/openssl/cipher.c
src/openssl/digest.c
src/protocol_key.c
src/sptps.c

index 5d4e915..00273cf 100644 (file)
@@ -680,6 +680,8 @@ static bool setup_myself(void) {
                return false;
        }
 
+       free(cipher);
+
        regenerate_key();
 
        /* Check if we want to use message authentication codes... */
@@ -700,6 +702,8 @@ static bool setup_myself(void) {
                return false;
        }
 
+       free(digest);
+
        /* Compression */
 
        if(get_config_int(lookup_config(config_tree, "Compression"), &myself->incompression)) {
index df6f3c0..36da2d0 100644 (file)
@@ -571,6 +571,9 @@ static void free_outgoing(outgoing_t *outgoing) {
        if(outgoing->ai)
                freeaddrinfo(outgoing->ai);
 
+       if(outgoing->config_tree)
+               exit_configuration(&outgoing->config_tree);
+
        if(outgoing->name)
                free(outgoing->name);
 
index 1ca15ab..abd652c 100644 (file)
@@ -65,10 +65,8 @@ bool cipher_open_blowfish_ofb(cipher_t *cipher) {
 
 void cipher_close(cipher_t *cipher) {
        EVP_CIPHER_CTX_cleanup(&cipher->ctx);
-       if(cipher->counter) {
-               free(cipher->counter);
-               cipher->counter = 0;
-       }
+       free(cipher->counter);
+       cipher->counter = NULL;
 }
 
 size_t cipher_keylength(const cipher_t *cipher) {
index 3159e7a..e88274e 100644 (file)
@@ -78,8 +78,7 @@ bool digest_set_key(digest_t *digest, const void *key, size_t len) {
 }
 
 void digest_close(digest_t *digest) {
-       if(digest->key)
-               free(digest->key);
+       free(digest->key);
        digest->key = NULL;
 }
 
index fb53fe1..c042c4b 100644 (file)
@@ -258,6 +258,9 @@ bool send_ans_key(node_t *to) {
        size_t keylen = cipher_keylength(&myself->incipher);
        char key[keylen * 2 + 1];
 
+       cipher_close(&to->incipher);
+       digest_close(&to->indigest);
+
        cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
        digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
        to->incompression = myself->incompression;
@@ -345,6 +348,8 @@ bool ans_key_h(connection_t *c, const char *request) {
        }
 
        /* Don't use key material until every check has passed. */
+       cipher_close(&from->outcipher);
+       digest_close(&from->outdigest);
        from->status.validkey = false;
 
        if(compression < 0 || compression > 11) {
index 10d6e04..ed1d952 100644 (file)
@@ -627,18 +627,17 @@ bool sptps_start(sptps_t *s, void *handle, bool initiator, bool datagram, ecdsa_
 // Stop a SPTPS session.
 bool sptps_stop(sptps_t *s) {
        // Clean up any resources.
+       cipher_close(&s->incipher);
+       cipher_close(&s->outcipher);
+       digest_close(&s->indigest);
+       digest_close(&s->outdigest);
        ecdh_free(&s->ecdh);
        free(s->inbuf);
-       s->inbuf = NULL;
        free(s->mykex);
-       s->mykex = NULL;
        free(s->hiskex);
-       s->hiskex = NULL;
        free(s->key);
-       s->key = NULL;
        free(s->label);
-       s->label = NULL;
        free(s->late);
-       s->late = NULL;
+       memset(s, 0, sizeof *s);
        return true;
 }