On 8/3/23 12:36, Michal Privoznik wrote: > There are some cases left after previous commit which were not > picked up by coccinelle. Mostly, becuase the spatch was not > generic enough. We are left with cases like: two variables > declared on one line, a variable declared in #ifdef-s (there are > notoriously difficult for coccinelle), arrays, macro definitions, > etc. > > Finish what coccinelle started, by hand. > > Signed-off-by: Michal Privoznik <mprivozn@xxxxxxxxxx> > --- > src/libxl/libxl_capabilities.c | 4 +--- > src/qemu/qemu_monitor.c | 4 +--- > src/rpc/virnettlscontext.c | 3 +-- > src/util/virnetlink.c | 4 +--- > src/util/virutil.c | 4 ++-- > src/vmx/vmx.c | 4 +--- > tests/libxlmock.c | 4 +--- > tests/qemumonitorjsontest.c | 5 ++--- > tests/sockettest.c | 6 ++---- > tools/nss/libvirt_nss.c | 7 +++---- > tools/virsh-domain.c | 11 ++++------- > 11 files changed, 19 insertions(+), 37 deletions(-) > > diff --git a/src/libxl/libxl_capabilities.c b/src/libxl/libxl_capabilities.c > index 5ee6fe3f67..177e8b988e 100644 > --- a/src/libxl/libxl_capabilities.c > +++ b/src/libxl/libxl_capabilities.c > @@ -328,11 +328,9 @@ libxlCapsInitGuests(libxl_ctx *ctx, virCaps *caps) > char *saveptr = NULL; > size_t i; > > - struct guest_arch guest_archs[32]; > + struct guest_arch guest_archs[32] = { 0 }; > int nr_guest_archs = 0; > > - memset(guest_archs, 0, sizeof(guest_archs)); > - > if ((ver_info = libxl_get_version_info(ctx)) == NULL) { > virReportError(VIR_ERR_INTERNAL_ERROR, "%s", > _("Failed to get version info from libxenlight")); > diff --git a/src/qemu/qemu_monitor.c b/src/qemu/qemu_monitor.c > index db34b6c179..02da1d6dfc 100644 > --- a/src/qemu/qemu_monitor.c > +++ b/src/qemu/qemu_monitor.c > @@ -311,11 +311,9 @@ qemuMonitorIOWriteWithFD(qemuMonitor *mon, > struct msghdr msg = { 0 }; > struct iovec iov[1]; > int ret; > - char control[CMSG_SPACE(sizeof(int))]; > + char control[CMSG_SPACE(sizeof(int))] = { 0 }; > struct cmsghdr *cmsg; > > - memset(control, 0, sizeof(control)); > - > iov[0].iov_base = (void *)data; > iov[0].iov_len = len; > > diff --git a/src/rpc/virnettlscontext.c b/src/rpc/virnettlscontext.c > index f72597ee80..1939fe5d6e 100644 > --- a/src/rpc/virnettlscontext.c > +++ b/src/rpc/virnettlscontext.c > @@ -545,12 +545,11 @@ static int virNetTLSContextSanityCheckCredentials(bool isServer, > const char *certFile) > { > gnutls_x509_crt_t cert = NULL; > - gnutls_x509_crt_t cacerts[MAX_CERTS]; > + gnutls_x509_crt_t cacerts[MAX_CERTS] = { 0 }; > size_t ncacerts = 0; > size_t i; > int ret = -1; > > - memset(cacerts, 0, sizeof(cacerts)); > if ((access(certFile, R_OK) == 0) && > !(cert = virNetTLSContextLoadCertFromFile(certFile, isServer))) > goto cleanup; > diff --git a/src/util/virnetlink.c b/src/util/virnetlink.c > index d964c439b4..2d052cd03e 100644 > --- a/src/util/virnetlink.c > +++ b/src/util/virnetlink.c > @@ -250,7 +250,7 @@ virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t src_pid, > int fd; > int n; > virNetlinkHandle *nlhandle = NULL; > - struct pollfd fds[1]; > + struct pollfd fds[1] = { 0 }; > struct nlmsghdr *nlmsg = nlmsg_hdr(nl_msg); > > if (protocol >= MAX_LINKS) { > @@ -286,8 +286,6 @@ virNetlinkSendRequest(struct nl_msg *nl_msg, uint32_t src_pid, > goto error; > } > > - memset(fds, 0, sizeof(fds)); > - > fds[0].fd = fd; > fds[0].events = POLLIN; > > diff --git a/src/util/virutil.c b/src/util/virutil.c > index 40ca205595..b5b65fb415 100644 > --- a/src/util/virutil.c > +++ b/src/util/virutil.c > @@ -428,7 +428,8 @@ virGetHostnameImpl(bool quiet) > { > int r; > char hostname[HOST_NAME_MAX+1], *result = NULL; > - struct addrinfo hints, *info; > + struct addrinfo hints = { 0 }; > + struct addrinfo *info; > > r = gethostname(hostname, sizeof(hostname)); > if (r == -1) { > @@ -453,7 +454,6 @@ virGetHostnameImpl(bool quiet) > * canonicalize the hostname by running it through getaddrinfo > */ > > - memset(&hints, 0, sizeof(hints)); > hints.ai_flags = AI_CANONNAME|AI_CANONIDN; > hints.ai_family = AF_UNSPEC; > r = getaddrinfo(hostname, NULL, &hints, &info); > diff --git a/src/vmx/vmx.c b/src/vmx/vmx.c > index fe4f253e9e..5c6925be22 100644 > --- a/src/vmx/vmx.c > +++ b/src/vmx/vmx.c > @@ -3229,7 +3229,7 @@ virVMXFormatConfig(virVMXContext *ctx, virDomainXMLOption *xmlopt, virDomainDef > char *vmx = NULL; > size_t i; > int sched_cpu_affinity_length; > - unsigned char zero[VIR_UUID_BUFLEN]; > + unsigned char zero[VIR_UUID_BUFLEN] = { 0 }; > g_auto(virBuffer) buffer = VIR_BUFFER_INITIALIZER; > char *preliminaryDisplayName = NULL; > char *displayName = NULL; > @@ -3247,8 +3247,6 @@ virVMXFormatConfig(virVMXContext *ctx, virDomainXMLOption *xmlopt, virDomainDef > return NULL; > } > > - memset(zero, 0, VIR_UUID_BUFLEN); > - > if (def->virtType != VIR_DOMAIN_VIRT_VMWARE) { > virReportError(VIR_ERR_INTERNAL_ERROR, > _("Expecting virt type to be '%1$s' but found '%2$s'"), > diff --git a/tests/libxlmock.c b/tests/libxlmock.c > index 205d34df19..f564a0ef72 100644 > --- a/tests/libxlmock.c > +++ b/tests/libxlmock.c > @@ -60,9 +60,7 @@ VIR_MOCK_IMPL_RET_ARGS(libxl_get_version_info, > const libxl_version_info*, > libxl_ctx *, ctx) > { > - static libxl_version_info info; > - > - memset(&info, 0, sizeof(info)); > + static libxl_version_info info = { 0 }; > > /* silence gcc warning about unused function */ > if (0) > diff --git a/tests/qemumonitorjsontest.c b/tests/qemumonitorjsontest.c > index a1740d3f45..2e7b661db4 100644 > --- a/tests/qemumonitorjsontest.c > +++ b/tests/qemumonitorjsontest.c > @@ -1591,15 +1591,14 @@ testQemuMonitorJSONqemuMonitorJSONGetMigrationStats(const void *opaque) > { > const testGenericData *data = opaque; > virDomainXMLOption *xmlopt = data->xmlopt; > - qemuMonitorMigrationStats stats, expectedStats; > + qemuMonitorMigrationStats stats; > + qemuMonitorMigrationStats expectedStats = { 0 }; > g_autofree char *error = NULL; > g_autoptr(qemuMonitorTest) test = NULL; > > if (!(test = qemuMonitorTestNewSchema(xmlopt, data->schema))) > return -1; > > - memset(&expectedStats, 0, sizeof(expectedStats)); > - > expectedStats.status = QEMU_MONITOR_MIGRATION_STATUS_ACTIVE; > expectedStats.total_time = 47; > expectedStats.ram_total = 1611038720; > diff --git a/tests/sockettest.c b/tests/sockettest.c > index 6b9063e11d..5cb8a9fb72 100644 > --- a/tests/sockettest.c > +++ b/tests/sockettest.c > @@ -269,10 +269,9 @@ mymain(void) > > #define DO_TEST_PARSE_AND_FORMAT(addrstr, family, pass) \ > do { \ > - virSocketAddr addr; \ > + virSocketAddr addr = { 0 }; \ > struct testParseData data = { &addr, addrstr, family, pass }; \ > struct testFormatData data2 = { &addr, addrstr, pass }; \ > - memset(&addr, 0, sizeof(addr)); \ > if (virTestRun("Test parse " addrstr " family " #family, \ > testParseHelper, &data) < 0) \ > ret = -1; \ > @@ -283,10 +282,9 @@ mymain(void) > > #define DO_TEST_PARSE_AND_CHECK_FORMAT(addrstr, addrformated, family, pass) \ > do { \ > - virSocketAddr addr; \ > + virSocketAddr addr = { 0 }; \ > struct testParseData data = { &addr, addrstr, family, true}; \ > struct testFormatData data2 = { &addr, addrformated, pass }; \ > - memset(&addr, 0, sizeof(addr)); \ > if (virTestRun("Test parse " addrstr " family " #family, \ > testParseHelper, &data) < 0) \ > ret = -1; \ > diff --git a/tools/nss/libvirt_nss.c b/tools/nss/libvirt_nss.c > index ec14ac804b..5c4796e8bc 100644 > --- a/tools/nss/libvirt_nss.c > +++ b/tools/nss/libvirt_nss.c > @@ -538,15 +538,14 @@ _nss_compat_getaddrinfo(void *retval, > void *mdata __attribute__((unused)), > va_list ap) > { > - struct addrinfo sentinel, *cur, *ai; > + struct addrinfo sentinel = { 0 }; > + struct addrinfo *cur = & sentinel; &sentinel; (without the space) With this fixed, for this patch: Reviewed-by: Claudio Fontana <cfontana@xxxxxxx> > + struct addrinfo *ai; > const char *name; > > name = va_arg(ap, char *); > ai = va_arg(ap, struct addrinfo *); > > - memset(&sentinel, 0, sizeof(sentinel)); > - cur = &sentinel; > - > if ((ai->ai_family == AF_UNSPEC) || (ai->ai_family == AF_INET6)) > aiforaf(name, AF_INET6, ai, &cur); > if ((ai->ai_family == AF_UNSPEC) || (ai->ai_family == AF_INET)) > diff --git a/tools/virsh-domain.c b/tools/virsh-domain.c > index 7d76055eda..f8758f18a3 100644 > --- a/tools/virsh-domain.c > +++ b/tools/virsh-domain.c > @@ -942,7 +942,8 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) > *inboundStr = NULL, *outboundStr = NULL, *alias = NULL; > const char *sourceModeStr = NULL; > int sourceMode = -1; > - virNetDevBandwidthRate inbound, outbound; > + virNetDevBandwidthRate inbound = { 0 }; > + virNetDevBandwidthRate outbound = { 0 }; > virDomainNetType typ; > int ret; > g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER; > @@ -990,7 +991,6 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) > } > > if (inboundStr) { > - memset(&inbound, 0, sizeof(inbound)); > if (virshParseRateStr(ctl, inboundStr, &inbound) < 0) > return false; > if (!inbound.average && !inbound.floor) { > @@ -999,7 +999,6 @@ cmdAttachInterface(vshControl *ctl, const vshCmd *cmd) > } > } > if (outboundStr) { > - memset(&outbound, 0, sizeof(outbound)); > if (virshParseRateStr(ctl, outboundStr, &outbound) < 0) > return false; > if (outbound.average == 0) { > @@ -3286,7 +3285,8 @@ cmdDomIftune(vshControl *ctl, const vshCmd *cmd) > bool current = vshCommandOptBool(cmd, "current"); > bool config = vshCommandOptBool(cmd, "config"); > bool live = vshCommandOptBool(cmd, "live"); > - virNetDevBandwidthRate inbound, outbound; > + virNetDevBandwidthRate inbound = { 0 }; > + virNetDevBandwidthRate outbound = { 0 }; > size_t i; > > VSH_EXCLUSIVE_OPTIONS_VAR(current, live); > @@ -3307,9 +3307,6 @@ cmdDomIftune(vshControl *ctl, const vshCmd *cmd) > vshCommandOptStringReq(ctl, cmd, "outbound", &outboundStr) < 0) > goto cleanup; > > - memset(&inbound, 0, sizeof(inbound)); > - memset(&outbound, 0, sizeof(outbound)); > - > if (inboundStr) { > if (virshParseRateStr(ctl, inboundStr, &inbound) < 0) > goto cleanup;