From owner-dev-commits-src-main@freebsd.org Wed Apr 21 18:20:07 2021 Return-Path: Delivered-To: dev-commits-src-main@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 7ED915EDD71; Wed, 21 Apr 2021 18:20:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4FQTP33D9Cz3rwX; Wed, 21 Apr 2021 18:20:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 5D9B32A7E; Wed, 21 Apr 2021 18:20:07 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 13LIK7J3046434; Wed, 21 Apr 2021 18:20:07 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 13LIK7Wi046431; Wed, 21 Apr 2021 18:20:07 GMT (envelope-from git) Date: Wed, 21 Apr 2021 18:20:07 GMT Message-Id: <202104211820.13LIK7Wi046431@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Michael Reifenberger Subject: git: dcc2fb370791 - main - systat: Avoid incorrect reallocation in pigs.c MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: mr X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: dcc2fb3707919c5184480d8cbde98d16f24a3945 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-main@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for the main branch of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Apr 2021 18:20:07 -0000 The branch main has been updated by mr: URL: https://cgit.FreeBSD.org/src/commit/?id=dcc2fb3707919c5184480d8cbde98d16f24a3945 commit dcc2fb3707919c5184480d8cbde98d16f24a3945 Author: Michael Reifenberger AuthorDate: 2021-04-21 18:09:21 +0000 Commit: Michael Reifenberger CommitDate: 2021-04-21 18:09:21 +0000 systat: Avoid incorrect reallocation in pigs.c Stop free() even if kvm_getprocs as we can come back but set nprocs = 0. Check nprocs in showpigs() to ensure not try displaying with kvm_getprocs failed. Current code can have pt with non-null after kvm_getprocs() failure. Replace to realloc for simpler operations. Submitted by: Yoshihiro Ota ota@j.email.ne.jp Reviewed by: mckusick@ Differential Revision: https://reviews.freebsd.org/D29303 --- usr.bin/systat/pigs.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/usr.bin/systat/pigs.c b/usr.bin/systat/pigs.c index d9f3f8d4ad14..0bec6aceaaa1 100644 --- a/usr.bin/systat/pigs.c +++ b/usr.bin/systat/pigs.c @@ -62,7 +62,7 @@ static int nproc; static struct p_times { float pt_pctcpu; struct kinfo_proc *pt_kp; -} *pt; +} *pt = NULL; static int fscale; static double lccpu; @@ -90,7 +90,7 @@ showpigs(void) const char *uname, *pname; char pidname[30]; - if (pt == NULL) + if (nproc == 0) return; qsort(pt, nproc, sizeof (struct p_times), compar); @@ -146,23 +146,20 @@ fetchpigs(void) float ftime; float *pctp; struct kinfo_proc *kpp; - static int lastnproc = 0; + static int maxnproc = 0; if ((kpp = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nproc)) == NULL) { error("%s", kvm_geterr(kd)); - if (pt) - free(pt); + nproc = 0; return; } - if (nproc > lastnproc) { - free(pt); - if ((pt = - malloc(nproc * sizeof(struct p_times))) == NULL) { + if (nproc > maxnproc) { + if ((pt = realloc(pt, nproc * sizeof(*pt))) == NULL) { error("Out of memory"); die(0); } + maxnproc = nproc; } - lastnproc = nproc; /* * calculate %cpu for each proc */