From d5888c10a181d6247e407f480524d36be6319b3e Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Thu, 21 Aug 2014 16:42:12 +0200 Subject: [PATCH] Fix memory and resource leaks. --- selftest.c | 2 ++ test.c | 4 +++- utcp.c | 15 ++++++++++++--- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/selftest.c b/selftest.c index c0ecd3a..2c29943 100644 --- a/selftest.c +++ b/selftest.c @@ -114,6 +114,8 @@ int main(int argc, char *argv[]) { fprintf(stderr, "closing...\n"); utcp_close(c); + utcp_exit(a); + utcp_exit(b); return 0; } diff --git a/test.c b/test.c index 38240db..d362845 100644 --- a/test.c +++ b/test.c @@ -82,6 +82,8 @@ int main(int argc, char *argv[]) { connected = true; } + freeaddrinfo(ai); + struct utcp *u = utcp_init(server ? do_accept : NULL, NULL, do_send, &s); if(!u) return 1; @@ -120,7 +122,7 @@ int main(int argc, char *argv[]) { if(fds[1].revents) { struct sockaddr_storage ss; - socklen_t sl; + socklen_t sl = sizeof ss; int len = recvfrom(s, buf, sizeof buf, MSG_DONTWAIT, (struct sockaddr *)&ss, &sl); if(len <= 0) break; diff --git a/utcp.c b/utcp.c index 730f12b..1fae32c 100644 --- a/utcp.c +++ b/utcp.c @@ -127,6 +127,7 @@ static void free_connection(struct utcp_connection *c) { memmove(cp + i, cp + i + 1, (utcp->nconnections - i - 1) * sizeof *cp); utcp->nconnections--; + free(c->sndbuf); free(c); } @@ -206,8 +207,9 @@ struct utcp_connection *utcp_connect(struct utcp *utcp, uint16_t dst, utcp_recv_ hdr.dst = c->dst; hdr.seq = c->snd.iss; hdr.ack = 0; - hdr.ctl = SYN; hdr.wnd = c->rcv.wnd; + hdr.ctl = SYN; + hdr.aux = 0; set_state(c, SYN_SENT); @@ -260,6 +262,7 @@ static void ack(struct utcp_connection *c, bool sendatleastone) { pkt.hdr.ack = c->rcv.nxt; pkt.hdr.wnd = c->snd.wnd; pkt.hdr.ctl = ACK; + pkt.hdr.aux = 0; do { uint32_t seglen = left > c->utcp->mtu ? c->utcp->mtu : left; @@ -757,6 +760,7 @@ int utcp_recv(struct utcp *utcp, const void *data, size_t len) { // FIN counts as one sequence number c->rcv.nxt++; + len++; // Inform the application that the peer closed the connection. if(c->recv) { @@ -1037,8 +1041,13 @@ struct utcp *utcp_init(utcp_accept_t accept, utcp_pre_accept_t pre_accept, utcp_ void utcp_exit(struct utcp *utcp) { if(!utcp) return; - for(int i = 0; i < utcp->nconnections; i++) - free_connection(utcp->connections[i]); + for(int i = 0; i < utcp->nconnections; i++) { + if(!utcp->connections[i]->reapable) + debug("Warning, freeing unclosed connection %p\n", utcp->connections[i]); + free(utcp->connections[i]->sndbuf); + free(utcp->connections[i]); + } + free(utcp->connections); free(utcp); } -- 2.39.5