From 2368f18ac465224da2b90e0037f39d141448e123 Mon Sep 17 00:00:00 2001 From: marcus Date: Fri, 19 Dec 2008 06:47:59 +0000 Subject: 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 --- lib/libutil/kinfo_getvmmap.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lib/libutil/kinfo_getvmmap.c') diff --git a/lib/libutil/kinfo_getvmmap.c b/lib/libutil/kinfo_getvmmap.c index 5436108..559182e 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.1