summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorngie <ngie@FreeBSD.org>2016-05-13 09:52:39 +0000
committerngie <ngie@FreeBSD.org>2016-05-13 09:52:39 +0000
commit999fa1c6e0efd35482d15b1b8f57f1bb8b76d00f (patch)
tree95eb98e99c8c36fa0ae54b8c8c0d6f4b0b8d67e2 /lib
parent4b32d6fd17461cf94aa5beb9217985fd26d6a36b (diff)
downloadFreeBSD-src-999fa1c6e0efd35482d15b1b8f57f1bb8b76d00f.zip
FreeBSD-src-999fa1c6e0efd35482d15b1b8f57f1bb8b76d00f.tar.gz
MFC r298839:
Fix memory allocation edgecases in kvm_argv(..) - Don't leak nbufp on realloc failure in kvm_argv - Catch malloc errors with bufp - Set buflen last in the "buflen == 0" case to ensure that bufp/nbufp is properly reallocated on the next go around
Diffstat (limited to 'lib')
-rw-r--r--lib/libkvm/kvm_proc.c15
1 files changed, 12 insertions, 3 deletions
diff --git a/lib/libkvm/kvm_proc.c b/lib/libkvm/kvm_proc.c
index 31258d7..f8e152e 100644
--- a/lib/libkvm/kvm_proc.c
+++ b/lib/libkvm/kvm_proc.c
@@ -642,6 +642,7 @@ kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr)
static char *buf, *p;
static char **bufp;
static int argc;
+ char **nbufp;
if (!ISALIVE(kd)) {
_kvm_err(kd, kd->program,
@@ -657,9 +658,15 @@ kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr)
_kvm_err(kd, kd->program, "cannot allocate memory");
return (0);
}
- buflen = nchr;
argc = 32;
bufp = malloc(sizeof(char *) * argc);
+ if (bufp == NULL) {
+ free(buf);
+ buf = NULL;
+ _kvm_err(kd, kd->program, "cannot allocate memory");
+ return (NULL);
+ }
+ buflen = nchr;
} else if (nchr > buflen) {
p = realloc(buf, nchr);
if (p != NULL) {
@@ -693,8 +700,10 @@ kvm_argv(kvm_t *kd, const struct kinfo_proc *kp, int env, int nchr)
p += strlen(p) + 1;
if (i >= argc) {
argc += argc;
- bufp = realloc(bufp,
- sizeof(char *) * argc);
+ nbufp = realloc(bufp, sizeof(char *) * argc);
+ if (nbufp == NULL)
+ return (NULL);
+ bufp = nbufp;
}
} while (p < buf + bufsz);
bufp[i++] = 0;
OpenPOWER on IntegriCloud