avoid problems with sign-extended "char" operand to is* functions

* 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.
This commit is contained in:
Jim Meyering 2008-04-25 14:53:05 +00:00
parent 3ad50bec71
commit 225ba3f0af
8 changed files with 37 additions and 19 deletions

View File

@ -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> Thu Apr 24 17:18:18 CEST 2008 Daniel Veillard <veillard@redhat.com>
* docs/page.xsl docs/redhat.gif docs/*.html: using the shadowman * docs/page.xsl docs/redhat.gif docs/*.html: using the shadowman

View File

@ -1,7 +1,7 @@
/* /*
* nodeinfo.c: Helper routines for OS specific node information * 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 * Copyright (C) 2006 Daniel P. Berrange
* *
* This library is free software; you can redistribute it and/or * 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; char *buf = line;
if (STREQLEN(buf, "processor", 9)) { /* aka a single logical CPU */ if (STREQLEN(buf, "processor", 9)) { /* aka a single logical CPU */
buf += 9; buf += 9;
while (*buf && isspace(*buf)) while (*buf && isspace(to_uchar(*buf)))
buf++; buf++;
if (*buf != ':') { if (*buf != ':') {
__virRaiseError(conn, NULL, NULL, 0, VIR_ERR_INTERNAL_ERROR, __virRaiseError(conn, NULL, NULL, 0, VIR_ERR_INTERNAL_ERROR,
@ -72,7 +72,7 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
char *p; char *p;
unsigned int ui; unsigned int ui;
buf += 9; buf += 9;
while (*buf && isspace(*buf)) while (*buf && isspace(to_uchar(*buf)))
buf++; buf++;
if (*buf != ':' || !buf[1]) { if (*buf != ':' || !buf[1]) {
__virRaiseError(conn, NULL, NULL, 0, VIR_ERR_INTERNAL_ERROR, __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 if (virStrToLong_ui(buf+1, &p, 10, &ui) == 0
/* Accept trailing fractional part. */ /* Accept trailing fractional part. */
&& (*p == '\0' || *p == '.' || isspace(*p))) && (*p == '\0' || *p == '.' || isspace(to_uchar(*p))))
nodeinfo->mhz = ui; nodeinfo->mhz = ui;
} else if (STREQLEN(buf, "cpu cores", 9)) { /* aka cores */ } else if (STREQLEN(buf, "cpu cores", 9)) { /* aka cores */
char *p; char *p;
unsigned int id; unsigned int id;
buf += 9; buf += 9;
while (*buf && isspace(*buf)) while (*buf && isspace(to_uchar(*buf)))
buf++; buf++;
if (*buf != ':' || !buf[1]) { if (*buf != ':' || !buf[1]) {
__virRaiseError(conn, NULL, NULL, 0, VIR_ERR_INTERNAL_ERROR, __virRaiseError(conn, NULL, NULL, 0, VIR_ERR_INTERNAL_ERROR,
@ -97,7 +97,7 @@ int linuxNodeInfoCPUPopulate(virConnectPtr conn, FILE *cpuinfo, virNodeInfoPtr n
return -1; return -1;
} }
if (virStrToLong_ui(buf+1, &p, 10, &id) == 0 if (virStrToLong_ui(buf+1, &p, 10, &id) == 0
&& (*p == '\0' || isspace(*p)) && (*p == '\0' || isspace(to_uchar(*p)))
&& id > nodeinfo->cores) && id > nodeinfo->cores)
nodeinfo->cores = id; nodeinfo->cores = id;
} }

View File

@ -513,7 +513,7 @@ static int qemudExtractMonitorPath(const char *haystack, char *path, int pathmax
* The monitor path ends at first whitespace char * The monitor path ends at first whitespace char
* so lets search for it & NULL terminate it there * so lets search for it & NULL terminate it there
*/ */
if (isspace(*path)) { if (isspace(to_uchar(*path))) {
*path = '\0'; *path = '\0';
return 0; return 0;
} }

View File

@ -20,6 +20,7 @@
#include "internal.h" #include "internal.h"
#include "sexpr.h" #include "sexpr.h"
#include "util.h"
/** /**
* virSexprError: * virSexprError:
@ -357,7 +358,8 @@ _string2sexpr(const char *buffer, size_t * end)
} else { } else {
start = ptr; start = ptr;
while (*ptr && !isspace(*ptr) && *ptr != ')' && *ptr != '(') { while (*ptr && !isspace(to_uchar(*ptr))
&& *ptr != ')' && *ptr != '(') {
ptr++; ptr++;
} }

View File

@ -1,7 +1,7 @@
/* /*
* Linux block and network stats. * 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 * 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 (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 || virStrToLong_i(path+4, NULL, 10, &part) < 0 ||
part < 1 || part > 15) { part < 1 || part > 15) {
statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__, statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__,
@ -306,7 +306,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)
} else { } else {
p = path + 3; p = path + 3;
} }
if (p && (!isdigit(*p) || *p == '0' || if (p && (!isdigit(to_uchar(*p)) || *p == '0' ||
virStrToLong_i(p, NULL, 10, &part) < 0 || virStrToLong_i(p, NULL, 10, &part) < 0 ||
part < 1 || part > 15)) { part < 1 || part > 15)) {
statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__, statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__,
@ -332,7 +332,7 @@ xenLinuxDomainDeviceID(virConnectPtr conn, int domid, const char *path)
} }
if (path[3] != '\0') { 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 || virStrToLong_i(path+3, NULL, 10, &part) < 0 ||
part < 1 || part > 63) { part < 1 || part > 63) {
statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__, statsErrorFunc (conn, VIR_ERR_INVALID_ARG, __FUNCTION__,

View File

@ -57,7 +57,7 @@
#define MAX_ERROR_LEN 1024 #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) #define virLog(msg...) fprintf(stderr, msg)
@ -704,9 +704,9 @@ __virMacAddrCompare (const char *p, const char *q)
{ {
unsigned char c, d; unsigned char c, d;
do { do {
while (*p == '0' && isxdigit (p[1])) while (*p == '0' && isxdigit (to_uchar(p[1])))
++p; ++p;
while (*q == '0' && isxdigit (q[1])) while (*q == '0' && isxdigit (to_uchar(q[1])))
++q; ++q;
c = TOLOWER (*p); c = TOLOWER (*p);
d = TOLOWER (*q); d = TOLOWER (*q);
@ -749,7 +749,7 @@ virParseMacAddr(const char* str, unsigned char *addr)
/* This is solely to avoid accepting the leading /* This is solely to avoid accepting the leading
* space or "+" that strtoul would otherwise accept. * space or "+" that strtoul would otherwise accept.
*/ */
if (!isxdigit(*str)) if (!isxdigit(to_uchar(*str)))
break; break;
result = strtoul(str, &end_ptr, 16); result = strtoul(str, &end_ptr, 16);

View File

@ -27,6 +27,11 @@
#include "internal.h" #include "internal.h"
#include "util-lib.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 virExec(virConnectPtr conn, char **argv, int *retpid,
int infd, int *outfd, int *errfd); int infd, int *outfd, int *errfd);
int virExecNonBlock(virConnectPtr conn, char **argv, int *retpid, int virExecNonBlock(virConnectPtr conn, char **argv, int *retpid,

View File

@ -1763,7 +1763,7 @@ cmdVcpupin(vshControl * ctl, vshCmd * cmd)
for (i = 0; cpulist[i]; i++) { for (i = 0; cpulist[i]; i++) {
switch (state) { switch (state) {
case expect_num: 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]); vshError( ctl, FALSE, _("cpulist: %s: Invalid format. Expecting digit at position %d (near '%c')."), cpulist, i, cpulist[i]);
virDomainFree (dom); virDomainFree (dom);
return FALSE; return FALSE;
@ -1773,7 +1773,7 @@ cmdVcpupin(vshControl * ctl, vshCmd * cmd)
case expect_num_or_comma: case expect_num_or_comma:
if (cpulist[i] == ',') if (cpulist[i] == ',')
state = expect_num; 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]); vshError(ctl, FALSE, _("cpulist: %s: Invalid format. Expecting digit or comma at position %d (near '%c')."), cpulist, i, cpulist[i]);
virDomainFree (dom); virDomainFree (dom);
return FALSE; return FALSE;
@ -5685,7 +5685,7 @@ vshCommandGetToken(vshControl * ctl, char *str, char **end, char **res)
if (tk == VSH_TK_NONE) { if (tk == VSH_TK_NONE) {
if (*p == '-' && *(p + 1) == '-' && *(p + 2) if (*p == '-' && *(p + 1) == '-' && *(p + 2)
&& isalnum((unsigned char) *(p + 2))) { && isalnum(to_uchar(*(p + 2)))) {
tk = VSH_TK_OPTION; tk = VSH_TK_OPTION;
p += 2; p += 2;
} else { } else {