[Bug 377] New: Reduce compiler warnings. Use unsigned args to the ctype.h is*() macros.

Mark D. Baushke mdb at juniper.net
Sat Aug 3 00:20:03 EST 2002


Florian writes:

>This is not quite correct on compilers with signed chars.  Arguments
>for the is* functions are indeed ints.  (I think the warnings are
>bogus.)

The ANSI-ISO-IED-9899-1999 (aka C99) Standards says this:

|     7.4 Character handling <ctype.h>
|
|     The header <ctype.h> declares several functions useful for
|     classifying and mapping characters[See future library directions
|     7.26.2]. In all cases, the argument is an int, the value of which
|     shall be representable as an unsigned char or shall equal the
|     value of the macro EOF. If the argument has any other value, the
|     behavior is undefined.

I take this to mean that values of EOF (-1) thru 256 inclusive (on a
machine with eight bit character types) are valid values to pass in as
an argument and the rest do not have a defined value.

I agree that an expression like: (isascii(ch) && isdigit(ch)) will
always be valid. However, if a char variable ch has a value if 0x80 and
char types are signed, then the plain (isdigit(ch)) will be passing
0xffffff80 due to sign extension and promotion to an int type as the
argument and the behavior is that argument is NOT defined.

All that said, Richard Kettlewell <rjk at terraraq.org.uk> was correct that
the cast to (unsigned) I used should more properly be a case to
(unsigned char). A revised patch is after my .signature

	Thanks,
	-- Mark

Index: canohost.c
===================================================================
RCS file: /cvs/openssh/canohost.c,v
retrieving revision 1.30
diff -u -p -r1.30 canohost.c
--- canohost.c	11 Jul 2002 03:56:47 -0000	1.30
+++ canohost.c	2 Aug 2002 14:19:16 -0000
@@ -90,7 +90,7 @@ get_remote_hostname(int socket, int veri
 	 * of this software).
 	 */
 	for (i = 0; name[i]; i++)
-		if (isupper(name[i]))
+		if (isupper((unsigned char)name[i]))
 			name[i] = tolower(name[i]);
 
 	if (!verify_reverse_mapping)
Index: clientloop.c
===================================================================
RCS file: /cvs/openssh/clientloop.c,v
retrieving revision 1.87
diff -u -p -r1.87 clientloop.c
--- clientloop.c	4 Jul 2002 00:14:18 -0000	1.87
+++ clientloop.c	2 Aug 2002 14:19:17 -0000
@@ -485,7 +485,7 @@ process_cmdline(void)
 	cmd = s = read_passphrase("\r\nssh> ", RP_ECHO);
 	if (s == NULL)
 		goto out;
-	while (*s && isspace(*s))
+	while (*s && isspace((unsigned char)*s))
 		s++;
 	if (*s == 0)
 		goto out;
@@ -500,7 +500,7 @@ process_cmdline(void)
 		goto out;
 	}
 	s += 2;
-	while (*s && isspace(*s))
+	while (*s && isspace((unsigned char)*s))
 		s++;
 
 	if (sscanf(s, "%5[0-9]:%255[^:]:%5[0-9]",
Index: match.c
===================================================================
RCS file: /cvs/openssh/match.c,v
retrieving revision 1.18
diff -u -p -r1.18 match.c
--- match.c	5 Mar 2002 01:42:43 -0000	1.18
+++ match.c	2 Aug 2002 14:19:17 -0000
@@ -135,7 +135,7 @@ match_pattern_list(const char *string, c
 		for (subi = 0;
 		    i < len && subi < sizeof(sub) - 1 && pattern[i] != ',';
 		    subi++, i++)
-			sub[subi] = dolower && isupper(pattern[i]) ?
+			sub[subi] = dolower && isupper((unsigned char)pattern[i]) ?
 			    tolower(pattern[i]) : pattern[i];
 		/* If subpattern too long, return failure (no match). */
 		if (subi >= sizeof(sub) - 1)
Index: scp.c
===================================================================
RCS file: /cvs/openssh/scp.c,v
retrieving revision 1.97
diff -u -p -r1.97 scp.c
--- scp.c	21 Jun 2002 00:41:52 -0000	1.97
+++ scp.c	2 Aug 2002 14:19:17 -0000
@@ -755,7 +755,7 @@ sink(argc, argv)
 		if (*cp++ != ' ')
 			SCREWUP("mode not delimited");
 
-		for (size = 0; isdigit(*cp);)
+		for (size = 0; isdigit((unsigned char)*cp);)
 			size = size * 10 + (*cp++ - '0');
 		if (*cp++ != ' ')
 			SCREWUP("size not delimited");
Index: sshconnect.c
===================================================================
RCS file: /cvs/openssh/sshconnect.c,v
retrieving revision 1.99
diff -u -p -r1.99 sshconnect.c
--- sshconnect.c	1 Aug 2002 01:26:30 -0000	1.99
+++ sshconnect.c	2 Aug 2002 14:19:17 -0000
@@ -831,7 +831,7 @@ ssh_login(Sensitive *sensitive, const ch
 	/* Convert the user-supplied hostname into all lowercase. */
 	host = xstrdup(orighost);
 	for (cp = host; *cp; cp++)
-		if (isupper(*cp))
+		if (isupper((unsigned char)*cp))
 			*cp = tolower(*cp);
 
 	/* Exchange protocol version identification strings with the server. */
Index: openbsd-compat/inet_aton.c
===================================================================
RCS file: /cvs/openssh/openbsd-compat/inet_aton.c,v
retrieving revision 1.4
diff -u -p -r1.4 inet_aton.c
--- openbsd-compat/inet_aton.c	12 Apr 2002 03:35:40 -0000	1.4
+++ openbsd-compat/inet_aton.c	2 Aug 2002 14:19:18 -0000
@@ -103,7 +103,7 @@ inet_aton(const char *cp, struct in_addr
 {
 	register u_int32_t val;
 	register int base, n;
-	register char c;
+	register unsigned char c;
 	unsigned int parts[4];
 	register unsigned int *pp = parts;
 
Index: openbsd-compat/mktemp.c
===================================================================
RCS file: /cvs/openssh/openbsd-compat/mktemp.c,v
retrieving revision 1.2
diff -u -p -r1.2 mktemp.c
--- openbsd-compat/mktemp.c	13 Feb 2002 05:00:16 -0000	1.2
+++ openbsd-compat/mktemp.c	2 Aug 2002 14:19:18 -0000
@@ -165,7 +165,7 @@ _gettemp(path, doopen, domkdir, slen)
 					return (0);
 				*trv++ = 'a';
 			} else {
-				if (isdigit(*trv))
+				if (isdigit((unsigned char)*trv))
 					*trv = 'a';
 				else if (*trv == 'z')	/* inc from z to A */
 					*trv = 'A';
Index: openbsd-compat/readpassphrase.c
===================================================================
RCS file: /cvs/openssh/openbsd-compat/readpassphrase.c,v
retrieving revision 1.8
diff -u -p -r1.8 readpassphrase.c
--- openbsd-compat/readpassphrase.c	1 May 2002 12:00:22 -0000	1.8
+++ openbsd-compat/readpassphrase.c	2 Aug 2002 14:19:18 -0000
@@ -120,7 +120,7 @@ restart:
 		if (p < end) {
 			if ((flags & RPP_SEVENBIT))
 				ch &= 0x7f;
-			if (isalpha(ch)) {
+			if (isalpha((unsigned char)ch)) {
 				if ((flags & RPP_FORCELOWER))
 					ch = tolower(ch);
 				if ((flags & RPP_FORCEUPPER))



More information about the openssh-unix-dev mailing list