aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJung-uk Kim <jkim@FreeBSD.org>2021-03-03 23:10:00 +0000
committerJung-uk Kim <jkim@FreeBSD.org>2021-03-03 23:10:00 +0000
commit645eaa2ccaed6eea801d07d6a092974fc1713896 (patch)
tree3ca2401358899d24c0e3748c0b7eeb682dbc0e37
parente6cfd2939a4261c1f4bf802368cea8faf824c128 (diff)
downloadsrc-645eaa2ccaed6eea801d07d6a092974fc1713896.tar.gz
src-645eaa2ccaed6eea801d07d6a092974fc1713896.zip
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
-rw-r--r--lib/libkvm/kvm.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/lib/libkvm/kvm.c b/lib/libkvm/kvm.c
index 95c5a580a2dd..2905302f32f2 100644
--- a/lib/libkvm/kvm.c
+++ b/lib/libkvm/kvm.c
@@ -301,6 +301,8 @@ kvm_close(kvm_t *kd)
free(kd->pt_map);
if (kd->page_map != NULL)
free(kd->page_map);
+ if (kd->dpcpu_initialized != 0)
+ free(kd->dpcpu_off);
if (kd->sparse_map != MAP_FAILED)
munmap(kd->sparse_map, kd->pt_sparse_size);
free((void *)kd);
@@ -340,6 +342,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);
@@ -349,6 +355,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);
}