Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Jun 2015 05:36:08 +0000 (UTC)
From:      Michael Tuexen <tuexen@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r284694 - stable/10/usr.bin/sockstat
Message-ID:  <201506220536.t5M5a8cF032997@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: tuexen
Date: Mon Jun 22 05:36:08 2015
New Revision: 284694
URL: https://svnweb.freebsd.org/changeset/base/284694

Log:
  MFC r284604:
  Don't leak sockets.
  
  Reported by:	Coverity
  CID:		1306785

Modified:
  stable/10/usr.bin/sockstat/sockstat.c
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/usr.bin/sockstat/sockstat.c
==============================================================================
--- stable/10/usr.bin/sockstat/sockstat.c	Mon Jun 22 05:34:13 2015	(r284693)
+++ stable/10/usr.bin/sockstat/sockstat.c	Mon Jun 22 05:36:08 2015	(r284694)
@@ -255,6 +255,26 @@ sockaddr(struct sockaddr_storage *sa, in
 }
 
 static void
+free_socket(struct sock *sock)
+{
+	struct addr *cur, *next;
+
+	cur = sock->laddr;
+	while (cur != NULL) {
+		next = cur->next;
+		free(cur);
+		cur = next;
+	}
+	cur = sock->faddr;
+	while (cur != NULL) {
+		next = cur->next;
+		free(cur);
+		cur = next;
+	}
+	free(sock);
+}
+
+static void
 gather_sctp(void)
 {
 	struct sock *sock;
@@ -366,14 +386,17 @@ gather_sctp(void)
 		while (offset < len) {
 			xstcb = (struct xsctp_tcb *)(void *)(buf + offset);
 			offset += sizeof(struct xsctp_tcb);
-			if (no_stcb &&
-			    opt_l &&
-			    (!opt_L || !local_all_loopback) &&
-			    ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
-			     (xstcb->last == 1))) {
-				hash = (int)((uintptr_t)sock->socket % HASHSIZE);
-				sock->next = sockhash[hash];
-				sockhash[hash] = sock;
+			if (no_stcb) {
+				if (opt_l &&
+				    (!opt_L || !local_all_loopback) &&
+				    ((xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) ||
+				     (xstcb->last == 1))) {
+					hash = (int)((uintptr_t)sock->socket % HASHSIZE);
+					sock->next = sockhash[hash];
+					sockhash[hash] = sock;
+				} else {
+					free_socket(sock);
+				}
 			}
 			if (xstcb->last == 1)
 				break;
@@ -476,11 +499,14 @@ gather_sctp(void)
 					prev_faddr->next = faddr;
 				prev_faddr = faddr;
 			}
-			if (opt_c &&
-			    (!opt_L || !(local_all_loopback || foreign_all_loopback))) {
-				hash = (int)((uintptr_t)sock->socket % HASHSIZE);
-				sock->next = sockhash[hash];
-				sockhash[hash] = sock;
+			if (opt_c) {
+				if (!opt_L || !(local_all_loopback || foreign_all_loopback)) {
+					hash = (int)((uintptr_t)sock->socket % HASHSIZE);
+					sock->next = sockhash[hash];
+					sockhash[hash] = sock;
+				} else {
+					free_socket(sock);
+				}
 			}
 		}
 		xinpcb = (struct xsctp_inpcb *)(void *)(buf + offset);



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201506220536.t5M5a8cF032997>