svn commit: r299652 - stable/9/lib/libkvm

Garrett Cooper ngie at FreeBSD.org
Fri May 13 09:54:17 UTC 2016


Author: ngie
Date: Fri May 13 09:54:15 2016
New Revision: 299652
URL: https://svnweb.freebsd.org/changeset/base/299652

Log:
  MFstable/10 r299651:
  
  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

Modified:
  stable/9/lib/libkvm/kvm_proc.c
Directory Properties:
  stable/9/   (props changed)
  stable/9/lib/   (props changed)
  stable/9/lib/libkvm/   (props changed)

Modified: stable/9/lib/libkvm/kvm_proc.c
==============================================================================
--- stable/9/lib/libkvm/kvm_proc.c	Fri May 13 09:52:39 2016	(r299651)
+++ stable/9/lib/libkvm/kvm_proc.c	Fri May 13 09:54:15 2016	(r299652)
@@ -642,6 +642,7 @@ kvm_argv(kvm_t *kd, const struct kinfo_p
 	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_p
 			_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_p
 		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;


More information about the svn-src-stable-9 mailing list