From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thomas Huth Subject: Re: [kvm-unit-tests PATCH 02/18] trivial: lib: fail hard on failed mallocs Date: Fri, 6 Nov 2015 15:05:41 +0100 Message-ID: <563CB3B5.9090304@redhat.com> References: <1446769483-21586-1-git-send-email-drjones@redhat.com> <1446769483-21586-3-git-send-email-drjones@redhat.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Cc: alex.bennee@linaro.org, cov@codeaurora.org To: Andrew Jones , pbonzini@redhat.com, kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:50977 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932918AbbKFOFp (ORCPT ); Fri, 6 Nov 2015 09:05:45 -0500 In-Reply-To: <1446769483-21586-3-git-send-email-drjones@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: On 06/11/15 01:24, Andrew Jones wrote: > It's pretty safe to not even bother checking for NULL when > using malloc and friends, but if we do check, then fail > hard. > > Signed-off-by: Andrew Jones > --- > lib/virtio-mmio.c | 7 ++----- > 1 file changed, 2 insertions(+), 5 deletions(-) > > diff --git a/lib/virtio-mmio.c b/lib/virtio-mmio.c > index 043832299174e..1b6f0cc378b79 100644 > --- a/lib/virtio-mmio.c > +++ b/lib/virtio-mmio.c ... > @@ -161,9 +160,7 @@ static struct virtio_device *virtio_mmio_dt_bind(u32 devid) > if (node == -FDT_ERR_NOTFOUND) > return NULL; > > - vm_dev = calloc(1, sizeof(*vm_dev)); > - if (!vm_dev) > - return NULL; > + assert((vm_dev = calloc(1, sizeof(*vm_dev))) != NULL); Could you maybe write that as two statements? assert() is classically a macro which could also be disabled, so if somebody introduces a switch to "#define assert(...) /*nothing*/" in the future, that calloc call would be completely gone! Thomas