Make sure to use unsigned char for is*() functions
On some platforms, the implementation is such that a signed char
triggers a warning when used with is*() functions. On others, the
behavior is outright buggy when presented with a char that happens
to get promoted to a negative integer.
The safest thing is to cast the char that's used to an unsigned char.
Reviewed-by: Andy Polyakov <appro@openssl.org>
diff --git a/apps/apps.c b/apps/apps.c
index 7a4608f..2a189f2 100644
--- a/apps/apps.c
+++ b/apps/apps.c
@@ -183,7 +183,7 @@
for (p = buf;;) {
/* Skip whitespace. */
- while (*p && isspace(*p))
+ while (*p && isspace(_UC(*p)))
p++;
if (!*p)
break;
@@ -207,7 +207,7 @@
p++;
*p++ = '\0';
} else {
- while (*p && !isspace(*p))
+ while (*p && !isspace(_UC(*p)))
p++;
if (*p)
*p++ = '\0';
diff --git a/apps/apps.h b/apps/apps.h
index 8ac7c03..878dc11 100644
--- a/apps/apps.h
+++ b/apps/apps.h
@@ -149,6 +149,13 @@
# define uintmax_t unsigned long
# endif
+/*
+ * quick macro when you need to pass an unsigned char instead of a char.
+ * this is true for some implementations of the is*() functions, for
+ * example.
+ */
+#define _UC(c) ((unsigned char)(c))
+
int app_RAND_load_file(const char *file, int dont_warn);
int app_RAND_write_file(const char *file);
/*
diff --git a/apps/ca.c b/apps/ca.c
index 9a1b69f..716df02 100644
--- a/apps/ca.c
+++ b/apps/ca.c
@@ -731,7 +731,7 @@
goto end;
}
for ( ; *p; p++) {
- if (!isxdigit(*p)) {
+ if (!isxdigit(_UC(*p))) {
BIO_printf(bio_err,
"entry %d: bad char 0%o '%c' in serial number\n",
i + 1, *p, *p);
diff --git a/apps/ocsp.c b/apps/ocsp.c
index 73b407c..f9ba4e1 100644
--- a/apps/ocsp.c
+++ b/apps/ocsp.c
@@ -1065,7 +1065,7 @@
for (; *p; p++) {
if (*p != '%')
*out++ = *p;
- else if (isxdigit(p[1]) && isxdigit(p[2])) {
+ else if (isxdigit(_UC(p[1])) && isxdigit(_UC(p[2]))) {
*out++ = (app_hex(p[1]) << 4) | app_hex(p[2]);
p += 2;
}
diff --git a/apps/s_client.c b/apps/s_client.c
index c122c1a..55d1283 100644
--- a/apps/s_client.c
+++ b/apps/s_client.c
@@ -509,9 +509,9 @@
for (byte = 0; *in; ++in) {
char c;
- if (isspace(*in))
+ if (isspace(_UC(*in)))
continue;
- c = tolower(*in);
+ c = tolower(_UC(*in));
if ('0' <= c && c <= '9') {
byte |= c - '0';
} else if ('a' <= c && c <= 'f') {
@@ -553,11 +553,11 @@
e = restore_errno();
if (((v == LONG_MIN || v == LONG_MAX) && e == ERANGE) ||
- endp == in || !isspace(*endp) ||
+ endp == in || !isspace(_UC(*endp)) ||
v != (*result = (uint8_t) v)) {
return -1;
}
- for (in = endp; isspace(*in); ++in)
+ for (in = endp; isspace(_UC(*in)); ++in)
continue;
*inptr = in;
@@ -1141,7 +1141,7 @@
break;
case OPT_PSK:
for (p = psk_key = opt_arg(); *p; p++) {
- if (isxdigit(*p))
+ if (isxdigit(_UC(*p)))
continue;
BIO_printf(bio_err, "Not a hex number '%s'\n", psk_key);
goto end;
diff --git a/apps/s_server.c b/apps/s_server.c
index 489924c..3803036 100644
--- a/apps/s_server.c
+++ b/apps/s_server.c
@@ -1380,7 +1380,7 @@
case OPT_PSK:
#ifndef OPENSSL_NO_PSK
for (p = psk_key = opt_arg(); *p; p++) {
- if (isxdigit(*p))
+ if (isxdigit(_UC(*p)))
continue;
BIO_printf(bio_err, "Not a hex number '%s'\n", *argv);
goto end;