Initialize the cntp pointer to 0 prior to doing any work so that callers

don't try to iterate through garbage or NULL memory.  Additionally, return
NULL instead of 0 on error.

Reviewed by:	peter
Approved by:	peter
This commit is contained in:
Joe Marcus Clarke 2008-12-19 06:47:59 +00:00
parent e657c679e1
commit 6c3b8117ad
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=186314
2 changed files with 10 additions and 8 deletions

View File

@ -19,6 +19,7 @@ kinfo_getfile(pid_t pid, int *cntp)
char *buf, *bp, *eb;
struct kinfo_file *kif, *kp, *kf;
*cntp = 0;
len = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
@ -27,15 +28,15 @@ kinfo_getfile(pid_t pid, int *cntp)
error = sysctl(mib, 4, NULL, &len, NULL, 0);
if (error)
return (0);
return (NULL);
len = len * 4 / 3;
buf = malloc(len);
if (buf == NULL)
return (0);
return (NULL);
error = sysctl(mib, 4, buf, &len, NULL, 0);
if (error) {
free(buf);
return (0);
return (NULL);
}
/* Pass 1: count items */
cnt = 0;
@ -50,7 +51,7 @@ kinfo_getfile(pid_t pid, int *cntp)
kif = calloc(cnt, sizeof(*kif));
if (kif == NULL) {
free(buf);
return (0);
return (NULL);
}
bp = buf;
eb = buf + len;

View File

@ -19,6 +19,7 @@ kinfo_getvmmap(pid_t pid, int *cntp)
char *buf, *bp, *eb;
struct kinfo_vmentry *kiv, *kp, *kv;
*cntp = 0;
len = 0;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
@ -27,15 +28,15 @@ kinfo_getvmmap(pid_t pid, int *cntp)
error = sysctl(mib, 4, NULL, &len, NULL, 0);
if (error)
return (0);
return (NULL);
len = len * 4 / 3;
buf = malloc(len);
if (buf == NULL)
return (0);
return (NULL);
error = sysctl(mib, 4, buf, &len, NULL, 0);
if (error) {
free(buf);
return (0);
return (NULL);
}
/* Pass 1: count items */
cnt = 0;
@ -50,7 +51,7 @@ kinfo_getvmmap(pid_t pid, int *cntp)
kiv = calloc(cnt, sizeof(*kiv));
if (kiv == NULL) {
free(buf);
return (0);
return (NULL);
}
bp = buf;
eb = buf + len;