From a9f56210b8b8a8dc6b19c46445a79600f523d9dd Mon Sep 17 00:00:00 2001 From: ngie Date: Fri, 22 Apr 2016 18:05:34 +0000 Subject: Fix up pointer issues with lib/libkvm In particular, - avoid dereferencing NULL pointers - test pointers against NULL, not 0 - test for errout == NULL in the top-level functions (kvm_open, kvm_openfiles, kvm_open2, etc) - Replace a realloc and free on failure with reallocf Found with: devel/cocchinelle Differential Revision: https://reviews.freebsd.org/D5954 MFC after: 1 week Reviewed by: jhb Sponsored by: EMC / Isilon Storage Division --- lib/libkvm/kvm_amd64.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'lib/libkvm/kvm_amd64.c') diff --git a/lib/libkvm/kvm_amd64.c b/lib/libkvm/kvm_amd64.c index 70789f4..57a8728 100644 --- a/lib/libkvm/kvm_amd64.c +++ b/lib/libkvm/kvm_amd64.c @@ -118,7 +118,7 @@ _amd64_initvtop(kvm_t *kd) amd64_pml4e_t *PML4; kd->vmst = (struct vmstate *)_kvm_malloc(kd, sizeof(*kd->vmst)); - if (kd->vmst == 0) { + if (kd->vmst == NULL) { _kvm_err(kd, kd->program, "cannot allocate vm"); return (-1); } @@ -153,6 +153,10 @@ _amd64_initvtop(kvm_t *kd) } pa = le64toh(pa); PML4 = _kvm_malloc(kd, AMD64_PAGE_SIZE); + if (PML4 == NULL) { + _kvm_err(kd, kd->program, "cannot allocate PML4"); + return (-1); + } if (kvm_read2(kd, pa, PML4, AMD64_PAGE_SIZE) != AMD64_PAGE_SIZE) { _kvm_err(kd, kd->program, "cannot read KPML4phys"); return (-1); @@ -188,7 +192,7 @@ _amd64_vatop(kvm_t *kd, kvaddr_t va, off_t *pa) * If we are initializing (kernel page table descriptor pointer * not yet set) then return pa == va to avoid infinite recursion. */ - if (vm->PML4 == 0) { + if (vm->PML4 == NULL) { s = _kvm_pa2off(kd, va, pa); if (s == 0) { _kvm_err(kd, kd->program, -- cgit v1.1