From 974e3f18163a96826e1728102021d3df3775e956 Mon Sep 17 00:00:00 2001 From: Joe Marcus Clarke Date: Fri, 19 Dec 2008 20:22:55 +0000 Subject: MFC r186314: 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. Approved by: re (kib) --- lib/libutil/kinfo_getfile.c | 9 +++++---- lib/libutil/kinfo_getvmmap.c | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/libutil/kinfo_getfile.c b/lib/libutil/kinfo_getfile.c index ca0f832f1829..7a7a09e2dad8 100644 --- a/lib/libutil/kinfo_getfile.c +++ b/lib/libutil/kinfo_getfile.c @@ -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; diff --git a/lib/libutil/kinfo_getvmmap.c b/lib/libutil/kinfo_getvmmap.c index 54361089a599..559182e40520 100644 --- a/lib/libutil/kinfo_getvmmap.c +++ b/lib/libutil/kinfo_getvmmap.c @@ -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; -- cgit v1.2.3