[libvirt] [PATCH] avoid using STREQLEN with a literal; use STRPREFIX instead

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



The goal of this change is to remove the error-prone duplication
between literal strings and corresponding literal length.

This was almost 100% mechanical:

# Do this:
# -        STRNEQLEN((const char *)target, "hd", 2) &&
# +        !STRPREFIX((const char *)target, "hd") &&
git grep -l '\<STRNEQLEN *('| f | xargs \
  perl -pi -e 's/\bSTRNEQLEN( *\(.*?, *".*?"), \d+\)/!STRPREFIX$1)/'

# Do this:
# -                } else if (STREQLEN(key, "vnclisten=", 10)) {
# +                } else if (STRPREFIX(key, "vnclisten=")) {
git grep -l '\<STREQLEN *('| f | xargs \
  perl -pi -e 's/\bSTREQLEN( *\(.*?, *".*?"), \d+\)/STRPREFIX$1)/'

# Two missed by the above
# src/qemu_conf.c:  if (STREQLEN("vnet", (const char*)ifname, 4)) {
# src/xm_internal.c: if (STREQLEN(ent->d_name, QEMU_IF_SCRIPT, strlen(QEMU_IF_SCRIPT)))
# Since there are 3 more like the latter, do them automatically:
git grep -l '\<STREQLEN *('| f | xargs \
  perl -pi -e 's/\bSTREQLEN( *\(.*?, *(\w+)), strlen\(\2\)\)/STRPREFIX$1)/'

I manually reversed the two args with the "vnet" use in qemu_conf.c,
so that the perl code would transform that one, too.

>From b043c2d627a7133d75664c01a3fe813b760502e9 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering@xxxxxxxxxx>
Date: Thu, 15 May 2008 15:58:23 +0200
Subject: [PATCH] avoid using STREQLEN with a literal; use STRPREFIX instead

Likewise for STRNEQLEN -> !STRPREFIX.
* src/nodeinfo.c (linuxNodeInfoCPUPopulate):
* src/qemu_conf.c (qemudNetworkIfaceConnect):
(qemudParseInterfaceXML):
* src/qemu_driver.c (qemudDomainBlockStats):
* src/remote_internal.c (call):
* src/stats_linux.c (xenLinuxDomainDeviceID):
* src/xend_internal.c (xend_parse_sexp_desc):
(xend_get, sexpr_to_xend_topology):
* src/xm_internal.c (xenXMConfigCacheRefresh)
(xenXMDomainFormatXML):
---
 src/nodeinfo.c        |    6 +++---
 src/qemu_conf.c       |    6 +++---
 src/qemu_driver.c     |   14 +++++++-------
 src/remote_internal.c |    2 +-
 src/stats_linux.c     |    8 ++++----
 src/xend_internal.c   |    6 +++---
 src/xm_internal.c     |   14 +++++++-------
 7 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/src/nodeinfo.c b/src/nodeinfo.c
index e227c69..b2ef6ee 100644
--- a/src/nodeinfo.c
+++ b/src/nodeinfo.c
@@ -57,7 +57,7 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
     /* XXX hyperthreads */
     while (fgets(line, sizeof(line), cpuinfo) != NULL) {
         char *buf = line;
-        if (STREQLEN(buf, "processor", 9)) { /* aka a single logical CPU */
+        if (STRPREFIX(buf, "processor")) { /* aka a single logical CPU */
             buf += 9;
             while (*buf && c_isspace(*buf))
                 buf++;
@@ -68,7 +68,7 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
                 return -1;
             }
             nodeinfo->cpus++;
-        } else if (STREQLEN(buf, "cpu MHz", 7)) {
+        } else if (STRPREFIX(buf, "cpu MHz")) {
             char *p;
             unsigned int ui;
             buf += 9;
@@ -84,7 +84,7 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
                 /* Accept trailing fractional part.  */
                 && (*p == '\0' || *p == '.' || c_isspace(*p)))
                 nodeinfo->mhz = ui;
-        } else if (STREQLEN(buf, "cpu cores", 9)) { /* aka cores */
+        } else if (STRPREFIX(buf, "cpu cores")) { /* aka cores */
             char *p;
             unsigned int id;
             buf += 9;
diff --git a/src/qemu_conf.c b/src/qemu_conf.c
index f31b33a..8ae0960 100644
--- a/src/qemu_conf.c
+++ b/src/qemu_conf.c
@@ -816,7 +816,7 @@ static int qemudParseInterfaceXML(virConnectPtr conn,
                         (net->type == QEMUD_NET_BRIDGE)) &&
                        xmlStrEqual(cur->name, BAD_CAST "target")) {
                 ifname = xmlGetProp(cur, BAD_CAST "dev");
-                if (STREQLEN("vnet", (const char*)ifname, 4)) {
+                if (STRPREFIX((const char*)ifname, "vnet")) {
                     /* An auto-generated target name, blank it out */
                     xmlFree(ifname);
                     ifname = NULL;
@@ -2130,7 +2130,7 @@ qemudNetworkIfaceConnect(virConnectPtr conn,
         }
         brname = network->bridge;
         if (net->dst.network.ifname[0] == '\0' ||
-            STREQLEN(net->dst.network.ifname, "vnet", 4) ||
+            STRPREFIX(net->dst.network.ifname, "vnet") ||
             strchr(net->dst.network.ifname, '%')) {
             strcpy(net->dst.network.ifname, "vnet%d");
         }
@@ -2138,7 +2138,7 @@ qemudNetworkIfaceConnect(virConnectPtr conn,
     } else if (net->type == QEMUD_NET_BRIDGE) {
         brname = net->dst.bridge.brname;
         if (net->dst.bridge.ifname[0] == '\0' ||
-            STREQLEN(net->dst.bridge.ifname, "vnet", 4) ||
+            STRPREFIX(net->dst.bridge.ifname, "vnet") ||
             strchr(net->dst.bridge.ifname, '%')) {
             strcpy(net->dst.bridge.ifname, "vnet%d");
         }
diff --git a/src/qemu_driver.c b/src/qemu_driver.c
index 7d52798..6ba6179 100644
--- a/src/qemu_driver.c
+++ b/src/qemu_driver.c
@@ -2650,12 +2650,12 @@ qemudDomainBlockStats (virDomainPtr dom,
      *   cdrom    to  ide1-cd0
      *   fd[a-]   to  floppy[0-]
      */
-    if (STREQLEN (path, "hd", 2) && path[2] >= 'a' && path[2] <= 'z')
+    if (STRPREFIX (path, "hd") && path[2] >= 'a' && path[2] <= 'z')
         snprintf (qemu_dev_name, sizeof (qemu_dev_name),
                   "ide0-hd%d", path[2] - 'a');
     else if (STREQ (path, "cdrom"))
         strcpy (qemu_dev_name, "ide1-cd0");
-    else if (STREQLEN (path, "fd", 2) && path[2] >= 'a' && path[2] <= 'z')
+    else if (STRPREFIX (path, "fd") && path[2] >= 'a' && path[2] <= 'z')
         snprintf (qemu_dev_name, sizeof (qemu_dev_name),
                   "floppy%d", path[2] - 'a');
     else {
@@ -2679,7 +2679,7 @@ qemudDomainBlockStats (virDomainPtr dom,
      * unlikely to be the name of a block device, we can use this
      * to detect if qemu supports the command.
      */
-    if (STREQLEN (info, "info ", 5)) {
+    if (STRPREFIX (info, "info ")) {
         free (info);
         qemudReportError (dom->conn, dom, NULL, VIR_ERR_NO_SUPPORT,
                           "%s",
@@ -2711,19 +2711,19 @@ qemudDomainBlockStats (virDomainPtr dom,
             p += len+2;         /* Skip to first label. */

             while (*p) {
-                if (STREQLEN (p, "rd_bytes=", 9)) {
+                if (STRPREFIX (p, "rd_bytes=")) {
                     p += 9;
                     if (virStrToLong_ll (p, &dummy, 10, &stats->rd_bytes) == -1)
                         DEBUG ("error reading rd_bytes: %s", p);
-                } else if (STREQLEN (p, "wr_bytes=", 9)) {
+                } else if (STRPREFIX (p, "wr_bytes=")) {
                     p += 9;
                     if (virStrToLong_ll (p, &dummy, 10, &stats->wr_bytes) == -1)
                         DEBUG ("error reading wr_bytes: %s", p);
-                } else if (STREQLEN (p, "rd_operations=", 14)) {
+                } else if (STRPREFIX (p, "rd_operations=")) {
                     p += 14;
                     if (virStrToLong_ll (p, &dummy, 10, &stats->rd_req) == -1)
                         DEBUG ("error reading rd_req: %s", p);
-                } else if (STREQLEN (p, "wr_operations=", 14)) {
+                } else if (STRPREFIX (p, "wr_operations=")) {
                     p += 14;
                     if (virStrToLong_ll (p, &dummy, 10, &stats->wr_req) == -1)
                         DEBUG ("error reading wr_req: %s", p);
diff --git a/src/remote_internal.c b/src/remote_internal.c
index 1b6e9d3..51e8eb7 100644
--- a/src/remote_internal.c
+++ b/src/remote_internal.c
@@ -4339,7 +4339,7 @@ call (virConnectPtr conn, struct private_data *priv,
             rerror.domain == VIR_FROM_REMOTE &&
             rerror.code == VIR_ERR_RPC &&
             rerror.level == VIR_ERR_ERROR &&
-            STREQLEN(*rerror.message, "unknown procedure", 17)) {
+            STRPREFIX(*rerror.message, "unknown procedure")) {
             return -2;
         }
         server_error (flags & REMOTE_CALL_IN_OPEN ? NULL : conn, &rerror);
diff --git a/src/stats_linux.c b/src/stats_linux.c
index cb647fe..30a4990 100644
--- a/src/stats_linux.c
+++ b/src/stats_linux.c
@@ -230,7 +230,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)

     /* Strip leading path if any */
     if (strlen(path) > 5 &&
-        STREQLEN(path, "/dev/", 5))
+        STRPREFIX(path, "/dev/"))
         path += 5;

     /*
@@ -251,7 +251,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)
      */

     if (strlen (path) >= 4 &&
-        STREQLEN (path, "xvd", 3)) {
+        STRPREFIX (path, "xvd")) {
         /* Xen paravirt device handling */
         disk = (path[3] - 'a');
         if (disk < 0 || disk > 15) {
@@ -274,7 +274,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)

         return (XENVBD_MAJOR * 256) + (disk * 16) + part;
     } else if (strlen (path) >= 3 &&
-               STREQLEN (path, "sd", 2)) {
+               STRPREFIX (path, "sd")) {
         /* SCSI device handling */
         int majors[] = { SCSI_DISK0_MAJOR, SCSI_DISK1_MAJOR, SCSI_DISK2_MAJOR,
                          SCSI_DISK3_MAJOR, SCSI_DISK4_MAJOR, SCSI_DISK5_MAJOR,
@@ -318,7 +318,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)

         return (majors[disk/16] * 256) + ((disk%16) * 16) + part;
     } else if (strlen (path) >= 3 &&
-               STREQLEN (path, "hd", 2)) {
+               STRPREFIX (path, "hd")) {
         /* IDE device handling */
         int majors[] = { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR,
                          IDE4_MAJOR, IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR,
diff --git a/src/xend_internal.c b/src/xend_internal.c
index 6aecfdd..590fabe 100644
--- a/src/xend_internal.c
+++ b/src/xend_internal.c
@@ -487,7 +487,7 @@ xend_get(virConnectPtr xend, const char *path,
     close(s);

     if (((ret < 0) || (ret >= 300)) &&
-        ((ret != 404) || (STRNEQLEN(path, "/xend/domain/", 13)))) {
+        ((ret != 404) || (!STRPREFIX(path, "/xend/domain/")))) {
         virXendError(xend, VIR_ERR_GET_FAILED, content);
     }

@@ -1943,7 +1943,7 @@ xend_parse_sexp_desc(virConnectPtr conn, struct sexpr *root,
                 }
             }

-            if (STREQLEN(dst, "ioemu:", 6))
+            if (STRPREFIX(dst, "ioemu:"))
                 dst += 6;

             /* New style disk config from Xen >= 3.0.3 */
@@ -2399,7 +2399,7 @@ sexpr_to_xend_topology(virConnectPtr conn,
             goto parse_error;
         cur++;
         virSkipSpaces(&cur);
-        if (STREQLEN(cur, "no cpus", 7)) {
+        if (STRPREFIX(cur, "no cpus")) {
             nb_cpus = 0;
             for (cpu = 0; cpu < numCpus; cpu++)
                 cpuset[cpu] = 0;
diff --git a/src/xm_internal.c b/src/xm_internal.c
index 393700c..9731125 100644
--- a/src/xm_internal.c
+++ b/src/xm_internal.c
@@ -353,19 +353,19 @@ static int xenXMConfigCacheRefresh (virConnectPtr conn) {
          */

         /* Like 'dot' files... */
-        if (STREQLEN(ent->d_name, ".", 1))
+        if (STRPREFIX(ent->d_name, "."))
             continue;
         /* ...and the XenD server config file */
-        if (STREQLEN(ent->d_name, XEND_CONFIG_FILE, strlen(XEND_CONFIG_FILE)))
+        if (STRPREFIX(ent->d_name, XEND_CONFIG_FILE))
             continue;
         /* ...and random PCI config cruft */
-        if (STREQLEN(ent->d_name, XEND_PCI_CONFIG_PREFIX, strlen(XEND_PCI_CONFIG_PREFIX)))
+        if (STRPREFIX(ent->d_name, XEND_PCI_CONFIG_PREFIX))
             continue;
         /* ...and the example domain configs */
-        if (STREQLEN(ent->d_name, XM_EXAMPLE_PREFIX, strlen(XM_EXAMPLE_PREFIX)))
+        if (STRPREFIX(ent->d_name, XM_EXAMPLE_PREFIX))
             continue;
         /* ...and the QEMU networking script */
-        if (STREQLEN(ent->d_name, QEMU_IF_SCRIPT, strlen(QEMU_IF_SCRIPT)))
+        if (STRPREFIX(ent->d_name, QEMU_IF_SCRIPT))
             continue;

         /* ...and editor backups */
@@ -778,7 +778,7 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
             }

             /* And the source driver sub-type */
-            if (STREQLEN(drvName, "tap", 3)) {
+            if (STRPREFIX(drvName, "tap")) {
                 if (!(tmp1 = strchr(tmp+1, ':')) || !tmp1[0])
                     goto skipdisk;
                 strncpy(drvType, tmp+1, (tmp1-(tmp+1)));
@@ -795,7 +795,7 @@ char *xenXMDomainFormatXML(virConnectPtr conn, virConfPtr conf) {
             }

             /* Remove legacy ioemu: junk */
-            if (STREQLEN(dev, "ioemu:", 6)) {
+            if (STRPREFIX(dev, "ioemu:")) {
                 memmove(dev, dev+6, strlen(dev)-5);
             }

--
1.5.5.1.148.gbc1be

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]