Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 12 Feb 2012 07:55:34 +0000 (UTC)
From:      Mikolaj Golub <trociny@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-9@freebsd.org
Subject:   svn commit: r231553 - stable/9/usr.bin/sockstat
Message-ID:  <201202120755.q1C7tYb3071586@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: trociny
Date: Sun Feb 12 07:55:33 2012
New Revision: 231553
URL: http://svn.freebsd.org/changeset/base/231553

Log:
  MFC r230874:
  
  Try to avoid ambiguity when sysctl returns ENOMEM additionally
  checking the returned oldlen: when ENOMEM is due to the supplied
  buffer being too short the return oldlen is equal to buffer size.
  
  Without this additional check sockstat gets stuck in loop leaking the
  memory if the returned ENOMEM was due the exceeded memorylocked
  limit. This is easily can be observed running `limits -l 1k sockstat'.
  
  Submitted by:	Andrey Zonov <andrey zonov org>

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

Modified: stable/9/usr.bin/sockstat/sockstat.c
==============================================================================
--- stable/9/usr.bin/sockstat/sockstat.c	Sun Feb 12 07:53:37 2012	(r231552)
+++ stable/9/usr.bin/sockstat/sockstat.c	Sun Feb 12 07:55:33 2012	(r231553)
@@ -295,7 +295,7 @@ gather_inet(int proto)
 				break;
 			if (errno == ENOENT)
 				goto out;
-			if (errno != ENOMEM)
+			if (errno != ENOMEM || len != bufsize)
 				err(1, "sysctlbyname()");
 			bufsize *= 2;
 		}
@@ -423,7 +423,7 @@ gather_unix(int proto)
 			len = bufsize;
 			if (sysctlbyname(varname, buf, &len, NULL, 0) == 0)
 				break;
-			if (errno != ENOMEM)
+			if (errno != ENOMEM || len != bufsize)
 				err(1, "sysctlbyname()");
 			bufsize *= 2;
 		}
@@ -475,14 +475,15 @@ out:
 static void
 getfiles(void)
 {
-	size_t len;
+	size_t len, olen;
 
-	if ((xfiles = malloc(len = sizeof *xfiles)) == NULL)
+	olen = len = sizeof *xfiles;
+	if ((xfiles = malloc(len)) == NULL)
 		err(1, "malloc()");
 	while (sysctlbyname("kern.file", xfiles, &len, 0, 0) == -1) {
-		if (errno != ENOMEM)
+		if (errno != ENOMEM || len != olen)
 			err(1, "sysctlbyname()");
-		len *= 2;
+		olen = len *= 2;
 		if ((xfiles = realloc(xfiles, len)) == NULL)
 			err(1, "realloc()");
 	}



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