Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 6 Mar 2021 20:04:59 GMT
From:      Jung-uk Kim <jkim@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 859105d755ce - stable/11 - libkvm: Plug couple of memory leaks and check possible calloc(3) failure
Message-ID:  <202103062004.126K4xXj047752@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/11 has been updated by jkim:

URL: https://cgit.FreeBSD.org/src/commit/?id=859105d755ce0ead5ab356c67a6234b10650477f

commit 859105d755ce0ead5ab356c67a6234b10650477f
Author:     Jung-uk Kim <jkim@FreeBSD.org>
AuthorDate: 2021-03-03 23:10:00 +0000
Commit:     Jung-uk Kim <jkim@FreeBSD.org>
CommitDate: 2021-03-06 20:04:32 +0000

    libkvm: Plug couple of memory leaks and check possible calloc(3) failure
    
    First, r204494 introduced dpcpu_off in struct __kvm and it was allocated
    from _kvm_dpcpu_init() but it was not free(3)'ed from kvm_close(3).
    Second, r291406 introduced kvm_nlist2(3) and converted kvm_nlist(3) to
    use the new function but it did not free the temporary buffer.
    Also, check possible calloc(3) failure while I am in the neighborhood.
    
    MFC after:      3 days
    Differential Revision:  https://reviews.freebsd.org/D29019
    
    (cherry picked from commit 645eaa2ccaed6eea801d07d6a092974fc1713896)
---
 lib/libkvm/kvm.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/lib/libkvm/kvm.c b/lib/libkvm/kvm.c
index d6cc7d847ee6..66f38cef41f6 100644
--- a/lib/libkvm/kvm.c
+++ b/lib/libkvm/kvm.c
@@ -559,6 +559,8 @@ kvm_close(kvm_t *kd)
 		free((void *) kd->argspc);
 	if (kd->argv != 0)
 		free((void *)kd->argv);
+	if (kd->dpcpu_initialized != 0)
+		free(kd->dpcpu_off);
 	free((void *)kd);
 
 	return (error);
@@ -797,6 +799,10 @@ kvm_nlist(kvm_t *kd, struct nlist *nl)
 	if (count == 0)
 		return (0);
 	kl = calloc(count + 1, sizeof(*kl));
+	if (kl == NULL) {
+		_kvm_err(kd, kd->program, "cannot allocate memory");
+		return (-1);
+	}
 	for (i = 0; i < count; i++)
 		kl[i].n_name = nl[i].n_name;
 	nfail = kvm_nlist2(kd, kl);
@@ -806,6 +812,7 @@ kvm_nlist(kvm_t *kd, struct nlist *nl)
 		nl[i].n_desc = 0;
 		nl[i].n_value = kl[i].n_value;
 	}
+	free(kl);
 	return (nfail);
 }
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202103062004.126K4xXj047752>