## 01_nstx-leaks_and_cleanups.dpatch 
## by Matthew William Solloway Bell <m.w.s.bell@durham.ac.uk>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Fix nstxd leaks like a sieve.

--- a/nstx_dns.c
+++ b/nstx_dns.c
@@ -450,7 +450,7 @@
       ptr += list->len;
    }
    *l = len;
-   dns_free (pkt);
+   dns_free(pkt);
    return buf;
 }
 
@@ -505,7 +505,7 @@
      {
 	if (remain < 12)
 	  {
-	     syslog(LOG_ERR, "dns_extractpkt: too less bytes in an\n");
+	     syslog(LOG_ERR, "dns_extractpkt: too few bytes in an\n");
 	     free(offsets);
 	     dns_free(pkt);
 	     return NULL;
@@ -530,6 +530,7 @@
 	  {
 	     syslog(LOG_ERR, "dns_extractpkt: record too long in an (%d->%d)\n",
 		    remain, rrp->len);
+	     free(offsets);
 	     dns_free(pkt);
 	     return NULL;
 	  }
@@ -538,6 +539,7 @@
 	ptr += rrp->len;
 	remain -= rrp->len;
      }
+   free(offsets);
    return pkt;
 }
 
--- a/nstx_queue.c
+++ b/nstx_queue.c
@@ -59,8 +59,6 @@
 
    if (!qhead)
      return NULL;
-   if (tmp)
-     free(tmp);
 
    if ((id < 0) || (qhead->id == id))
      {
--- a/nstxd.c
+++ b/nstxd.c
@@ -179,7 +179,8 @@
 
 void nstx_getpacket (void) {
    int len, link;
-   const char *name, *buf, *data;
+   const char *name, *inbuf, *data;
+   char *outbuf = NULL;
    struct nstxmsg *msg;
    struct nstxqueue *qitem;
    struct dnspkt *pkt;
@@ -198,9 +199,9 @@
 			name);
 		  queueitem(pkt->id, name, &msg->peer);
 		  if ((data = dns_fqdn2data(name)) &&
-		      (buf = nstx_decode((unsigned char*)data, &len)))
+		      (inbuf = nstx_decode((unsigned char*)data, &len)))
 		    {
-		       nstx_handlepacket(buf, len, &sendtun);
+		       nstx_handlepacket(inbuf, len, &sendtun);
 		    }
 	       }
 	     dns_free(pkt);
@@ -218,10 +219,12 @@
       dns_settype(pkt, DNS_RESPONSE);
       link = dns_addquery(pkt, qitem->name);
       len = dns_getfreespace(pkt, DNS_RESPONSE);
-      buf = dequeue_senditem(&len);
-      dns_addanswer(pkt, buf, len, link);
-      buf = (char*)dns_constructpacket(pkt, &len);
-      sendns(buf, len, &qitem->peer);
+      outbuf = dequeue_senditem(&len);
+      dns_addanswer(pkt, outbuf, len, link);
+      outbuf = (char*)dns_constructpacket(pkt, &len);
+      sendns(outbuf, len, &qitem->peer);
+      free(outbuf);
+      free(qitem);
    }
    timeoutqueue(do_timeout);
 }
