While convenient, avoid using alloca() for reasons specified in
the BUGS section of the alloca(3) manpage. In particular, when the number of TCP sockets is several tens of thousand, trying to "sysctl -a" would SIGSEGV on the net.inet.tcp.pcblist entry (it would exceed the stacksize ulimit, in an undetectable manner). Reported by: Igor Sysoev
This commit is contained in:
parent
14ca3cd6ff
commit
ccf2597759
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=162073
@ -541,7 +541,7 @@ oidfmt(int *oid, int len, char *fmt, u_int *kind)
|
|||||||
static int
|
static int
|
||||||
show_var(int *oid, int nlen)
|
show_var(int *oid, int nlen)
|
||||||
{
|
{
|
||||||
u_char buf[BUFSIZ], *val, *p;
|
u_char buf[BUFSIZ], *val, *oval, *p;
|
||||||
char name[BUFSIZ], *fmt, *sep;
|
char name[BUFSIZ], *fmt, *sep;
|
||||||
int qoid[CTL_MAXNAME+2];
|
int qoid[CTL_MAXNAME+2];
|
||||||
int i;
|
int i;
|
||||||
@ -584,14 +584,21 @@ show_var(int *oid, int nlen)
|
|||||||
i = sysctl(oid, nlen, 0, &j, 0, 0);
|
i = sysctl(oid, nlen, 0, &j, 0, 0);
|
||||||
j += j; /* we want to be sure :-) */
|
j += j; /* we want to be sure :-) */
|
||||||
|
|
||||||
val = alloca(j + 1);
|
val = oval = malloc(j + 1);
|
||||||
|
if (val == NULL) {
|
||||||
|
warnx("malloc failed");
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
len = j;
|
len = j;
|
||||||
i = sysctl(oid, nlen, val, &len, 0, 0);
|
i = sysctl(oid, nlen, val, &len, 0, 0);
|
||||||
if (i || !len)
|
if (i || !len) {
|
||||||
|
free(oval);
|
||||||
return (1);
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
if (bflag) {
|
if (bflag) {
|
||||||
fwrite(val, 1, len, stdout);
|
fwrite(val, 1, len, stdout);
|
||||||
|
free(oval);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
val[len] = '\0';
|
val[len] = '\0';
|
||||||
@ -603,6 +610,7 @@ show_var(int *oid, int nlen)
|
|||||||
if (!nflag)
|
if (!nflag)
|
||||||
printf("%s%s", name, sep);
|
printf("%s%s", name, sep);
|
||||||
printf("%.*s", len, p);
|
printf("%.*s", len, p);
|
||||||
|
free(oval);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
case 'I':
|
case 'I':
|
||||||
@ -630,6 +638,7 @@ show_var(int *oid, int nlen)
|
|||||||
len -= sizeof(int);
|
len -= sizeof(int);
|
||||||
p += sizeof(int);
|
p += sizeof(int);
|
||||||
}
|
}
|
||||||
|
free(oval);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
case 'L':
|
case 'L':
|
||||||
@ -657,12 +666,14 @@ show_var(int *oid, int nlen)
|
|||||||
len -= sizeof(long);
|
len -= sizeof(long);
|
||||||
p += sizeof(long);
|
p += sizeof(long);
|
||||||
}
|
}
|
||||||
|
free(oval);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
case 'P':
|
case 'P':
|
||||||
if (!nflag)
|
if (!nflag)
|
||||||
printf("%s%s", name, sep);
|
printf("%s%s", name, sep);
|
||||||
printf("%p", *(void **)p);
|
printf("%p", *(void **)p);
|
||||||
|
free(oval);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
||||||
case 'T':
|
case 'T':
|
||||||
@ -683,12 +694,15 @@ show_var(int *oid, int nlen)
|
|||||||
if (func) {
|
if (func) {
|
||||||
if (!nflag)
|
if (!nflag)
|
||||||
printf("%s%s", name, sep);
|
printf("%s%s", name, sep);
|
||||||
|
free(oval);
|
||||||
return ((*func)(len, p));
|
return ((*func)(len, p));
|
||||||
}
|
}
|
||||||
/* FALLTHROUGH */
|
/* FALLTHROUGH */
|
||||||
default:
|
default:
|
||||||
if (!oflag && !xflag)
|
if (!oflag && !xflag) {
|
||||||
|
free(oval);
|
||||||
return (1);
|
return (1);
|
||||||
|
}
|
||||||
if (!nflag)
|
if (!nflag)
|
||||||
printf("%s%s", name, sep);
|
printf("%s%s", name, sep);
|
||||||
printf("Format:%s Length:%d Dump:0x", fmt, len);
|
printf("Format:%s Length:%d Dump:0x", fmt, len);
|
||||||
@ -696,8 +710,10 @@ show_var(int *oid, int nlen)
|
|||||||
printf("%02x", *p++);
|
printf("%02x", *p++);
|
||||||
if (!xflag && len > 16)
|
if (!xflag && len > 16)
|
||||||
printf("...");
|
printf("...");
|
||||||
|
free(oval);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
free(oval);
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user