svn commit: r230874 - head/usr.bin/sockstat

[ Available lists | Index of svn-src-head | Month of Feb 2012 | Week of 1 Feb 2012 | Raw email | View thread | Wrap long lines | Reply | Tag ]
From
Mikolaj Golub <trociny@FreeBSD.org>
Date
1 Feb 2012 18:03:22
Subject
svn commit: r230874 - head/usr.bin/sockstat
Message-ID
201202011803.q11I3MDf098366@svn.freebsd.org


[ Hide this part ]
Author: trociny
Date: Wed Feb 1 18:03:21 2012
New Revision: 230874
URL: http://svn.freebsd.org/changeset/base/230874

Log:
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>
MFC after: 1 week

Modified:
head/usr.bin/sockstat/sockstat.c

Modified: head/usr.bin/sockstat/sockstat.c
==============================================================================
--- head/usr.bin/sockstat/sockstat.c Wed Feb 1 18:02:13 2012 (r230873)
+++ head/usr.bin/sockstat/sockstat.c Wed Feb 1 18:03:21 2012 (r230874)
@@ -296,7 +296,7 @@ gather_inet(int proto)
break;
if (errno == ENOENT)
goto out;
- if (errno != ENOMEM)
+ if (errno != ENOMEM || len != bufsize)
err(1, "sysctlbyname()");
bufsize *= 2;
}
@@ -424,7 +424,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;
}
@@ -476,14 +476,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()");
}


Elapsed time: 0.106 seconds