From owner-svn-src-head@freebsd.org Sat Apr 30 09:13:27 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 81061B0FB5B; Sat, 30 Apr 2016 09:13:27 +0000 (UTC) (envelope-from ngie@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 53280195E; Sat, 30 Apr 2016 09:13:27 +0000 (UTC) (envelope-from ngie@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id u3U9DQZF051581; Sat, 30 Apr 2016 09:13:26 GMT (envelope-from ngie@FreeBSD.org) Received: (from ngie@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id u3U9DQOk051580; Sat, 30 Apr 2016 09:13:26 GMT (envelope-from ngie@FreeBSD.org) Message-Id: <201604300913.u3U9DQOk051580@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: ngie set sender to ngie@FreeBSD.org using -f From: Garrett Cooper Date: Sat, 30 Apr 2016 09:13:26 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r298839 - head/lib/libkvm X-SVN-Group: head MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 30 Apr 2016 09:13:27 -0000 Author: ngie Date: Sat Apr 30 09:13:26 2016 New Revision: 298839 URL: https://svnweb.freebsd.org/changeset/base/298839 Log: 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 Differential Revision: https://reviews.freebsd.org/D6051 MFC after: 1 week Reviewed by: jhb, markj Reported by: cppcheck Sponsored by: EMC / Isilon Storage Division Modified: head/lib/libkvm/kvm_proc.c Modified: head/lib/libkvm/kvm_proc.c ============================================================================== --- head/lib/libkvm/kvm_proc.c Sat Apr 30 06:48:48 2016 (r298838) +++ head/lib/libkvm/kvm_proc.c Sat Apr 30 09:13:26 2016 (r298839) @@ -666,6 +666,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, @@ -681,9 +682,15 @@ kvm_argv(kvm_t *kd, const struct kinfo_p _kvm_err(kd, kd->program, "cannot allocate memory"); return (NULL); } - 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) { @@ -716,8 +723,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;