[libvirt] [PATCH 0/7] static zero initialization

Based on a comment I just made while reviewing Dan's patches, we might as well adopt a consistent style of relying on the C guarantee that static variables are automatically zero-initialized without any effort on our part. Eric Blake (7): audit: use bool for audit log choice maint: avoid static zero init in core files maint: avoid static zero init in tools maint: avoid static zero init in tests maint: avoid static zero init in helpers maint: avoid static zero init in drivers maint: add syntax check to prohibit static zero init cfg.mk | 8 ++++++++ daemon/libvirtd.c | 2 +- src/conf/nwfilter_conf.c | 2 +- src/cpu/cpu_x86.c | 2 +- src/interface/interface_backend_netcf.c | 2 +- src/libvirt.c | 18 +++++++++--------- src/libxl/libxl_driver.c | 2 +- src/locking/lock_daemon.c | 2 +- src/locking/lock_driver_lockd.c | 4 ++-- src/locking/lock_driver_sanlock.c | 2 +- src/lxc/lxc_controller.c | 2 +- src/network/bridge_driver.c | 2 +- src/node_device/node_device_udev.c | 2 +- src/nwfilter/nwfilter_learnipaddr.c | 2 +- src/openvz/openvz_util.c | 4 ++-- src/phyp/phyp_driver.c | 8 +++++--- src/qemu/qemu_driver.c | 5 +++-- src/remote/remote_driver.c | 2 +- src/rpc/virnetserver.c | 4 ++-- src/security/security_selinux.c | 2 +- src/test/test_driver.c | 2 +- src/uml/uml_driver.c | 2 +- src/util/viralloc.c | 10 +++++----- src/util/viraudit.c | 4 ++-- src/util/viraudit.h | 4 ++-- src/util/virdbus.c | 4 ++-- src/util/virevent.c | 14 +++++++------- src/util/virfile.c | 2 +- src/util/virlog.c | 16 ++++++++-------- src/util/virnetlink.c | 2 +- src/util/virprocess.c | 3 +-- src/util/virthread.h | 4 ++-- src/vbox/vbox_XPCOMCGlue.c | 4 ++-- src/vbox/vbox_tmpl.c | 2 +- src/xen/xen_driver.c | 4 ++-- src/xen/xen_hypervisor.c | 4 ++-- tests/eventtest.c | 6 +++--- tests/testutils.c | 12 ++++++------ tests/virhostdevtest.c | 2 +- tests/virportallocatortest.c | 2 +- tests/virscsitest.c | 2 +- tools/virsh-console.c | 5 +---- tools/virsh-domain.c | 2 +- tools/virsh.c | 4 ++-- tools/virt-host-validate-common.c | 4 ++-- 45 files changed, 102 insertions(+), 95 deletions(-) -- 1.9.3

We weren't ever using the value for anything other than being non-zero. * src/util/viraudit.h (virAuditLog): Change signature. * src/util/viraudit.c (virAuditLog): Update user. * daemon/libvirtd.c (main): Likewise. Signed-off-by: Eric Blake <eblake@redhat.com> --- daemon/libvirtd.c | 2 +- src/util/viraudit.c | 4 ++-- src/util/viraudit.h | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c index 329d8d4..1864bfd 100644 --- a/daemon/libvirtd.c +++ b/daemon/libvirtd.c @@ -1443,7 +1443,7 @@ int main(int argc, char **argv) { VIR_DEBUG("Proceeding without auditing"); } } - virAuditLog(config->audit_logging); + virAuditLog(config->audit_logging > 0); /* setup the hooks if any */ if (virHookInitialize() < 0) { diff --git a/src/util/viraudit.c b/src/util/viraudit.c index 23928fd..17e58b3 100644 --- a/src/util/viraudit.c +++ b/src/util/viraudit.c @@ -53,7 +53,7 @@ VIR_LOG_INIT("util.audit"); #if WITH_AUDIT static int auditfd = -1; #endif -static int auditlog = 0; +static bool auditlog; int virAuditOpen(void) { @@ -70,7 +70,7 @@ int virAuditOpen(void) } -void virAuditLog(int logging) +void virAuditLog(bool logging) { auditlog = logging; } diff --git a/src/util/viraudit.h b/src/util/viraudit.h index 8101e50..861cbf6 100644 --- a/src/util/viraudit.h +++ b/src/util/viraudit.h @@ -1,7 +1,7 @@ /* * viraudit.h: auditing support * - * Copyright (C) 2010-2011 Red Hat, Inc. + * Copyright (C) 2010-2011, 2014 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 @@ -34,7 +34,7 @@ typedef enum { int virAuditOpen(void); -void virAuditLog(int enabled); +void virAuditLog(bool enabled); void virAuditSend(virLogSourcePtr source, const char *filename, size_t linenr, const char *funcname, -- 1.9.3

C guarantees that static variables are zero-initialized. Some older compilers (and also gcc -fno-zero-initialized-in-bss) create larger binaries if you explicitly zero-initialize a static variable. * src/libvirt.c: Fix initialization. * src/util/viralloc.c: Likewise. * src/util/virdbus.c: Likewise. * src/util/virevent.c: Likewise. * src/util/virfile.c (safezero): Likewise. * src/util/virlog.c: Likewise. * src/util/virnetlink.c: Likewise. * src/util/virthread.h (VIR_ONCE_GLOBAL_INIT): Likewise. * src/util/virprocess.c (virProcessGetStartTime): Likewise. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/libvirt.c | 18 +++++++++--------- src/util/viralloc.c | 10 +++++----- src/util/virdbus.c | 4 ++-- src/util/virevent.c | 14 +++++++------- src/util/virfile.c | 2 +- src/util/virlog.c | 16 ++++++++-------- src/util/virnetlink.c | 2 +- src/util/virprocess.c | 3 +-- src/util/virthread.h | 4 ++-- 9 files changed, 36 insertions(+), 37 deletions(-) diff --git a/src/libvirt.c b/src/libvirt.c index 5594883..3abedb4 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -122,22 +122,22 @@ VIR_LOG_INIT("libvirt"); } while (0) static virHypervisorDriverPtr virHypervisorDriverTab[MAX_DRIVERS]; -static int virHypervisorDriverTabCount = 0; +static int virHypervisorDriverTabCount; static virNetworkDriverPtr virNetworkDriverTab[MAX_DRIVERS]; -static int virNetworkDriverTabCount = 0; +static int virNetworkDriverTabCount; static virInterfaceDriverPtr virInterfaceDriverTab[MAX_DRIVERS]; -static int virInterfaceDriverTabCount = 0; +static int virInterfaceDriverTabCount; static virStorageDriverPtr virStorageDriverTab[MAX_DRIVERS]; -static int virStorageDriverTabCount = 0; +static int virStorageDriverTabCount; static virNodeDeviceDriverPtr virNodeDeviceDriverTab[MAX_DRIVERS]; -static int virNodeDeviceDriverTabCount = 0; +static int virNodeDeviceDriverTabCount; static virSecretDriverPtr virSecretDriverTab[MAX_DRIVERS]; -static int virSecretDriverTabCount = 0; +static int virSecretDriverTabCount; static virNWFilterDriverPtr virNWFilterDriverTab[MAX_DRIVERS]; -static int virNWFilterDriverTabCount = 0; +static int virNWFilterDriverTabCount; #ifdef WITH_LIBVIRTD static virStateDriverPtr virStateDriverTab[MAX_DRIVERS]; -static int virStateDriverTabCount = 0; +static int virStateDriverTabCount; #endif @@ -354,7 +354,7 @@ static struct gcry_thread_cbs virTLSThreadImpl = { #endif /* WITH_GNUTLS_GCRYPT */ -static bool virGlobalError = false; +static bool virGlobalError; static virOnceControl virGlobalOnce = VIR_ONCE_CONTROL_INITIALIZER; static void diff --git a/src/util/viralloc.c b/src/util/viralloc.c index dc423f5..63f43d0 100644 --- a/src/util/viralloc.c +++ b/src/util/viralloc.c @@ -32,11 +32,11 @@ VIR_LOG_INIT("util.alloc"); #if TEST_OOM -static int testMallocNext = 0; -static int testMallocFailFirst = 0; -static int testMallocFailLast = 0; -static void (*testMallocHook)(int, void*) = NULL; -static void *testMallocHookData = NULL; +static int testMallocNext; +static int testMallocFailFirst; +static int testMallocFailLast; +static void (*testMallocHook)(int, void*); +static void *testMallocHookData; void virAllocTestInit(void) { diff --git a/src/util/virdbus.c b/src/util/virdbus.c index dc3a535..7c24cbf 100644 --- a/src/util/virdbus.c +++ b/src/util/virdbus.c @@ -35,8 +35,8 @@ VIR_LOG_INIT("util.dbus"); #ifdef WITH_DBUS static bool sharedBus = true; -static DBusConnection *systembus = NULL; -static DBusConnection *sessionbus = NULL; +static DBusConnection *systembus; +static DBusConnection *sessionbus; static virOnceControl systemonce = VIR_ONCE_CONTROL_INITIALIZER; static virOnceControl sessiononce = VIR_ONCE_CONTROL_INITIALIZER; static DBusError systemdbuserr; diff --git a/src/util/virevent.c b/src/util/virevent.c index 84d28a4..54b6396 100644 --- a/src/util/virevent.c +++ b/src/util/virevent.c @@ -32,12 +32,12 @@ VIR_LOG_INIT("util.event"); -static virEventAddHandleFunc addHandleImpl = NULL; -static virEventUpdateHandleFunc updateHandleImpl = NULL; -static virEventRemoveHandleFunc removeHandleImpl = NULL; -static virEventAddTimeoutFunc addTimeoutImpl = NULL; -static virEventUpdateTimeoutFunc updateTimeoutImpl = NULL; -static virEventRemoveTimeoutFunc removeTimeoutImpl = NULL; +static virEventAddHandleFunc addHandleImpl; +static virEventUpdateHandleFunc updateHandleImpl; +static virEventRemoveHandleFunc removeHandleImpl; +static virEventAddTimeoutFunc addTimeoutImpl; +static virEventUpdateTimeoutFunc updateTimeoutImpl; +static virEventRemoveTimeoutFunc removeTimeoutImpl; /***************************************************** @@ -291,7 +291,7 @@ int virEventRegisterDefaultImpl(void) * function, as it will block forever if there are no * registered events. * - * static bool quit = false; + * static bool quit; * * while (!quit) { * if (virEventRunDefaultImpl() < 0) diff --git a/src/util/virfile.c b/src/util/virfile.c index c379df5..64f0e5b 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -1054,7 +1054,7 @@ safezero(int fd, off_t offset, off_t len) char *buf; unsigned long long remain, bytes; # ifdef HAVE_MMAP - static long pagemask = 0; + static long pagemask; off_t map_skip; /* align offset and length, rounding offset down and length up */ diff --git a/src/util/virlog.c b/src/util/virlog.c index 056950e..286ad9e 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -1,7 +1,7 @@ /* * virlog.c: internal logging and debugging * - * Copyright (C) 2008, 2010-2013 Red Hat, Inc. + * Copyright (C) 2008, 2010-2014 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 @@ -61,7 +61,7 @@ VIR_LOG_INIT("util.log"); -static regex_t *virLogRegex = NULL; +static regex_t *virLogRegex; #define VIR_LOG_DATE_REGEX "[0-9]{4}-[0-9]{2}-[0-9]{2}" @@ -86,8 +86,8 @@ typedef struct _virLogFilter virLogFilter; typedef virLogFilter *virLogFilterPtr; static int virLogFiltersSerial = 1; -static virLogFilterPtr virLogFilters = NULL; -static int virLogNbFilters = 0; +static virLogFilterPtr virLogFilters; +static int virLogNbFilters; /* * Outputs are used to emit the messages retained @@ -105,8 +105,8 @@ struct _virLogOutput { typedef struct _virLogOutput virLogOutput; typedef virLogOutput *virLogOutputPtr; -static virLogOutputPtr virLogOutputs = NULL; -static int virLogNbOutputs = 0; +static virLogOutputPtr virLogOutputs; +static int virLogNbOutputs; /* * Default priorities @@ -645,7 +645,7 @@ virLogStackTraceToFd(int fd) { void *array[100]; int size; - static bool doneWarning = false; + static bool doneWarning; const char *msg = "Stack trace not available on this platform\n"; #define STRIP_DEPTH 3 @@ -782,7 +782,7 @@ virLogOutputToSyslog(virLogSourcePtr source ATTRIBUTE_UNUSED, syslog(virLogPrioritySyslog(priority), "%s", str); } -static char *current_ident = NULL; +static char *current_ident; static void diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c index 29511ad..eab888f 100644 --- a/src/util/virnetlink.c +++ b/src/util/virnetlink.c @@ -103,7 +103,7 @@ static int nextWatch = 1; /* Linux kernel supports up to MAX_LINKS (32 at the time) individual * netlink protocols. */ static virNetlinkEventSrvPrivatePtr server[MAX_LINKS] = {NULL}; -static virNetlinkHandle *placeholder_nlhandle = NULL; +static virNetlinkHandle *placeholder_nlhandle; /* Function definitions */ diff --git a/src/util/virprocess.c b/src/util/virprocess.c index fe497b9..0c8a32f 100644 --- a/src/util/virprocess.c +++ b/src/util/virprocess.c @@ -916,11 +916,10 @@ int virProcessGetStartTime(pid_t pid, int virProcessGetStartTime(pid_t pid, unsigned long long *timestamp) { - static int warned = 0; + static int warned; if (virAtomicIntInc(&warned) == 1) { VIR_WARN("Process start time of pid %llu not available on this platform", (unsigned long long)pid); - warned = true; } *timestamp = 0; return 0; diff --git a/src/util/virthread.h b/src/util/virthread.h index 4b92a43..7146f0f 100644 --- a/src/util/virthread.h +++ b/src/util/virthread.h @@ -1,7 +1,7 @@ /* * virthread.h: basic thread synchronization primitives * - * Copyright (C) 2009-2011, 2013 Red Hat, Inc. + * Copyright (C) 2009-2011, 2013-2014 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 @@ -187,7 +187,7 @@ int virThreadLocalSet(virThreadLocalPtr l, void*) ATTRIBUTE_RETURN_CHECK; */ # define VIR_ONCE_GLOBAL_INIT(classname) \ static virOnceControl classname ## OnceControl = VIR_ONCE_CONTROL_INITIALIZER; \ - static virErrorPtr classname ## OnceError = NULL; \ + static virErrorPtr classname ## OnceError; \ \ static void classname ## Once(void) \ { \ -- 1.9.3

C guarantees that static variables are zero-initialized. Some older compilers (and also gcc -fno-zero-initialized-in-bss) create larger binaries if you explicitly zero-initialize a static variable. * tools/virsh-console.c (got_signal): Drop unused variable. * tools/virsh-domain.c: Fix initialization. * tools/virsh.c: Likewise. * tools/virt-host-validate-common.c (virHostMsgWantEscape): Likewise. Signed-off-by: Eric Blake <eblake@redhat.com> --- tools/virsh-console.c | 5 +---- tools/virsh-domain.c | 2 +- tools/virsh.c | 4 ++-- tools/virt-host-validate-common.c | 4 ++-- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/tools/virsh-console.c b/tools/virsh-console.c index 9838a56..c245df7 100644 --- a/tools/virsh-console.c +++ b/tools/virsh-console.c @@ -1,7 +1,7 @@ /* * virsh-console.c: A dumb serial console client * - * Copyright (C) 2007-2008, 2010-2013 Red Hat, Inc. + * Copyright (C) 2007-2008, 2010-2014 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 @@ -81,11 +81,9 @@ struct virConsole { }; -static int got_signal = 0; static void virConsoleHandleSignal(int sig ATTRIBUTE_UNUSED) { - got_signal = 1; } @@ -336,7 +334,6 @@ vshRunConsole(vshControl *ctl, /* Trap all common signals so that we can safely restore the original * terminal settings on STDIN before the process exits - people don't like * being left with a messed up terminal ! */ - got_signal = 0; sigaction(SIGQUIT, &sighandler, &old_sigquit); sigaction(SIGTERM, &sighandler, &old_sigterm); sigaction(SIGINT, &sighandler, &old_sigint); diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c index 94ae3d3..da9f041 100644 --- a/tools/virsh-domain.c +++ b/tools/virsh-domain.c @@ -1582,7 +1582,7 @@ vshPrintJobProgress(const char *label, unsigned long long remaining, fflush(stderr); } -static volatile sig_atomic_t intCaught = 0; +static volatile sig_atomic_t intCaught; static void vshCatchInt(int sig ATTRIBUTE_UNUSED, siginfo_t *siginfo ATTRIBUTE_UNUSED, diff --git a/tools/virsh.c b/tools/virsh.c index 589f3b4..036b517 100644 --- a/tools/virsh.c +++ b/tools/virsh.c @@ -316,7 +316,7 @@ vshReportError(vshControl *ctl) /* * Detection of disconnections and automatic reconnection support */ -static int disconnected = 0; /* we may have been disconnected */ +static int disconnected; /* we may have been disconnected */ /* * vshCatchDisconnect: @@ -2985,7 +2985,7 @@ static char * vshReadlineOptionsGenerator(const char *text, int state) { static int list_index, len; - static const vshCmdDef *cmd = NULL; + static const vshCmdDef *cmd; const char *name; if (!state) { diff --git a/tools/virt-host-validate-common.c b/tools/virt-host-validate-common.c index 496324e..92a19c5 100644 --- a/tools/virt-host-validate-common.c +++ b/tools/virt-host-validate-common.c @@ -1,7 +1,7 @@ /* * virt-host-validate-common.c: Sanity check helper APIs * - * Copyright (C) 2012 Red Hat, Inc. + * Copyright (C) 2012, 2014 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 @@ -66,7 +66,7 @@ void virHostMsgCheck(const char *prefix, static bool virHostMsgWantEscape(void) { static bool detectTty = true; - static bool wantEscape = false; + static bool wantEscape; if (detectTty) { if (isatty(STDOUT_FILENO)) wantEscape = true; -- 1.9.3

C guarantees that static variables are zero-initialized. Some older compilers (and also gcc -fno-zero-initialized-in-bss) create larger binaries if you explicitly zero-initialize a static variable. * tests/eventtest.c: Fix initialization. * tests/testutils.c: Likewise. * tests/virhostdevtest.c: Likewise. * tests/virportallocatortest.c: Likewise. * tests/virscsitest.c: Likewise. Signed-off-by: Eric Blake <eblake@redhat.com> --- tests/eventtest.c | 6 +++--- tests/testutils.c | 12 ++++++------ tests/virhostdevtest.c | 2 +- tests/virportallocatortest.c | 2 +- tests/virscsitest.c | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/eventtest.c b/tests/eventtest.c index 2cfa0c6..87b49d3 100644 --- a/tests/eventtest.c +++ b/tests/eventtest.c @@ -1,7 +1,7 @@ /* * eventtest.c: Test the libvirtd event loop impl * - * Copyright (C) 2009, 2011-2013 Red Hat, Inc. + * Copyright (C) 2009, 2011-2014 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 @@ -116,9 +116,9 @@ testTimer(int timer, void *data) static pthread_mutex_t eventThreadMutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t eventThreadRunCond = PTHREAD_COND_INITIALIZER; -static int eventThreadRunOnce = 0; +static int eventThreadRunOnce; static pthread_cond_t eventThreadJobCond = PTHREAD_COND_INITIALIZER; -static int eventThreadJobDone = 0; +static int eventThreadJobDone; ATTRIBUTE_NORETURN static void *eventThreadLoop(void *data ATTRIBUTE_UNUSED) { diff --git a/tests/testutils.c b/tests/testutils.c index dd65fe8..9d6980f 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -74,20 +74,20 @@ static unsigned int testVerbose = -1; static unsigned int testExpensive = -1; #ifdef TEST_OOM -static unsigned int testOOM = 0; +static unsigned int testOOM; static unsigned int testOOMStart = -1; static unsigned int testOOMEnd = -1; -static unsigned int testOOMTrace = 0; +static unsigned int testOOMTrace; # ifdef TEST_OOM_TRACE void *testAllocStack[30]; int ntestAllocStack; # endif #endif -static bool testOOMActive = false; +static bool testOOMActive; -static size_t testCounter = 0; -static size_t testStart = 0; -static size_t testEnd = 0; +static size_t testCounter; +static size_t testStart; +static size_t testEnd; char *progname; diff --git a/tests/virhostdevtest.c b/tests/virhostdevtest.c index de4cdde..1e93819 100644 --- a/tests/virhostdevtest.c +++ b/tests/virhostdevtest.c @@ -53,7 +53,7 @@ static const unsigned char *uuid = static int nhostdevs = 3; static virDomainHostdevDefPtr hostdevs[] = {NULL, NULL, NULL}; static virPCIDevicePtr dev[] = {NULL, NULL, NULL}; -static virHostdevManagerPtr mgr = NULL; +static virHostdevManagerPtr mgr; static void myCleanup(void) diff --git a/tests/virportallocatortest.c b/tests/virportallocatortest.c index 96d2ade..ef503ce 100644 --- a/tests/virportallocatortest.c +++ b/tests/virportallocatortest.c @@ -36,7 +36,7 @@ # include <netinet/in.h> # include <stdio.h> -static bool host_has_ipv6 = false; +static bool host_has_ipv6; static int (*realsocket)(int domain, int type, int protocol); static void init_syms(void) diff --git a/tests/virscsitest.c b/tests/virscsitest.c index b4ed5e9..a86ca28 100644 --- a/tests/virscsitest.c +++ b/tests/virscsitest.c @@ -34,7 +34,7 @@ VIR_LOG_INIT("tests.scsitest"); static const char *abs_top_srcdir; -static char *virscsi_prefix = NULL; +static char *virscsi_prefix; static int test1(const void *data ATTRIBUTE_UNUSED) -- 1.9.3

C guarantees that static variables are zero-initialized. Some older compilers (and also gcc -fno-zero-initialized-in-bss) create larger binaries if you explicitly zero-initialize a static variable. * src/conf/nwfilter_conf.c: Fix initialization. * src/cpu/cpu_x86.c: Likewise. * src/interface/interface_backend_netcf.c: Likewise. * src/locking/lock_daemon.c: Likewise. * src/locking/lock_driver_lockd.c: Likewise. * src/locking/lock_driver_sanlock.c: Likewise. * src/network/bridge_driver.c: Likewise. * src/node_device/node_device_udev.c: Likewise. * src/nwfilter/nwfilter_learnipaddr.c: Likewise. * src/rpc/virnetserver.c: Likewise. * src/security/security_selinux.c (virSecuritySELinuxGenSecurityLabel): Likewise. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/conf/nwfilter_conf.c | 2 +- src/cpu/cpu_x86.c | 2 +- src/interface/interface_backend_netcf.c | 2 +- src/locking/lock_daemon.c | 2 +- src/locking/lock_driver_lockd.c | 4 ++-- src/locking/lock_driver_sanlock.c | 2 +- src/network/bridge_driver.c | 2 +- src/node_device/node_device_udev.c | 2 +- src/nwfilter/nwfilter_learnipaddr.c | 2 +- src/rpc/virnetserver.c | 4 ++-- src/security/security_selinux.c | 2 +- 11 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c index 0a0265d..4b22709 100644 --- a/src/conf/nwfilter_conf.c +++ b/src/conf/nwfilter_conf.c @@ -144,7 +144,7 @@ static const struct int_map chain_priorities[] = { * only one filter update allowed */ static virRWLock updateLock; -static bool initialized = false; +static bool initialized; void virNWFilterReadLockFilterUpdates(void) diff --git a/src/cpu/cpu_x86.c b/src/cpu/cpu_x86.c index 57f343c..026b54e 100644 --- a/src/cpu/cpu_x86.c +++ b/src/cpu/cpu_x86.c @@ -92,7 +92,7 @@ struct x86_map { struct x86_feature *migrate_blockers; }; -static struct x86_map* virCPUx86Map = NULL; +static struct x86_map* virCPUx86Map; int virCPUx86MapOnceInit(void); VIR_ONCE_GLOBAL_INIT(virCPUx86Map); diff --git a/src/interface/interface_backend_netcf.c b/src/interface/interface_backend_netcf.c index c55a080..a4b2ea9 100644 --- a/src/interface/interface_backend_netcf.c +++ b/src/interface/interface_backend_netcf.c @@ -63,7 +63,7 @@ virNetcfDriverStateOnceInit(void) VIR_ONCE_GLOBAL_INIT(virNetcfDriverState) -static virNetcfDriverStatePtr driverState = NULL; +static virNetcfDriverStatePtr driverState; static void diff --git a/src/locking/lock_daemon.c b/src/locking/lock_daemon.c index fe7cfb8..75244e8 100644 --- a/src/locking/lock_daemon.c +++ b/src/locking/lock_daemon.c @@ -67,7 +67,7 @@ struct _virLockDaemon { virLockDaemonPtr lockDaemon = NULL; -static bool execRestart = false; +static bool execRestart; enum { VIR_LOCK_DAEMON_ERR_NONE = 0, diff --git a/src/locking/lock_driver_lockd.c b/src/locking/lock_driver_lockd.c index 0a40e94..a642122 100644 --- a/src/locking/lock_driver_lockd.c +++ b/src/locking/lock_driver_lockd.c @@ -1,7 +1,7 @@ /* * lock_driver_lockd.c: A lock driver which locks nothing * - * Copyright (C) 2010-2011 Red Hat, Inc. + * Copyright (C) 2010-2011, 2014 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 @@ -79,7 +79,7 @@ struct _virLockManagerLockDaemonDriver { char *scsiLockSpaceDir; }; -static virLockManagerLockDaemonDriverPtr driver = NULL; +static virLockManagerLockDaemonDriverPtr driver; static int virLockManagerLockDaemonLoadConfig(const char *configFile) { diff --git a/src/locking/lock_driver_sanlock.c b/src/locking/lock_driver_sanlock.c index aa6b8fb..0318f25 100644 --- a/src/locking/lock_driver_sanlock.c +++ b/src/locking/lock_driver_sanlock.c @@ -79,7 +79,7 @@ struct _virLockManagerSanlockDriver { gid_t group; }; -static virLockManagerSanlockDriver *driver = NULL; +static virLockManagerSanlockDriver *driver; struct _virLockManagerSanlockPrivate { const char *vm_uri; diff --git a/src/network/bridge_driver.c b/src/network/bridge_driver.c index 5f2e778..10ded33 100644 --- a/src/network/bridge_driver.c +++ b/src/network/bridge_driver.c @@ -126,7 +126,7 @@ static int networkUnplugBandwidth(virNetworkObjPtr net, static void networkNetworkObjTaint(virNetworkObjPtr net, virNetworkTaintFlags taint); -static virNetworkDriverStatePtr driverState = NULL; +static virNetworkDriverStatePtr driverState; static virNetworkObjPtr networkObjFromNetwork(virNetworkPtr net) diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c index 53792f0..f580a9b 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -56,7 +56,7 @@ struct _udevPrivate { bool privileged; }; -static virNodeDeviceDriverStatePtr driverState = NULL; +static virNodeDeviceDriverStatePtr driverState; static int udevStrToLong_ull(char const *s, char **end_ptr, diff --git a/src/nwfilter/nwfilter_learnipaddr.c b/src/nwfilter/nwfilter_learnipaddr.c index 4cea9cf..911079e 100644 --- a/src/nwfilter/nwfilter_learnipaddr.c +++ b/src/nwfilter/nwfilter_learnipaddr.c @@ -134,7 +134,7 @@ struct _virNWFilterIfaceLock { }; -static bool threadsTerminate = false; +static bool threadsTerminate; int diff --git a/src/rpc/virnetserver.c b/src/rpc/virnetserver.c index 762c185..3d3e422 100644 --- a/src/rpc/virnetserver.c +++ b/src/rpc/virnetserver.c @@ -817,8 +817,8 @@ void virNetServerRemoveShutdownInhibition(virNetServerPtr srv) -static sig_atomic_t sigErrors = 0; -static int sigLastErrno = 0; +static sig_atomic_t sigErrors; +static int sigLastErrno; static int sigWrite = -1; static void diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c index 352f1ab..f96be50 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -641,7 +641,7 @@ virSecuritySELinuxGenSecurityLabel(virSecurityManagerPtr mgr, if (!baselabel) { if (def->virtType == VIR_DOMAIN_VIRT_QEMU) { if (data->alt_domain_context == NULL) { - static bool warned = false; + static bool warned; if (!warned) { VIR_WARN("SELinux policy does not define a domain type for QEMU TCG. " "Guest startup may be denied due to missing 'execmem' privilege " -- 1.9.3

C guarantees that static variables are zero-initialized. Some older compilers (and also gcc -fno-zero-initialized-in-bss) create larger binaries if you explicitly zero-initialize a static variable. * src/libxl/libxl_driver.c: Fix initialization. * src/lxc/lxc_controller.c: Likewise. * src/openvz/openvz_util.c (openvzKBPerPages): Likewise. * src/phyp/phyp_driver.c: Likewise. * src/remote/remote_driver.c: Likewise. * src/test/test_driver.c: Likewise. * src/uml/uml_driver.c: Likewise. * src/vbox/vbox_XPCOMCGlue.c: Likewise. * src/vbox/vbox_tmpl.c: Likewise. * src/xen/xen_driver.c: Likewise. * src/xen/xen_hypervisor.c: Likewise. Signed-off-by: Eric Blake <eblake@redhat.com> --- src/libxl/libxl_driver.c | 2 +- src/lxc/lxc_controller.c | 2 +- src/openvz/openvz_util.c | 4 ++-- src/phyp/phyp_driver.c | 8 +++++--- src/remote/remote_driver.c | 2 +- src/test/test_driver.c | 2 +- src/uml/uml_driver.c | 2 +- src/vbox/vbox_XPCOMCGlue.c | 4 ++-- src/vbox/vbox_tmpl.c | 2 +- src/xen/xen_driver.c | 4 ++-- src/xen/xen_hypervisor.c | 4 ++-- 11 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/libxl/libxl_driver.c b/src/libxl/libxl_driver.c index eeebb4f..d2c077c 100644 --- a/src/libxl/libxl_driver.c +++ b/src/libxl/libxl_driver.c @@ -76,7 +76,7 @@ VIR_LOG_INIT("libxl.libxl_driver"); #define XEN_SCHED_CREDIT_NPARAM 2 -static libxlDriverPrivatePtr libxl_driver = NULL; +static libxlDriverPrivatePtr libxl_driver; /* Function declarations */ static int diff --git a/src/lxc/lxc_controller.c b/src/lxc/lxc_controller.c index 1861dd6..1e4b9bc 100644 --- a/src/lxc/lxc_controller.c +++ b/src/lxc/lxc_controller.c @@ -819,7 +819,7 @@ static int lxcControllerClearCapabilities(void) return 0; } -static bool wantReboot = false; +static bool wantReboot; static virMutex lock = VIR_MUTEX_INITIALIZER; diff --git a/src/openvz/openvz_util.c b/src/openvz/openvz_util.c index 4214335..8032f6a 100644 --- a/src/openvz/openvz_util.c +++ b/src/openvz/openvz_util.c @@ -1,7 +1,7 @@ /* * openvz_util.c: core driver methods for managing OpenVZ VEs * - * Copyright (C) 2013 Red Hat, Inc. + * Copyright (C) 2013-2014 Red Hat, Inc. * Copyright (C) 2012 Guido Günther * * This library is free software; you can redistribute it and/or @@ -39,7 +39,7 @@ long openvzKBPerPages(void) { - static long kb_per_pages = 0; + static long kb_per_pages; if (kb_per_pages == 0) { kb_per_pages = sysconf(_SC_PAGESIZE); diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c index a56f25d..09617c8 100644 --- a/src/phyp/phyp_driver.c +++ b/src/phyp/phyp_driver.c @@ -68,9 +68,11 @@ VIR_LOG_INIT("phyp.phyp_driver"); * URI: phyp://user@[hmc|ivm]/managed_system * */ -static unsigned const int HMC = 0; -static unsigned const int PHYP_IFACENAME_SIZE = 24; -static unsigned const int PHYP_MAC_SIZE = 12; +enum { + HMC = 0, + PHYP_IFACENAME_SIZE = 24, + PHYP_MAC_SIZE = 12, +}; static int waitsocket(int socket_fd, LIBSSH2_SESSION * session) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 067f2d0..1ec1e15 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -77,7 +77,7 @@ VIR_LOG_INIT("remote.remote_driver"); deserializeTypedParameters(__FUNCTION__, ret_params_val, ret_params_len, \ limit, params, nparams) -static bool inside_daemon = false; +static bool inside_daemon; struct private_data { virMutex lock; diff --git a/src/test/test_driver.c b/src/test/test_driver.c index f435d03..2afd6fe 100644 --- a/src/test/test_driver.c +++ b/src/test/test_driver.c @@ -471,7 +471,7 @@ static const char *defaultNodeXML = "</device>"; static const unsigned long long defaultPoolCap = (100 * 1024 * 1024 * 1024ull); -static const unsigned long long defaultPoolAlloc = 0; +static const unsigned long long defaultPoolAlloc; static int testStoragePoolObjSetDefaults(virStoragePoolObjPtr pool); static int testNodeGetInfo(virConnectPtr conn, virNodeInfoPtr info); diff --git a/src/uml/uml_driver.c b/src/uml/uml_driver.c index 4f5f6e1..2baf2fa 100644 --- a/src/uml/uml_driver.c +++ b/src/uml/uml_driver.c @@ -147,7 +147,7 @@ static int umlMonitorCommand(const struct uml_driver *driver, const char *cmd, char **reply); -static struct uml_driver *uml_driver = NULL; +static struct uml_driver *uml_driver; static int umlVMFilterRebuild(virDomainObjListIterator iter, void *data) diff --git a/src/vbox/vbox_XPCOMCGlue.c b/src/vbox/vbox_XPCOMCGlue.c index d0c4a15..aaa7c60 100644 --- a/src/vbox/vbox_XPCOMCGlue.c +++ b/src/vbox/vbox_XPCOMCGlue.c @@ -68,9 +68,9 @@ VIR_LOG_INIT("vbox.vbox_XPCOMCGlue"); * Global Variables * *******************************************************************************/ /** The dlopen handle for VBoxXPCOMC. */ -static void *hVBoxXPCOMC = NULL; +static void *hVBoxXPCOMC; /** Pointer to the VBoxXPCOMC function table. */ -static PCVBOXXPCOM pVBoxFuncs_v2_2 = NULL; +static PCVBOXXPCOM pVBoxFuncs_v2_2; /** Pointer to VBoxGetXPCOMCFunctions for the loaded VBoxXPCOMC so/dylib/dll. */ PFNVBOXGETXPCOMCFUNCTIONS g_pfnGetFunctions = NULL; diff --git a/src/vbox/vbox_tmpl.c b/src/vbox/vbox_tmpl.c index db98c90..24c8a50 100644 --- a/src/vbox/vbox_tmpl.c +++ b/src/vbox/vbox_tmpl.c @@ -235,7 +235,7 @@ if (strUtf16) {\ * them that way */ -static vboxGlobalData *g_pVBoxGlobalData = NULL; +static vboxGlobalData *g_pVBoxGlobalData; #endif /* !(VBOX_API_VERSION == 2002000) */ diff --git a/src/xen/xen_driver.c b/src/xen/xen_driver.c index d042050..5f7c98f 100644 --- a/src/xen/xen_driver.c +++ b/src/xen/xen_driver.c @@ -91,8 +91,8 @@ xenUnifiedDomainGetVcpusInternal(virDomainPtr dom, int maplen); -static bool is_privileged = false; -static virSysinfoDefPtr hostsysinfo = NULL; +static bool is_privileged; +static virSysinfoDefPtr hostsysinfo; static virDomainDefPtr xenGetDomainDefForID(virConnectPtr conn, int id) { diff --git a/src/xen/xen_hypervisor.c b/src/xen/xen_hypervisor.c index d3d4aea..4be8891 100644 --- a/src/xen/xen_hypervisor.c +++ b/src/xen/xen_hypervisor.c @@ -126,7 +126,7 @@ typedef privcmd_hypercall_t hypercall_t; # define SYS_IFACE_MIN_VERS_NUMA 4 #endif -static int xen_ioctl_hypercall_cmd = 0; +static int xen_ioctl_hypercall_cmd; static struct xenHypervisorVersions hv_versions = { .hv = 0, .hypervisor = 2, @@ -134,7 +134,7 @@ static struct xenHypervisorVersions hv_versions = { .dom_interface = -1, }; -static int kb_per_pages = 0; +static int kb_per_pages; /* Regular expressions used by xenHypervisorGetCapabilities, and * compiled once by xenHypervisorInit. Note that these are POSIX.2 -- 1.9.3

Now that all offenders have been cleaned, turn on a syntax-check rule to prevent future offenders. * cfg.mk (sc_prohibit_static_zero_init): New rule. * src/qemu/qemu_driver.c (qemuDomainBlockJobImpl): Avoid false positive. Signed-off-by: Eric Blake <eblake@redhat.com> --- cfg.mk | 8 ++++++++ src/qemu/qemu_driver.c | 5 +++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/cfg.mk b/cfg.mk index 682687a..927240b 100644 --- a/cfg.mk +++ b/cfg.mk @@ -962,6 +962,14 @@ sc_prohibit_paren_brace: halt='Put space between closing parenthesis and opening brace' \ $(_sc_search_regexp) +# C guarantees that static variables are zero initialized, and some compilers +# waste space by sticking explicit initializers in .data instead of .bss +sc_prohibit_static_zero_init: + @prohibit='\bstatic\b.*= *(0[^xX0-9]|NULL|false)' \ + in_vc_files='\.[chx](\.in)?$$' \ + halt='static variables do not need explicit zero initialization'\ + $(_sc_search_regexp) + # FreeBSD exports the "devname" symbol which produces a warning. sc_prohibit_devname: @prohibit='\bdevname\b' \ diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 5eccacc..2606dd0 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -15578,8 +15578,9 @@ qemuDomainBlockJobImpl(virDomainObjPtr vm, * that back into the return status of this API call. */ while (1) { /* Poll every 50ms */ - static struct timespec ts = { .tv_sec = 0, - .tv_nsec = 50 * 1000 * 1000ull }; + static struct timespec ts = { + .tv_sec = 0, + .tv_nsec = 50 * 1000 * 1000ull }; virDomainBlockJobInfo dummy; qemuDomainObjEnterMonitor(driver, vm); -- 1.9.3

On 28.10.2014 21:34, Eric Blake wrote:
Based on a comment I just made while reviewing Dan's patches, we might as well adopt a consistent style of relying on the C guarantee that static variables are automatically zero-initialized without any effort on our part.
Eric Blake (7): audit: use bool for audit log choice maint: avoid static zero init in core files maint: avoid static zero init in tools maint: avoid static zero init in tests maint: avoid static zero init in helpers maint: avoid static zero init in drivers maint: add syntax check to prohibit static zero init
cfg.mk | 8 ++++++++ daemon/libvirtd.c | 2 +- src/conf/nwfilter_conf.c | 2 +- src/cpu/cpu_x86.c | 2 +- src/interface/interface_backend_netcf.c | 2 +- src/libvirt.c | 18 +++++++++--------- src/libxl/libxl_driver.c | 2 +- src/locking/lock_daemon.c | 2 +- src/locking/lock_driver_lockd.c | 4 ++-- src/locking/lock_driver_sanlock.c | 2 +- src/lxc/lxc_controller.c | 2 +- src/network/bridge_driver.c | 2 +- src/node_device/node_device_udev.c | 2 +- src/nwfilter/nwfilter_learnipaddr.c | 2 +- src/openvz/openvz_util.c | 4 ++-- src/phyp/phyp_driver.c | 8 +++++--- src/qemu/qemu_driver.c | 5 +++-- src/remote/remote_driver.c | 2 +- src/rpc/virnetserver.c | 4 ++-- src/security/security_selinux.c | 2 +- src/test/test_driver.c | 2 +- src/uml/uml_driver.c | 2 +- src/util/viralloc.c | 10 +++++----- src/util/viraudit.c | 4 ++-- src/util/viraudit.h | 4 ++-- src/util/virdbus.c | 4 ++-- src/util/virevent.c | 14 +++++++------- src/util/virfile.c | 2 +- src/util/virlog.c | 16 ++++++++-------- src/util/virnetlink.c | 2 +- src/util/virprocess.c | 3 +-- src/util/virthread.h | 4 ++-- src/vbox/vbox_XPCOMCGlue.c | 4 ++-- src/vbox/vbox_tmpl.c | 2 +- src/xen/xen_driver.c | 4 ++-- src/xen/xen_hypervisor.c | 4 ++-- tests/eventtest.c | 6 +++--- tests/testutils.c | 12 ++++++------ tests/virhostdevtest.c | 2 +- tests/virportallocatortest.c | 2 +- tests/virscsitest.c | 2 +- tools/virsh-console.c | 5 +---- tools/virsh-domain.c | 2 +- tools/virsh.c | 4 ++-- tools/virt-host-validate-common.c | 4 ++-- 45 files changed, 102 insertions(+), 95 deletions(-)
ACK Michal

On 10/28/2014 03:55 PM, Michal Privoznik wrote:
On 28.10.2014 21:34, Eric Blake wrote:
Based on a comment I just made while reviewing Dan's patches, we might as well adopt a consistent style of relying on the C guarantee that static variables are automatically zero-initialized without any effort on our part.
ACK
No semantic change, and the series was posted prior to freeze, so I went ahead and pushed it now rather than waiting for post-release. -- Eric Blake eblake redhat com +1-919-301-3266 Libvirt virtualization library http://libvirt.org
participants (2)
-
Eric Blake
-
Michal Privoznik