From 46b1d8cf7a2b8b3c38ca747aea7cd3951dbea0e1 Mon Sep 17 00:00:00 2001 From: John Ferlan Date: Wed, 30 Jan 2013 15:43:39 -0500 Subject: [PATCH] Enforce return check on virAsprintf() calls Way back when I started making changes for Coverity messages my first set were to a bunch of CHECKED_RETURN errors. In particular virAsprintf() had a few callers that Coverity noted didn't check their return (although some did check if the buffer being printed to was NULL or not). It was suggested at the time as a further patch an ATTRIBUTE_RETURN_CHECK should be added to virAsprintf(), see: https://www.redhat.com/archives/libvir-list/2013-January/msg00120.html This patch does that and fixes a few more instances not found by Coverity that failed the check. --- src/util/virerror.c | 4 ++-- src/util/virutil.h | 8 +++++--- src/xen/xend_internal.c | 6 ++---- tests/networkxml2conftest.c | 5 ++--- tools/virsh-domain.c | 5 ++--- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/src/util/virerror.c b/src/util/virerror.c index 500c32bcb3..301a1aca59 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -1,7 +1,7 @@ /* * virerror.c: error handling and reporting code for libvirt * - * Copyright (C) 2006, 2008-2012 Red Hat, Inc. + * Copyright (C) 2006, 2008-2013 Red Hat, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -647,7 +647,7 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED, } else { va_list ap; va_start(ap, fmt); - virVasprintf(&str, fmt, ap); + ignore_value(virVasprintf(&str, fmt, ap)); va_end(ap); } diff --git a/src/util/virutil.h b/src/util/virutil.h index 5a08c8138b..4201aa10a9 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -1,7 +1,7 @@ /* * virutil.h: common, generic utility functions * - * Copyright (C) 2010-2012 Red Hat, Inc. + * Copyright (C) 2010-2013 Red Hat, Inc. * Copyright (C) 2006, 2007 Binary Karma * Copyright (C) 2006 Shuveb Hussain * @@ -200,9 +200,11 @@ int virParseNumber(const char **str); int virParseVersionString(const char *str, unsigned long *version, bool allowMissing); int virAsprintf(char **strp, const char *fmt, ...) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3); + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 3) + ATTRIBUTE_RETURN_CHECK; int virVasprintf(char **strp, const char *fmt, va_list list) - ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0); + ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0) + ATTRIBUTE_RETURN_CHECK; char *virStrncpy(char *dest, const char *src, size_t n, size_t destbytes) ATTRIBUTE_RETURN_CHECK; char *virStrcpy(char *dest, const char *src, size_t destbytes) diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c index b03b7bcb3c..3036cbbae5 100644 --- a/src/xen/xend_internal.c +++ b/src/xen/xend_internal.c @@ -2738,10 +2738,8 @@ xenDaemonAttachDeviceFlags(virDomainPtr domain, const char *xml, virDevicePCIAddress PCIAddr; PCIAddr = dev->data.hostdev->source.subsys.u.pci; - virAsprintf(&target, "PCI device: %.4x:%.2x:%.2x", PCIAddr.domain, - PCIAddr.bus, PCIAddr.slot); - - if (target == NULL) { + if (virAsprintf(&target, "PCI device: %.4x:%.2x:%.2x", + PCIAddr.domain, PCIAddr.bus, PCIAddr.slot) < 0) { virReportOOMError(); goto cleanup; } diff --git a/tests/networkxml2conftest.c b/tests/networkxml2conftest.c index 397954089e..88016ee119 100644 --- a/tests/networkxml2conftest.c +++ b/tests/networkxml2conftest.c @@ -102,9 +102,8 @@ testDnsmasqLeaseFileName(const char *netname) { char *leasefile; - virAsprintf(&leasefile, "/var/lib/libvirt/dnsmasq/%s.leases", - netname); - + ignore_value(virAsprintf(&leasefile, "/var/lib/libvirt/dnsmasq/%s.leases", + netname)); return leasefile; } diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 004fac4734..151b3494f0 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -1,7 +1,7 @@ /* * virsh-domain.c: Commands to manage domain * - * Copyright (C) 2005, 2007-2012 Red Hat, Inc. + * Copyright (C) 2005, 2007-2013 Red Hat, Inc. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -9011,8 +9011,7 @@ vshCompleteXMLFromDomain(vshControl *ctl, virDomainPtr dom, char *oldXML, } /* Get all possible devices */ - virAsprintf(&xpath, "/domain/devices/%s", node->name); - if (!xpath) { + if (virAsprintf(&xpath, "/domain/devices/%s", node->name) < 0) { virReportOOMError(); goto cleanup; }