start using c-ctype functions

Up to now, we've been avoiding ctype functions like isspace, isdigit,
etc.  because they are locale-dependent.  Now that we have the c-ctype
functions, we can start using *them*, to make the code more readable
with changes like these:

-        /* This may not work on EBCDIC. */
-        if ((*p >= 'a' && *p <= 'z') ||
-            (*p >= 'A' && *p <= 'Z') ||
-            (*p >= '0' && *p <= '9'))
+        if (c_isalnum(*p))

-    while ((*cur >= '0') && (*cur <= '9')) {
+    while (c_isdigit(*cur)) {

Also, some macros in conf.c used names that conflicted with
standard meaning of "BLANK" and "SPACE", so I've adjusted them
to be in line with the definition of e.g., isblank.
In addition, I've wrapped those statement macros with do {...} while (0),
so that we can't forget the ";" after a use.  There was one like that
already (fixed below).  The missing semicolon would mess up automatic
indenting.
* src/buf.c (virBufferURIEncodeString):
* src/conf.c (IS_EOL, SKIP_BLANKS_AND_EOL, SKIP_BLANKS)
(virConfParseLong, virConfParseValue, virConfParseName)
(virConfParseSeparator, virConfParseStatement, IS_BLANK, IS_CHAR)
(IS_DIGIT, IS_SPACE, SKIP_SPACES):
* src/nodeinfo.c:
* src/qemu_conf.c (qemudParseInterfaceXML):
* src/qemu_driver.c (qemudDomainBlockStats):
* src/sexpr.c:
* src/stats_linux.c:
* src/util.c (virParseNumber, virDiskNameToIndex):
* src/uuid.c (hextobin, virUUIDParse):
* src/virsh.c:
* src/xml.c (parseCpuNumber, virParseCpuSet):
This commit is contained in:
Jim Meyering 2008-05-16 09:37:44 +00:00
parent 0dc23afb0c
commit c1ee35af3b
12 changed files with 105 additions and 70 deletions

View File

@ -1,5 +1,42 @@
Fri May 16 11:29:30 CEST 2008 Jim Meyering <meyering@redhat.com> Fri May 16 11:29:30 CEST 2008 Jim Meyering <meyering@redhat.com>
start using c-ctype functions
Up to now, we've been avoiding ctype functions like isspace, isdigit,
etc. because they are locale-dependent. Now that we have the c-ctype
functions, we can start using *them*, to make the code more readable
with changes like these:
- /* This may not work on EBCDIC. */
- if ((*p >= 'a' && *p <= 'z') ||
- (*p >= 'A' && *p <= 'Z') ||
- (*p >= '0' && *p <= '9'))
+ if (c_isalnum(*p))
- while ((*cur >= '0') && (*cur <= '9')) {
+ while (c_isdigit(*cur)) {
Also, some macros in conf.c used names that conflicted with
standard meaning of "BLANK" and "SPACE", so I've adjusted them
to be in line with the definition of e.g., isblank.
In addition, I've wrapped those statement macros with do {...} while (0),
so that we can't forget the ";" after a use. There was one like that
already (fixed below). The missing semicolon would mess up automatic
indenting.
* src/buf.c (virBufferURIEncodeString):
* src/conf.c (IS_EOL, SKIP_BLANKS_AND_EOL, SKIP_BLANKS)
(virConfParseLong, virConfParseValue, virConfParseName)
(virConfParseSeparator, virConfParseStatement, IS_BLANK, IS_CHAR)
(IS_DIGIT, IS_SPACE, SKIP_SPACES):
* src/nodeinfo.c:
* src/qemu_conf.c (qemudParseInterfaceXML):
* src/qemu_driver.c (qemudDomainBlockStats):
* src/sexpr.c:
* src/stats_linux.c:
* src/util.c (virParseNumber, virDiskNameToIndex):
* src/uuid.c (hextobin, virUUIDParse):
* src/virsh.c:
* src/xml.c (parseCpuNumber, virParseCpuSet):
avoid a double-free bug avoid a double-free bug
* src/qemu_conf.c (qemudParseXML): Ensure that "obj" is either * src/qemu_conf.c (qemudParseXML): Ensure that "obj" is either
NULL or a valid malloc'd pointer before we might "goto error" NULL or a valid malloc'd pointer before we might "goto error"

View File

@ -16,6 +16,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdarg.h> #include <stdarg.h>
#include "c-ctype.h"
#define __VIR_BUFFER_C__ #define __VIR_BUFFER_C__
@ -349,10 +350,7 @@ virBufferURIEncodeString (virBufferPtr buf, const char *str)
return; return;
for (p = str; *p; ++p) { for (p = str; *p; ++p) {
/* This may not work on EBCDIC. */ if (c_isalnum(*p))
if ((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9'))
grow_size++; grow_size++;
else else
grow_size += 3; /* %ab */ grow_size += 3; /* %ab */
@ -362,10 +360,7 @@ virBufferURIEncodeString (virBufferPtr buf, const char *str)
return; return;
for (p = str; *p; ++p) { for (p = str; *p; ++p) {
/* This may not work on EBCDIC. */ if (c_isalnum(*p))
if ((*p >= 'a' && *p <= 'z') ||
(*p >= 'A' && *p <= 'Z') ||
(*p >= '0' && *p <= '9'))
buf->content[buf->use++] = *p; buf->content[buf->use++] = *p;
else { else {
uc = (unsigned char) *p; uc = (unsigned char) *p;

View File

@ -22,6 +22,7 @@
#include "buf.h" #include "buf.h"
#include "conf.h" #include "conf.h"
#include "util.h" #include "util.h"
#include "c-ctype.h"
/************************************************************************ /************************************************************************
* * * *
@ -45,17 +46,14 @@ struct _virConfParserCtxt {
#define CUR (*ctxt->cur) #define CUR (*ctxt->cur)
#define NEXT if (ctxt->cur < ctxt->end) ctxt->cur++; #define NEXT if (ctxt->cur < ctxt->end) ctxt->cur++;
#define IS_EOL(c) (((c) == '\n') || ((c) == '\r')) #define IS_EOL(c) (((c) == '\n') || ((c) == '\r'))
#define IS_BLANK(c) (((c) == ' ') || ((c) == '\n') || ((c) == '\r') || \
((c) == '\t')) #define SKIP_BLANKS_AND_EOL \
#define SKIP_BLANKS {while ((ctxt->cur < ctxt->end) && (IS_BLANK(CUR))){\ do { while ((ctxt->cur < ctxt->end) && (c_isblank(CUR) || IS_EOL(CUR))) { \
if (CUR == '\n') ctxt->line++; \ if (CUR == '\n') ctxt->line++; \
ctxt->cur++;}} ctxt->cur++;}} while (0)
#define IS_SPACE(c) (((c) == ' ') || ((c) == '\t')) #define SKIP_BLANKS \
#define SKIP_SPACES {while ((ctxt->cur < ctxt->end) && (IS_SPACE(CUR))) \ do { while ((ctxt->cur < ctxt->end) && (c_isblank(CUR))) \
ctxt->cur++;} ctxt->cur++; } while (0)
#define IS_CHAR(c) ((((c) >= 'a') && ((c) <= 'z')) || \
(((c) >= 'A') && ((c) <= 'Z')))
#define IS_DIGIT(c) (((c) >= '0') && ((c) <= '9'))
/************************************************************************ /************************************************************************
* * * *
@ -338,12 +336,12 @@ virConfParseLong(virConfParserCtxtPtr ctxt, long *val)
} else if (CUR == '+') { } else if (CUR == '+') {
NEXT; NEXT;
} }
if ((ctxt->cur >= ctxt->end) || (!IS_DIGIT(CUR))) { if ((ctxt->cur >= ctxt->end) || (!c_isdigit(CUR))) {
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated number"), virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("unterminated number"),
ctxt->line); ctxt->line);
return(-1); return(-1);
} }
while ((ctxt->cur < ctxt->end) && (IS_DIGIT(CUR))) { while ((ctxt->cur < ctxt->end) && (c_isdigit(CUR))) {
l = l * 10 + (CUR - '0'); l = l * 10 + (CUR - '0');
NEXT; NEXT;
} }
@ -428,7 +426,7 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
char *str = NULL; char *str = NULL;
long l = 0; long l = 0;
SKIP_SPACES; SKIP_BLANKS;
if (ctxt->cur >= ctxt->end) { if (ctxt->cur >= ctxt->end) {
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a value"), virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a value"),
ctxt->line); ctxt->line);
@ -442,10 +440,10 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
} else if (CUR == '[') { } else if (CUR == '[') {
type = VIR_CONF_LIST; type = VIR_CONF_LIST;
NEXT; NEXT;
SKIP_BLANKS; SKIP_BLANKS_AND_EOL;
if ((ctxt->cur < ctxt->end) && (CUR != ']')) { if ((ctxt->cur < ctxt->end) && (CUR != ']')) {
lst = virConfParseValue(ctxt); lst = virConfParseValue(ctxt);
SKIP_BLANKS; SKIP_BLANKS_AND_EOL;
} }
while ((ctxt->cur < ctxt->end) && (CUR != ']')) { while ((ctxt->cur < ctxt->end) && (CUR != ']')) {
if (CUR != ',') { if (CUR != ',') {
@ -455,7 +453,7 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
return(NULL); return(NULL);
} }
NEXT; NEXT;
SKIP_BLANKS; SKIP_BLANKS_AND_EOL;
if (CUR == ']') { if (CUR == ']') {
break; break;
} }
@ -467,7 +465,7 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
prev = lst; prev = lst;
while (prev->next != NULL) prev = prev->next; while (prev->next != NULL) prev = prev->next;
prev->next = tmp; prev->next = tmp;
SKIP_BLANKS; SKIP_BLANKS_AND_EOL;
} }
if (CUR == ']') { if (CUR == ']') {
NEXT; NEXT;
@ -477,7 +475,7 @@ virConfParseValue(virConfParserCtxtPtr ctxt)
virConfFreeList(lst); virConfFreeList(lst);
return(NULL); return(NULL);
} }
} else if (IS_DIGIT(CUR) || (CUR == '-') || (CUR == '+')) { } else if (c_isdigit(CUR) || (CUR == '-') || (CUR == '+')) {
if (virConfParseLong(ctxt, &l) < 0) { if (virConfParseLong(ctxt, &l) < 0) {
return(NULL); return(NULL);
} }
@ -514,14 +512,14 @@ virConfParseName(virConfParserCtxtPtr ctxt)
const char *base; const char *base;
char *ret; char *ret;
SKIP_SPACES; SKIP_BLANKS;
base = ctxt->cur; base = ctxt->cur;
/* TODO: probably need encoding support and UTF-8 parsing ! */ /* TODO: probably need encoding support and UTF-8 parsing ! */
if (!IS_CHAR(CUR)) { if (!c_isalpha(CUR)) {
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a name"), ctxt->line); virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a name"), ctxt->line);
return(NULL); return(NULL);
} }
while ((ctxt->cur < ctxt->end) && ((IS_CHAR(CUR)) || (IS_DIGIT(CUR)) || (CUR == '_'))) while ((ctxt->cur < ctxt->end) && (c_isalnum(CUR) || (CUR == '_')))
NEXT; NEXT;
ret = strndup(base, ctxt->cur - base); ret = strndup(base, ctxt->cur - base);
if (ret == NULL) { if (ret == NULL) {
@ -572,14 +570,14 @@ virConfParseComment(virConfParserCtxtPtr ctxt)
static int static int
virConfParseSeparator(virConfParserCtxtPtr ctxt) virConfParseSeparator(virConfParserCtxtPtr ctxt)
{ {
SKIP_SPACES; SKIP_BLANKS;
if (ctxt->cur >= ctxt->end) if (ctxt->cur >= ctxt->end)
return(0); return(0);
if (IS_EOL(CUR)) { if (IS_EOL(CUR)) {
SKIP_BLANKS SKIP_BLANKS_AND_EOL;
} else if (CUR == ';') { } else if (CUR == ';') {
NEXT; NEXT;
SKIP_BLANKS; SKIP_BLANKS_AND_EOL;
} else { } else {
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a separator"), virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting a separator"),
ctxt->line); ctxt->line);
@ -604,27 +602,27 @@ virConfParseStatement(virConfParserCtxtPtr ctxt)
virConfValuePtr value; virConfValuePtr value;
char *comm = NULL; char *comm = NULL;
SKIP_BLANKS; SKIP_BLANKS_AND_EOL;
if (CUR == '#') { if (CUR == '#') {
return(virConfParseComment(ctxt)); return(virConfParseComment(ctxt));
} }
name = virConfParseName(ctxt); name = virConfParseName(ctxt);
if (name == NULL) if (name == NULL)
return(-1); return(-1);
SKIP_SPACES; SKIP_BLANKS;
if (CUR != '=') { if (CUR != '=') {
virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting an assignment"), virConfError(NULL, VIR_ERR_CONF_SYNTAX, _("expecting an assignment"),
ctxt->line); ctxt->line);
return(-1); return(-1);
} }
NEXT; NEXT;
SKIP_SPACES; SKIP_BLANKS;
value = virConfParseValue(ctxt); value = virConfParseValue(ctxt);
if (value == NULL) { if (value == NULL) {
free(name); free(name);
return(-1); return(-1);
} }
SKIP_SPACES; SKIP_BLANKS;
if (CUR == '#') { if (CUR == '#') {
NEXT; NEXT;
base = ctxt->cur; base = ctxt->cur;

View File

@ -27,7 +27,7 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <errno.h> #include <errno.h>
#include <c-ctype.h> #include "c-ctype.h"
#ifdef HAVE_SYS_UTSNAME_H #ifdef HAVE_SYS_UTSNAME_H
#include <sys/utsname.h> #include <sys/utsname.h>

View File

@ -49,7 +49,8 @@
#include "buf.h" #include "buf.h"
#include "conf.h" #include "conf.h"
#include "util.h" #include "util.h"
#include <verify.h> #include "verify.h"
#include "c-ctype.h"
#define qemudLog(level, msg...) fprintf(stderr, msg) #define qemudLog(level, msg...) fprintf(stderr, msg)
@ -1007,7 +1008,7 @@ static int qemudParseInterfaceXML(virConnectPtr conn,
* i82551 i82557b i82559er ne2k_pci pcnet rtl8139 e1000 virtio * i82551 i82557b i82559er ne2k_pci pcnet rtl8139 e1000 virtio
*/ */
if (model != NULL) { if (model != NULL) {
int i, len, char_ok; int i, len;
len = xmlStrlen (model); len = xmlStrlen (model);
if (len >= QEMUD_MODEL_MAX_LEN) { if (len >= QEMUD_MODEL_MAX_LEN) {
@ -1016,10 +1017,7 @@ static int qemudParseInterfaceXML(virConnectPtr conn,
goto error; goto error;
} }
for (i = 0; i < len; ++i) { for (i = 0; i < len; ++i) {
char_ok = int char_ok = c_isalnum(model[i]) || model[i] == '_';
(model[i] >= '0' && model[i] <= '9') ||
(model[i] >= 'a' && model[i] <= 'z') ||
(model[i] >= 'A' && model[i] <= 'Z') || model[i] == '_';
if (!char_ok) { if (!char_ok) {
qemudReportError (conn, NULL, NULL, VIR_ERR_INVALID_ARG, "%s", qemudReportError (conn, NULL, NULL, VIR_ERR_INVALID_ARG, "%s",
_("Model name contains invalid characters")); _("Model name contains invalid characters"));

View File

@ -41,7 +41,6 @@
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <paths.h> #include <paths.h>
#include <c-ctype.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -49,6 +48,7 @@
#include "libvirt/virterror.h" #include "libvirt/virterror.h"
#include "c-ctype.h"
#include "event.h" #include "event.h"
#include "buf.h" #include "buf.h"
#include "util.h" #include "util.h"
@ -2650,12 +2650,12 @@ qemudDomainBlockStats (virDomainPtr dom,
* cdrom to ide1-cd0 * cdrom to ide1-cd0
* fd[a-] to floppy[0-] * fd[a-] to floppy[0-]
*/ */
if (STRPREFIX (path, "hd") && path[2] >= 'a' && path[2] <= 'z') if (STRPREFIX (path, "hd") && c_islower(path[2]))
snprintf (qemu_dev_name, sizeof (qemu_dev_name), snprintf (qemu_dev_name, sizeof (qemu_dev_name),
"ide0-hd%d", path[2] - 'a'); "ide0-hd%d", path[2] - 'a');
else if (STREQ (path, "cdrom")) else if (STREQ (path, "cdrom"))
strcpy (qemu_dev_name, "ide1-cd0"); strcpy (qemu_dev_name, "ide1-cd0");
else if (STRPREFIX (path, "fd") && path[2] >= 'a' && path[2] <= 'z') else if (STRPREFIX (path, "fd") && c_islower(path[2]))
snprintf (qemu_dev_name, sizeof (qemu_dev_name), snprintf (qemu_dev_name, sizeof (qemu_dev_name),
"floppy%d", path[2] - 'a'); "floppy%d", path[2] - 'a');
else { else {

View File

@ -15,7 +15,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <c-ctype.h> #include "c-ctype.h"
#include <errno.h> #include <errno.h>
#include "internal.h" #include "internal.h"

View File

@ -18,7 +18,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <c-ctype.h> #include "c-ctype.h"
#ifdef WITH_XEN #ifdef WITH_XEN
#include <xs.h> #include <xs.h>

View File

@ -37,7 +37,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#endif #endif
#include <string.h> #include <string.h>
#include <c-ctype.h> #include "c-ctype.h"
#ifdef HAVE_PATHS_H #ifdef HAVE_PATHS_H
#include <paths.h> #include <paths.h>
@ -681,7 +681,7 @@ virParseNumber(const char **str)
if ((*cur < '0') || (*cur > '9')) if ((*cur < '0') || (*cur > '9'))
return (-1); return (-1);
while ((*cur >= '0') && (*cur <= '9')) { while (c_isdigit(*cur)) {
unsigned int c = *cur - '0'; unsigned int c = *cur - '0';
if ((ret > INT_MAX / 10) || if ((ret > INT_MAX / 10) ||
@ -794,7 +794,7 @@ int virDiskNameToIndex(const char *name) {
while (*ptr) { while (*ptr) {
idx = idx * 26; idx = idx * 26;
if ('a' > *ptr || 'z' < *ptr) if (!c_islower(*ptr))
return -1; return -1;
idx += *ptr - 'a'; idx += *ptr - 'a';

View File

@ -32,6 +32,7 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#include "c-ctype.h"
#include "internal.h" #include "internal.h"
#define qemudLog(level, msg...) fprintf(stderr, msg) #define qemudLog(level, msg...) fprintf(stderr, msg)
@ -105,6 +106,22 @@ virUUIDGenerate(unsigned char *uuid)
return virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN); return virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
} }
/* Convert C from hexadecimal character to integer. */
static int
hextobin (unsigned char c)
{
switch (c)
{
default: return c - '0';
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
}
}
/** /**
* virUUIDParse: * virUUIDParse:
* @uuidstr: zero terminated string representation of the UUID * @uuidstr: zero terminated string representation of the UUID
@ -136,26 +153,16 @@ virUUIDParse(const char *uuidstr, unsigned char *uuid) {
cur++; cur++;
continue; continue;
} }
if ((*cur >= '0') && (*cur <= '9')) if (!c_isxdigit(*cur))
uuid[i] = *cur - '0';
else if ((*cur >= 'a') && (*cur <= 'f'))
uuid[i] = *cur - 'a' + 10;
else if ((*cur >= 'A') && (*cur <= 'F'))
uuid[i] = *cur - 'A' + 10;
else
goto error; goto error;
uuid[i] = hextobin(*cur);
uuid[i] *= 16; uuid[i] *= 16;
cur++; cur++;
if (*cur == 0) if (*cur == 0)
goto error; goto error;
if ((*cur >= '0') && (*cur <= '9')) if (!c_isxdigit(*cur))
uuid[i] += *cur - '0';
else if ((*cur >= 'a') && (*cur <= 'f'))
uuid[i] += *cur - 'a' + 10;
else if ((*cur >= 'A') && (*cur <= 'F'))
uuid[i] += *cur - 'A' + 10;
else
goto error; goto error;
uuid[i] += hextobin(*cur);
i++; i++;
cur++; cur++;
} }

View File

@ -26,7 +26,7 @@
#include <getopt.h> #include <getopt.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/time.h> #include <sys/time.h>
#include <c-ctype.h> #include "c-ctype.h"
#include <fcntl.h> #include <fcntl.h>
#include <locale.h> #include <locale.h>
#include <time.h> #include <time.h>

View File

@ -76,10 +76,10 @@ parseCpuNumber(const char **str, int maxcpu)
int ret = 0; int ret = 0;
const char *cur = *str; const char *cur = *str;
if ((*cur < '0') || (*cur > '9')) if (!c_isdigit(*cur))
return (-1); return (-1);
while ((*cur >= '0') && (*cur <= '9')) { while (c_isdigit(*cur)) {
ret = ret * 10 + (*cur - '0'); ret = ret * 10 + (*cur - '0');
if (ret >= maxcpu) if (ret >= maxcpu)
return (-1); return (-1);
@ -197,7 +197,7 @@ virParseCpuSet(virConnectPtr conn, const char **str, char sep,
neg = 1; neg = 1;
} }
if ((*cur < '0') || (*cur > '9')) if (!c_isdigit(*cur))
goto parse_error; goto parse_error;
start = parseCpuNumber(&cur, maxcpu); start = parseCpuNumber(&cur, maxcpu);
if (start < 0) if (start < 0)