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:
parent
fba34872ec
commit
2368f18ac4
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user