From nobody Tue Feb 10 03:49:04 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=gmail.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1530481485236361.34595530413526; Sun, 1 Jul 2018 14:44:45 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id B8FEF3680F; Sun, 1 Jul 2018 21:44:42 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7029C4B7; Sun, 1 Jul 2018 21:44:42 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 15EBE18037F9; Sun, 1 Jul 2018 21:44:42 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.25]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w61Lid18009155 for ; Sun, 1 Jul 2018 17:44:39 -0400 Received: by smtp.corp.redhat.com (Postfix) id 79EBD20155E5; Sun, 1 Jul 2018 21:44:39 +0000 (UTC) Received: from mx1.redhat.com (ext-mx19.extmail.prod.ext.phx2.redhat.com [10.5.110.48]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7093720155E4 for ; Sun, 1 Jul 2018 21:44:39 +0000 (UTC) Received: from mail-pg0-f66.google.com (mail-pg0-f66.google.com [74.125.83.66]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id CB277307D847 for ; Sun, 1 Jul 2018 21:44:27 +0000 (UTC) Received: by mail-pg0-f66.google.com with SMTP id z1-v6so6200313pgv.12 for ; Sun, 01 Jul 2018 14:44:27 -0700 (PDT) Received: from centos.localdomain ([157.37.164.55]) by smtp.gmail.com with ESMTPSA id v68-v6sm15006933pfv.143.2018.07.01.14.44.23 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Jul 2018 14:44:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:in-reply-to:references; bh=zZ57GmBoEfhxDgjPC9x4xpCfzSRTKV/B0f+4wugy59Y=; b=Bp6AozNiLkfw1wtozOExIZq5vkxRR1WPL3bjEM8P+i/WkrNWeU/NnkXHjF5V7O36Py /Cdn5acimlYhaAJn5Sre8x+XoitS+YLC5hKWoJvgYac0VT3q6UU/lmYHis/+bZeo5WHZ +jPTKaZ8t6xE7wYWwHiXs02LAdr2rgtwvTgRXziLNYMmmcAv68gydFxhfAKfY1292IaU dTu/5C0lqejGLlQPmNOQJjzuzzyfyD/StY34t3P3lc62FmnECpts07VUYLK0y4//wGc8 gvf/IGjl1EvyEVXnDicIJoituwT7EbB2Ul536C6PdqQIbcITXO9kUMkVklgItF26BjqA jr4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references; bh=zZ57GmBoEfhxDgjPC9x4xpCfzSRTKV/B0f+4wugy59Y=; b=nEKkx1yud1JOnEYSEw/e2kJxjIfQdx3N/4AjhwmY1YxLw+RHgXQB9JYXhr/F5MMGoW FsvUkbOwKQIWu/Wpe8eOs4VG4bShvo5Y/iaEup6Y2VyFDNMWdG0NuBFGGMvh+yPzQI0X 6RFP3Pufpdf06+UwZwgEhr95WUCqRFQA5BoHYs3ukXWYTIeEVPWxum03gkx+zBbUO7DO l3W7CjyC8udi8rVWH7f/JS8oZrSCG1Fiq/iTL7Q1wix7o7X5EOJqRX1+wWa2VGZBO/Q0 v/dK3ziEBfOyXvKCxpGALXH2cdOtdDo9SoWqBhT2723cYWPoV2kS9L+RjRrUrKLE+pcr d6TA== X-Gm-Message-State: APt69E35c8B4b6PtQJhHYKbT884oWcV4elb/VmuFBVbgE+JtuS0Z7aaB 4z7g8/fBxKLLU8cJmEiJjpKT9Wrv X-Google-Smtp-Source: ADUXVKLi6z8JkwhGsVGVk8n0bBStn02oJKxsJlaZiJnFQsq3SZ64mm138xVobqKh0Qhq2xQkjHOzlg== X-Received: by 2002:a65:418b:: with SMTP id a11-v6mr19544612pgq.118.1530481466869; Sun, 01 Jul 2018 14:44:26 -0700 (PDT) From: Sukrit Bhatnagar To: libvir-list@redhat.com Date: Sat, 30 Jun 2018 14:30:18 +0530 Message-Id: <1530349239-22049-15-git-send-email-skrtbhtngr@gmail.com> In-Reply-To: <1530349239-22049-1-git-send-email-skrtbhtngr@gmail.com> References: <1530349239-22049-1-git-send-email-skrtbhtngr@gmail.com> X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Sun, 01 Jul 2018 21:44:27 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.48]); Sun, 01 Jul 2018 21:44:27 +0000 (UTC) for IP:'74.125.83.66' DOMAIN:'mail-pg0-f66.google.com' HELO:'mail-pg0-f66.google.com' FROM:'skrtbhtngr@gmail.com' RCPT:'' X-RedHat-Spam-Score: 1.766 * (DATE_IN_PAST_24_48, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, NML_ADSP_CUSTOM_MED, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_PASS, T_DKIM_INVALID) 74.125.83.66 mail-pg0-f66.google.com 74.125.83.66 mail-pg0-f66.google.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.48 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.25 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH v3 14/35] util: file: use VIR_AUTOFREE instead of VIR_FREE for scalar types X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Sun, 01 Jul 2018 21:44:44 +0000 (UTC) X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZohoMail: RDKM_2 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" By making use of GNU C's cleanup attribute handled by the VIR_AUTOFREE macro for declaring scalar variables, majority of the VIR_FREE calls can be dropped, which in turn leads to getting rid of most of our cleanup sections. Signed-off-by: Sukrit Bhatnagar Reviewed-by: Erik Skultety (patches 12-14) --- src/util/virfile.c | 310 ++++++++++++++++++-------------------------------= ---- 1 file changed, 102 insertions(+), 208 deletions(-) diff --git a/src/util/virfile.c b/src/util/virfile.c index 2690e2d..3a7445f 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -211,7 +211,7 @@ virFileWrapperFdNew(int *fd, const char *name, unsigned= int flags) bool output =3D false; int pipefd[2] =3D { -1, -1 }; int mode =3D -1; - char *iohelper_path =3D NULL; + VIR_AUTOFREE(char *) iohelper_path =3D NULL; =20 if (!flags) { virReportError(VIR_ERR_INTERNAL_ERROR, "%s", @@ -262,8 +262,6 @@ virFileWrapperFdNew(int *fd, const char *name, unsigned= int flags) =20 ret->cmd =3D virCommandNewArgList(iohelper_path, name, NULL); =20 - VIR_FREE(iohelper_path); - if (output) { virCommandSetInputFD(ret->cmd, pipefd[0]); virCommandSetOutputFD(ret->cmd, fd); @@ -294,7 +292,6 @@ virFileWrapperFdNew(int *fd, const char *name, unsigned= int flags) return ret; =20 error: - VIR_FREE(iohelper_path); VIR_FORCE_CLOSE(pipefd[0]); VIR_FORCE_CLOSE(pipefd[1]); virFileWrapperFdFree(ret); @@ -492,7 +489,7 @@ virFileRewrite(const char *path, virFileRewriteFunc rewrite, const void *opaque) { - char *newfile =3D NULL; + VIR_AUTOFREE(char *) newfile =3D NULL; int fd =3D -1; int ret =3D -1; =20 @@ -533,10 +530,8 @@ virFileRewrite(const char *path, =20 cleanup: VIR_FORCE_CLOSE(fd); - if (newfile) { + if (newfile) unlink(newfile); - VIR_FREE(newfile); - } return ret; } =20 @@ -763,7 +758,7 @@ int virFileLoopDeviceAssociate(const char *file, int lofd =3D -1; int fsfd =3D -1; struct loop_info64 lo; - char *loname =3D NULL; + VIR_AUTOFREE(char *) loname =3D NULL; int ret =3D -1; =20 if ((lofd =3D virFileLoopDeviceOpen(&loname)) < 0) @@ -801,7 +796,6 @@ int virFileLoopDeviceAssociate(const char *file, ret =3D 0; =20 cleanup: - VIR_FREE(loname); VIR_FORCE_CLOSE(fsfd); if (ret =3D=3D -1) VIR_FORCE_CLOSE(lofd); @@ -816,8 +810,7 @@ int virFileLoopDeviceAssociate(const char *file, static int virFileNBDDeviceIsBusy(const char *dev_name) { - char *path; - int ret =3D -1; + VIR_AUTOFREE(char *) path =3D NULL; =20 if (virAsprintf(&path, SYSFS_BLOCK_DIR "/%s/pid", dev_name) < 0) @@ -825,18 +818,14 @@ virFileNBDDeviceIsBusy(const char *dev_name) =20 if (!virFileExists(path)) { if (errno =3D=3D ENOENT) - ret =3D 0; + return 0; else virReportSystemError(errno, _("Cannot check NBD device %s pid"), dev_name); - goto cleanup; + return -1; } - ret =3D 1; - - cleanup: - VIR_FREE(path); - return ret; + return 1; } =20 =20 @@ -881,15 +870,13 @@ virFileNBDLoadDriver(void) "administratively prohibited")); return false; } else { - char *errbuf =3D NULL; + VIR_AUTOFREE(char *) errbuf =3D NULL; =20 if ((errbuf =3D virKModLoad(NBD_DRIVER, true))) { - VIR_FREE(errbuf); virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("Failed to load nbd module")); return false; } - VIR_FREE(errbuf); } return true; } @@ -899,8 +886,8 @@ int virFileNBDDeviceAssociate(const char *file, bool readonly, char **dev) { - char *nbddev =3D NULL; - char *qemunbd =3D NULL; + VIR_AUTOFREE(char *) nbddev =3D NULL; + VIR_AUTOFREE(char *) qemunbd =3D NULL; virCommandPtr cmd =3D NULL; int ret =3D -1; const char *fmtstr =3D NULL; @@ -948,8 +935,6 @@ int virFileNBDDeviceAssociate(const char *file, ret =3D 0; =20 cleanup: - VIR_FREE(nbddev); - VIR_FREE(qemunbd); virCommandFree(cmd); return ret; } @@ -996,7 +981,6 @@ int virFileDeleteTree(const char *dir) { DIR *dh; struct dirent *de; - char *filepath =3D NULL; int ret =3D -1; int direrr; =20 @@ -1008,6 +992,7 @@ int virFileDeleteTree(const char *dir) return -1; =20 while ((direrr =3D virDirRead(dh, &de, dir)) > 0) { + VIR_AUTOFREE(char *) filepath =3D NULL; struct stat sb; =20 if (virAsprintf(&filepath, "%s/%s", @@ -1031,8 +1016,6 @@ int virFileDeleteTree(const char *dir) goto cleanup; } } - - VIR_FREE(filepath); } if (direrr < 0) goto cleanup; @@ -1047,7 +1030,6 @@ int virFileDeleteTree(const char *dir) ret =3D 0; =20 cleanup: - VIR_FREE(filepath); VIR_DIR_CLOSE(dh); return ret; } @@ -1205,7 +1187,7 @@ static int safezero_slow(int fd, off_t offset, off_t len) { int r; - char *buf; + VIR_AUTOFREE(char *) buf =3D NULL; unsigned long long remain, bytes; =20 if (lseek(fd, offset, SEEK_SET) < 0) @@ -1226,15 +1208,12 @@ safezero_slow(int fd, off_t offset, off_t len) bytes =3D remain; =20 r =3D safewrite(fd, buf, bytes); - if (r < 0) { - VIR_FREE(buf); + if (r < 0) return -1; - } =20 /* safewrite() guarantees all data will be written */ remain -=3D bytes; } - VIR_FREE(buf); return 0; } =20 @@ -1597,8 +1576,7 @@ virFileRelLinkPointsTo(const char *directory, const char *checkLink, const char *checkDest) { - char *candidate; - int ret; + VIR_AUTOFREE(char *) candidate =3D NULL; =20 if (*checkLink =3D=3D '/') return virFileLinkPointsTo(checkLink, checkDest); @@ -1610,9 +1588,7 @@ virFileRelLinkPointsTo(const char *directory, } if (virAsprintf(&candidate, "%s/%s", directory, checkLink) < 0) return -1; - ret =3D virFileLinkPointsTo(candidate, checkDest); - VIR_FREE(candidate); - return ret; + return virFileLinkPointsTo(candidate, checkDest); } =20 =20 @@ -1945,44 +1921,40 @@ virFileIsExecutable(const char *file) */ int virFileIsMountPoint(const char *file) { - char *parent =3D NULL; - int ret =3D -1; + VIR_AUTOFREE(char *) parent =3D NULL; + int ret; struct stat sb1, sb2; =20 if (!(parent =3D mdir_name(file))) { virReportOOMError(); - goto cleanup; + return -1; } =20 VIR_DEBUG("Comparing '%s' to '%s'", file, parent); =20 if (stat(file, &sb1) < 0) { if (errno =3D=3D ENOENT) - ret =3D 0; + return 0; else virReportSystemError(errno, _("Cannot stat '%s'"), file); - goto cleanup; + return -1; } =20 if (stat(parent, &sb2) < 0) { virReportSystemError(errno, _("Cannot stat '%s'"), parent); - goto cleanup; + return -1; } =20 - if (!S_ISDIR(sb1.st_mode)) { - ret =3D 0; - goto cleanup; - } + if (!S_ISDIR(sb1.st_mode)) + return 0; =20 ret =3D sb1.st_dev !=3D sb2.st_dev; VIR_DEBUG("Is mount %d", ret); =20 - cleanup: - VIR_FREE(parent); return ret; } =20 @@ -2165,7 +2137,7 @@ virFileAccessibleAs(const char *path, int mode, { pid_t pid =3D 0; int status, ret =3D 0; - gid_t *groups; + VIR_AUTOFREE(gid_t *) groups =3D NULL; int ngroups; =20 if (uid =3D=3D geteuid() && @@ -2178,13 +2150,10 @@ virFileAccessibleAs(const char *path, int mode, =20 pid =3D virFork(); =20 - if (pid < 0) { - VIR_FREE(groups); + if (pid < 0) return -1; - } =20 if (pid) { /* parent */ - VIR_FREE(groups); if (virProcessWait(pid, &status, false) < 0) { /* virProcessWait() already reported error */ errno =3D EINTR; @@ -2280,7 +2249,7 @@ virFileOpenForked(const char *path, int openflags, mo= de_t mode, int recvfd_errno =3D 0; int fd =3D -1; int pair[2] =3D { -1, -1 }; - gid_t *groups; + VIR_AUTOFREE(gid_t *) groups =3D NULL; int ngroups; bool created =3D false; =20 @@ -2298,16 +2267,12 @@ virFileOpenForked(const char *path, int openflags, = mode_t mode, virReportSystemError(errno, _("failed to create socket needed for '%s'"), path); - VIR_FREE(groups); return ret; } =20 pid =3D virFork(); - if (pid < 0) { - ret =3D -errno; - VIR_FREE(groups); - return ret; - } + if (pid < 0) + return -errno; =20 if (pid =3D=3D 0) { =20 @@ -2372,7 +2337,6 @@ virFileOpenForked(const char *path, int openflags, mo= de_t mode, =20 /* parent */ =20 - VIR_FREE(groups); VIR_FORCE_CLOSE(pair[1]); =20 do { @@ -2573,7 +2537,7 @@ virFileRemove(const char *path, { pid_t pid; int status =3D 0, ret =3D 0; - gid_t *groups; + VIR_AUTOFREE(gid_t *) groups =3D NULL; int ngroups; =20 if (!virFileRemoveNeedsSetuid(path, uid, gid)) { @@ -2598,15 +2562,11 @@ virFileRemove(const char *path, =20 pid =3D virFork(); =20 - if (pid < 0) { - ret =3D -errno; - VIR_FREE(groups); - return ret; - } + if (pid < 0) + return -errno; =20 if (pid) { /* parent */ /* wait for child to complete, and retrieve its exit code */ - VIR_FREE(groups); =20 if (virProcessWait(pid, &status, 0) < 0) { /* virProcessWait() reports errno on waitpid failure, so we'll= just @@ -2738,7 +2698,7 @@ virDirCreate(const char *path, struct stat st; pid_t pid; int status =3D 0, ret =3D 0; - gid_t *groups; + VIR_AUTOFREE(gid_t *) groups =3D NULL; int ngroups; bool created =3D false; =20 @@ -2774,15 +2734,11 @@ virDirCreate(const char *path, =20 pid =3D virFork(); =20 - if (pid < 0) { - ret =3D -errno; - VIR_FREE(groups); - return ret; - } + if (pid < 0) + return -errno; =20 if (pid) { /* parent */ /* wait for child to complete, and retrieve its exit code */ - VIR_FREE(groups); =20 if (virProcessWait(pid, &status, 0) < 0) { /* virProcessWait() reports errno on waitpid failure, so we'll= just @@ -3047,13 +3003,13 @@ int virFileChownFiles(const char *name, int ret =3D -1; int direrr; DIR *dir; - char *path =3D NULL; =20 if (virDirOpen(&dir, name) < 0) return -1; =20 while ((direrr =3D virDirRead(dir, &ent, name)) > 0) { - VIR_FREE(path); + VIR_AUTOFREE(char *) path =3D NULL; + if (virAsprintf(&path, "%s/%s", name, ent->d_name) < 0) goto cleanup; =20 @@ -3075,8 +3031,6 @@ int virFileChownFiles(const char *name, ret =3D 0; =20 cleanup: - VIR_FREE(path); - virDirClose(&dir); =20 return ret; @@ -3138,19 +3092,14 @@ int virFileMakePathWithMode(const char *path, mode_t mode) { - int ret =3D -1; - char *tmp; + VIR_AUTOFREE(char *) tmp =3D NULL; =20 if (VIR_STRDUP(tmp, path) < 0) { errno =3D ENOMEM; - goto cleanup; + return -1; } =20 - ret =3D virFileMakePathHelper(tmp, mode); - - cleanup: - VIR_FREE(tmp); - return ret; + return virFileMakePathHelper(tmp, mode); } =20 =20 @@ -3158,8 +3107,7 @@ int virFileMakeParentPath(const char *path) { char *p; - char *tmp; - int ret =3D -1; + VIR_AUTOFREE(char *) tmp =3D NULL; =20 VIR_DEBUG("path=3D%s", path); =20 @@ -3170,15 +3118,11 @@ virFileMakeParentPath(const char *path) =20 if ((p =3D strrchr(tmp, '/')) =3D=3D NULL) { errno =3D EINVAL; - goto cleanup; + return -1; } *p =3D '\0'; =20 - ret =3D virFileMakePathHelper(tmp, 0777); - - cleanup: - VIR_FREE(tmp); - return ret; + return virFileMakePathHelper(tmp, 0777); } =20 =20 @@ -3212,7 +3156,7 @@ virFileOpenTty(int *ttymaster, char **ttyName, int ra= wmode) * doesn't have to worry about that mess? */ int ret =3D -1; int slave =3D -1; - char *name =3D NULL; + VIR_AUTOFREE(char *) name =3D NULL; =20 /* Unfortunately, we can't use the name argument of openpty, since * there is no guarantee on how large the buffer has to be. @@ -3273,7 +3217,6 @@ virFileOpenTty(int *ttymaster, char **ttyName, int ra= wmode) if (ret !=3D 0) VIR_FORCE_CLOSE(*ttymaster); VIR_FORCE_CLOSE(slave); - VIR_FREE(name); =20 return ret; } @@ -3373,21 +3316,17 @@ virFileSkipRoot(const char *path) int virFileAbsPath(const char *path, char **abspath) { - char *buf; - if (path[0] =3D=3D '/') { if (VIR_STRDUP(*abspath, path) < 0) return -1; } else { - buf =3D getcwd(NULL, 0); + VIR_AUTOFREE(char *) buf =3D getcwd(NULL, 0); + if (buf =3D=3D NULL) return -1; =20 - if (virAsprintf(abspath, "%s/%s", buf, path) < 0) { - VIR_FREE(buf); + if (virAsprintf(abspath, "%s/%s", buf, path) < 0) return -1; - } - VIR_FREE(buf); } =20 return 0; @@ -3487,7 +3426,7 @@ virFileRemoveLastComponent(char *path) int virFilePrintf(FILE *fp, const char *msg, ...) { va_list vargs; - char *str; + VIR_AUTOFREE(char *) str =3D NULL; int ret; =20 va_start(vargs, msg); @@ -3501,8 +3440,6 @@ int virFilePrintf(FILE *fp, const char *msg, ...) ret =3D -1; } =20 - VIR_FREE(str); - cleanup: va_end(vargs); =20 @@ -3538,7 +3475,8 @@ int virFileIsSharedFSType(const char *path, int fstypes) { - char *dirpath, *p; + VIR_AUTOFREE(char *) dirpath =3D NULL; + char *p; struct statfs sb; int statfs_ret; =20 @@ -3558,7 +3496,6 @@ virFileIsSharedFSType(const char *path, if ((p =3D strrchr(dirpath, '/')) =3D=3D NULL) { virReportSystemError(EINVAL, _("Invalid relative path '%s'"), path); - VIR_FREE(dirpath); return -1; } =20 @@ -3571,8 +3508,6 @@ virFileIsSharedFSType(const char *path, =20 } while ((statfs_ret < 0) && (p !=3D dirpath)); =20 - VIR_FREE(dirpath); - if (statfs_ret < 0) { virReportSystemError(errno, _("cannot determine filesystem for '%s'"), @@ -3639,18 +3574,20 @@ virFileGetHugepageSize(const char *path, static int virFileGetDefaultHugepageSize(unsigned long long *size) { - int ret =3D -1; - char *meminfo, *c, *n, *unit; + VIR_AUTOFREE(char *) meminfo =3D NULL; + char *c; + char *n; + char *unit; =20 if (virFileReadAll(PROC_MEMINFO, 4096, &meminfo) < 0) - goto cleanup; + return -1; =20 if (!(c =3D strstr(meminfo, HUGEPAGESIZE_STR))) { virReportError(VIR_ERR_NO_SUPPORT, _("%s not found in %s"), HUGEPAGESIZE_STR, PROC_MEMINFO); - goto cleanup; + return -1; } c +=3D strlen(HUGEPAGESIZE_STR); =20 @@ -3663,13 +3600,10 @@ virFileGetDefaultHugepageSize(unsigned long long *s= ize) virReportError(VIR_ERR_INTERNAL_ERROR, _("Unable to parse %s %s"), HUGEPAGESIZE_STR, c); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FREE(meminfo); - return ret; + return 0; } =20 # define PROC_MOUNTS "/proc/mounts" @@ -3963,10 +3897,8 @@ virFileCopyACLs(const char *src, int virFileComparePaths(const char *p1, const char *p2) { - int ret =3D -1; - char *res1, *res2; - - res1 =3D res2 =3D NULL; + VIR_AUTOFREE(char *) res1 =3D NULL; + VIR_AUTOFREE(char *) res2 =3D NULL; =20 /* Assume p1 and p2 are symlinks, so try to resolve and canonicalize t= hem. * Canonicalization fails for example on file systems names like 'proc= ' or @@ -3975,19 +3907,13 @@ virFileComparePaths(const char *p1, const char *p2) */ ignore_value(virFileResolveLink(p1, &res1)); if (!res1 && VIR_STRDUP(res1, p1) < 0) - goto cleanup; + return -1; =20 ignore_value(virFileResolveLink(p2, &res2)); if (!res2 && VIR_STRDUP(res2, p2) < 0) - goto cleanup; - - ret =3D STREQ_NULLABLE(res1, res2); - - cleanup: - VIR_FREE(res1); - VIR_FREE(res2); + return -1; =20 - return ret; + return STREQ_NULLABLE(res1, res2); } =20 =20 @@ -4131,25 +4057,22 @@ virFileInData(int fd ATTRIBUTE_UNUSED, int virFileReadValueInt(int *value, const char *format, ...) { - int ret =3D -1; - char *str =3D NULL; - char *path =3D NULL; + VIR_AUTOFREE(char *) str =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; va_list ap; =20 va_start(ap, format); if (virVasprintf(&path, format, ap) < 0) { va_end(ap); - goto cleanup; + return -1; } va_end(ap); =20 - if (!virFileExists(path)) { - ret =3D -2; - goto cleanup; - } + if (!virFileExists(path)) + return -2; =20 if (virFileReadAll(path, INT_BUFSIZE_BOUND(*value), &str) < 0) - goto cleanup; + return -1; =20 virStringTrimOptionalNewline(str); =20 @@ -4157,14 +4080,10 @@ virFileReadValueInt(int *value, const char *format,= ...) virReportError(VIR_ERR_INTERNAL_ERROR, _("Invalid integer value '%s' in file '%s'"), str, path); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FREE(path); - VIR_FREE(str); - return ret; + return 0; } =20 =20 @@ -4181,25 +4100,22 @@ virFileReadValueInt(int *value, const char *format,= ...) int virFileReadValueUint(unsigned int *value, const char *format, ...) { - int ret =3D -1; - char *str =3D NULL; - char *path =3D NULL; + VIR_AUTOFREE(char *) str =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; va_list ap; =20 va_start(ap, format); if (virVasprintf(&path, format, ap) < 0) { va_end(ap); - goto cleanup; + return -1; } va_end(ap); =20 - if (!virFileExists(path)) { - ret =3D -2; - goto cleanup; - } + if (!virFileExists(path)) + return -2; =20 if (virFileReadAll(path, INT_BUFSIZE_BOUND(*value), &str) < 0) - goto cleanup; + return -1; =20 virStringTrimOptionalNewline(str); =20 @@ -4207,14 +4123,10 @@ virFileReadValueUint(unsigned int *value, const cha= r *format, ...) virReportError(VIR_ERR_INTERNAL_ERROR, _("Invalid unsigned integer value '%s' in file '%s'= "), str, path); - goto cleanup; + return -1; } =20 - ret =3D 0; - cleanup: - VIR_FREE(path); - VIR_FREE(str); - return ret; + return 0; } =20 =20 @@ -4231,26 +4143,23 @@ virFileReadValueUint(unsigned int *value, const cha= r *format, ...) int virFileReadValueScaledInt(unsigned long long *value, const char *format, .= ..) { - int ret =3D -1; - char *str =3D NULL; + VIR_AUTOFREE(char *) str =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; char *endp =3D NULL; - char *path =3D NULL; va_list ap; =20 va_start(ap, format); if (virVasprintf(&path, format, ap) < 0) { va_end(ap); - goto cleanup; + return -1; } va_end(ap); =20 - if (!virFileExists(path)) { - ret =3D -2; - goto cleanup; - } + if (!virFileExists(path)) + return -2; =20 if (virFileReadAll(path, INT_BUFSIZE_BOUND(*value), &str) < 0) - goto cleanup; + return -1; =20 virStringTrimOptionalNewline(str); =20 @@ -4258,14 +4167,10 @@ virFileReadValueScaledInt(unsigned long long *value= , const char *format, ...) virReportError(VIR_ERR_INTERNAL_ERROR, _("Invalid unsigned scaled integer value '%s' in fi= le '%s'"), str, path); - goto cleanup; + return -1; } =20 - ret =3D virScaleInteger(value, endp, 1024, ULLONG_MAX); - cleanup: - VIR_FREE(path); - VIR_FREE(str); - return ret; + return virScaleInteger(value, endp, 1024, ULLONG_MAX); } =20 /* Arbitrarily sized number, feel free to change, but the function should = be @@ -4285,37 +4190,30 @@ virFileReadValueScaledInt(unsigned long long *value= , const char *format, ...) int virFileReadValueBitmap(virBitmapPtr *value, const char *format, ...) { - int ret =3D -1; - char *str =3D NULL; - char *path =3D NULL; + VIR_AUTOFREE(char *) str =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; va_list ap; =20 va_start(ap, format); if (virVasprintf(&path, format, ap) < 0) { va_end(ap); - goto cleanup; + return -1; } va_end(ap); =20 - if (!virFileExists(path)) { - ret =3D -2; - goto cleanup; - } + if (!virFileExists(path)) + return -2; =20 if (virFileReadAll(path, VIR_FILE_READ_VALUE_STRING_MAX, &str) < 0) - goto cleanup; + return -1; =20 virStringTrimOptionalNewline(str); =20 *value =3D virBitmapParseUnlimited(str); if (!*value) - goto cleanup; + return -1; =20 - ret =3D 0; - cleanup: - VIR_FREE(path); - VIR_FREE(str); - return ret; + return 0; } =20 /** @@ -4333,30 +4231,26 @@ virFileReadValueBitmap(virBitmapPtr *value, const c= har *format, ...) int virFileReadValueString(char **value, const char *format, ...) { - int ret =3D -1; - char *str =3D NULL; - char *path =3D NULL; + int ret; + VIR_AUTOFREE(char *) str =3D NULL; + VIR_AUTOFREE(char *) path =3D NULL; va_list ap; =20 va_start(ap, format); if (virVasprintf(&path, format, ap) < 0) { va_end(ap); - goto cleanup; + return -1; } va_end(ap); =20 - if (!virFileExists(path)) { - ret =3D -2; - goto cleanup; - } + if (!virFileExists(path)) + return -2; =20 ret =3D virFileReadAll(path, VIR_FILE_READ_VALUE_STRING_MAX, value); =20 if (*value) virStringTrimOptionalNewline(*value); - cleanup: - VIR_FREE(path); - VIR_FREE(str); + return ret; } =20 --=20 1.8.3.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list