]> xenbits.xensource.com Git - libvirt.git/commitdiff
avoid problems with sign-extended "char" operand to is* functions
authorJim Meyering <meyering@redhat.com>
Fri, 25 Apr 2008 14:53:05 +0000 (14:53 +0000)
committerJim Meyering <meyering@redhat.com>
Fri, 25 Apr 2008 14:53:05 +0000 (14:53 +0000)
* src/util.h (to_uchar): Define function.
* src/nodeinfo.c (linuxNodeInfoCPUPopulate): Apply to_uchar to is* operand.
* src/qemu_driver.c (qemudExtractMonitorPath): Likewise.
* src/sexpr.c (_string2sexpr): Likewise.
* src/stats_linux.c (xenLinuxDomainDeviceID): Likewise.
* src/util.c (TOLOWER, __virMacAddrCompare, virParseMacAddr): Likewise.
* src/virsh.c (cmdVcpupin, vshCommandGetToken): Likewise.

ChangeLog
src/nodeinfo.c
src/qemu_driver.c
src/sexpr.c
src/stats_linux.c
src/util.c
src/util.h
src/virsh.c

index 22f65cb0b633b1c4fc93af16b052574f51092cc9..abe6dc42a07a215b350483c46cc820b76f7bc803 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+Fri Apr 25 16:51:58 CEST 2008 Jim Meyering <meyering@redhat.com>
+
+       avoid problems with sign-extended "char" operand to is* functions
+       * src/util.h (to_uchar): Define function.
+       * src/sexpr.c (_string2sexpr): Apply to_uchar to is* operand.
+       * src/nodeinfo.c (linuxNodeInfoCPUPopulate): Likewise.
+       * src/qemu_driver.c (qemudExtractMonitorPath): Likewise.
+       * src/stats_linux.c (xenLinuxDomainDeviceID): Likewise.
+       * src/util.c (TOLOWER, __virMacAddrCompare, virParseMacAddr): Likewise.
+       * src/virsh.c (cmdVcpupin, vshCommandGetToken): Likewise.
+
 Thu Apr 24 17:18:18 CEST 2008 Daniel Veillard <veillard@redhat.com>
 
        * docs/page.xsl docs/redhat.gif docs/*.html: using the shadowman
index 77b2072fbf903b87e79522628b60438654b412bf..c1e80138c9e085e2260ff79f1407cd9fd0e9cfab 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * nodeinfo.c: Helper routines for OS specific node information
  *
- * Copyright (C) 2006, 2007 Red Hat, Inc.
+ * Copyright (C) 2006, 2007, 2008 Red Hat, Inc.
  * Copyright (C) 2006 Daniel P. Berrange
  *
  * This library is free software; you can redistribute it and/or
@@ -59,7 +59,7 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
         char *buf = line;
         if (STREQLEN(buf, "processor", 9)) { /* aka a single logical CPU */
             buf += 9;
-            while (*buf && isspace(*buf))
+            while (*buf && isspace(to_uchar(*buf)))
                 buf++;
             if (*buf != ':') {
                 __virRaiseError(conn, NULL, NULL, 0, VIR_ERR_INTERNAL_ERROR,
@@ -72,7 +72,7 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
             char *p;
             unsigned int ui;
             buf += 9;
-            while (*buf && isspace(*buf))
+            while (*buf && isspace(to_uchar(*buf)))
                 buf++;
             if (*buf != ':' || !buf[1]) {
                 __virRaiseError(conn, NULL, NULL, 0, VIR_ERR_INTERNAL_ERROR,
@@ -82,13 +82,13 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
             }
             if (virStrToLong_ui(buf+1, &p, 10, &ui) == 0
                 /* Accept trailing fractional part.  */
-                && (*p == '\0' || *p == '.' || isspace(*p)))
+                && (*p == '\0' || *p == '.' || isspace(to_uchar(*p))))
                 nodeinfo->mhz = ui;
         } else if (STREQLEN(buf, "cpu cores", 9)) { /* aka cores */
             char *p;
             unsigned int id;
             buf += 9;
-            while (*buf && isspace(*buf))
+            while (*buf && isspace(to_uchar(*buf)))
                 buf++;
             if (*buf != ':' || !buf[1]) {
                 __virRaiseError(conn, NULL, NULL, 0, VIR_ERR_INTERNAL_ERROR,
@@ -97,7 +97,7 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
                 return -1;
             }
             if (virStrToLong_ui(buf+1, &p, 10, &id) == 0
-                && (*p == '\0' || isspace(*p))
+                && (*p == '\0' || isspace(to_uchar(*p)))
                 && id > nodeinfo->cores)
                 nodeinfo->cores = id;
         }
index b65ae6625a2787b343befefd3b4687e89d28d4b3..eb2f6e8903ab2c0edd7139f3640bd3ecf82e6986 100644 (file)
@@ -513,7 +513,7 @@ static int qemudExtractMonitorPath(const char *haystack, char *path, int pathmax
          * The monitor path ends at first whitespace char
          * so lets search for it & NULL terminate it there
          */
-        if (isspace(*path)) {
+        if (isspace(to_uchar(*path))) {
             *path = '\0';
             return 0;
         }
index 9f6ed0d6588cc8024ef76f769061b315ca16be01..4cb4edf26873fcb30d1c7b8ee21732554af509ad 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "internal.h"
 #include "sexpr.h"
+#include "util.h"
 
 /**
  * virSexprError:
@@ -357,7 +358,8 @@ _string2sexpr(const char *buffer, size_t * end)
         } else {
             start = ptr;
 
-            while (*ptr && !isspace(*ptr) && *ptr != ')' && *ptr != '(') {
+            while (*ptr && !isspace(to_uchar(*ptr))
+                   && *ptr != ')' && *ptr != '(') {
                 ptr++;
             }
 
index bf46ca2191f7babaf1e4d9d28994cfe5bdf0057c..e84e24ff3e395951098d8a103b5f9e99088edf58 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Linux block and network stats.
  *
- * Copyright (C) 2007 Red Hat, Inc.
+ * Copyright (C) 2007, 2008 Red Hat, Inc.
  *
  * See COPYING.LIB for the License of this software
  *
@@ -262,7 +262,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)
         }
 
         if (path[4] != '\0') {
-            if (!isdigit(path[4]) || path[4] == '0' ||
+            if (!isdigit(to_uchar(path[4])) || path[4] == '0' ||
                 virStrToLong_i(path+4, NULL, 10, &part) < 0 ||
                 part < 1 || part > 15) {
                 statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__,
@@ -306,7 +306,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)
             } else {
                 p = path + 3;
             }
-            if (p && (!isdigit(*p) || *p == '0' ||
+            if (p && (!isdigit(to_uchar(*p)) || *p == '0' ||
                       virStrToLong_i(p, NULL, 10, &part) < 0 ||
                       part < 1 || part > 15)) {
                 statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__,
@@ -332,7 +332,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)
         }
 
         if (path[3] != '\0') {
-            if (!isdigit(path[3]) || path[3] == '0' ||
+            if (!isdigit(to_uchar(path[3])) || path[3] == '0' ||
                 virStrToLong_i(path+3, NULL, 10, &part) < 0 ||
                 part < 1 || part > 63) {
                 statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__,
index 5a44f1865cd1575fafc47c4f11165b689604b707..8f3cef9aa94588c8cfa62a15188b93f595c9f414 100644 (file)
@@ -57,7 +57,7 @@
 
 #define MAX_ERROR_LEN   1024
 
-#define TOLOWER(Ch) (isupper (Ch) ? tolower (Ch) : (Ch))
+#define TOLOWER(Ch) (isupper (to_uchar(Ch)) ? tolower (Ch) : (Ch))
 
 #define virLog(msg...) fprintf(stderr, msg)
 
@@ -704,9 +704,9 @@ __virMacAddrCompare (const char *p, const char *q)
 {
     unsigned char c, d;
     do {
-        while (*p == '0' && isxdigit (p[1]))
+        while (*p == '0' && isxdigit (to_uchar(p[1])))
             ++p;
-        while (*q == '0' && isxdigit (q[1]))
+        while (*q == '0' && isxdigit (to_uchar(q[1])))
             ++q;
         c = TOLOWER (*p);
         d = TOLOWER (*q);
@@ -749,7 +749,7 @@ virParseMacAddr(const char* str, unsigned char *addr)
         /* This is solely to avoid accepting the leading
          * space or "+" that strtoul would otherwise accept.
          */
-        if (!isxdigit(*str))
+        if (!isxdigit(to_uchar(*str)))
             break;
 
         result = strtoul(str, &end_ptr, 16);
index 25165046821efdebcb854862a5ed7256c5a66a3f..af68bc85807ba5b603fb999628fcf0ba433020ac 100644 (file)
 #include "internal.h"
 #include "util-lib.h"
 
+/* Convert a possibly-signed character to an unsigned character.  This is
+   a bit safer than casting to unsigned char, since it catches some type
+   errors that the cast doesn't.  */
+static inline unsigned char to_uchar (char ch) { return ch; }
+
 int virExec(virConnectPtr conn, char **argv, int *retpid,
             int infd, int *outfd, int *errfd);
 int virExecNonBlock(virConnectPtr conn, char **argv, int *retpid,
index 772967e5176c0c49ee1b9a58e91dd5d7be34b18f..ef6165bc3c0e7f1d947a2a277fdf301f808cd168 100644 (file)
@@ -1763,7 +1763,7 @@ cmdVcpupin(vshControl * ctl, vshCmd * cmd)
     for (i = 0; cpulist[i]; i++) {
         switch (state) {
         case expect_num:
-            if (!isdigit (cpulist[i])) {
+          if (!isdigit (to_uchar(cpulist[i]))) {
                 vshError( ctl, FALSE, _("cpulist: %s: Invalid format. Expecting digit at position %d (near '%c')."), cpulist, i, cpulist[i]);
                 virDomainFree (dom);
                 return FALSE;
@@ -1773,7 +1773,7 @@ cmdVcpupin(vshControl * ctl, vshCmd * cmd)
         case expect_num_or_comma:
             if (cpulist[i] == ',')
                 state = expect_num;
-            else if (!isdigit (cpulist[i])) {
+            else if (!isdigit (to_uchar(cpulist[i]))) {
                 vshError(ctl, FALSE, _("cpulist: %s: Invalid format. Expecting digit or comma at position %d (near '%c')."), cpulist, i, cpulist[i]);
                 virDomainFree (dom);
                 return FALSE;
@@ -5685,7 +5685,7 @@ vshCommandGetToken(vshControl * ctl, char *str, char **end, char **res)
 
         if (tk == VSH_TK_NONE) {
             if (*p == '-' && *(p + 1) == '-' && *(p + 2)
-                && isalnum((unsigned char) *(p + 2))) {
+                && isalnum(to_uchar(*(p + 2)))) {
                 tk = VSH_TK_OPTION;
                 p += 2;
             } else {