diff: fix more isalnum() cast problems

Fergus Henderson fjh at cs.mu.OZ.AU
Tue Jul 28 03:46:51 AEST 1998


Estimated hours taken: 0.5

Fix a portability problem: mkinit.c and mdemangle.c were not casting
the arguments of isdigit() and isalnum() to unsigned char.

runtime/mercury_trace_internal.c:
runtime/mercury_std.h:
	Move the definitions of MR_isdigit() and MR_isspace()
	from mercury_trace_internal.c to mercury_std.h.
	Add a similar definition for MR_isalnum().

runtime/mercury_wrapper.c:
	Use MR_isspace() rather than doing the casts manually.

util/mkinit.c:
util/mdemangle.c:
	Use the MR_ versions of isdigit() and isalnum().

util/mkinit.c:
	Change the type of a variable from `int' to `size_t' to
	avoid a compiler warning about "comparison between signed
	and unsigned".

Index: runtime/mercury_std.h
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_std.h,v
retrieving revision 1.3
diff -u -r1.3 mercury_std.h
--- mercury_std.h	1998/03/16 12:23:39	1.3
+++ mercury_std.h	1998/07/27 17:44:14
@@ -14,6 +14,7 @@
 
 #include <stdlib.h>	/* for size_t */
 #include <assert.h>	/* for assert() */
+#include <ctype.h>	/* for isalnum(), etc. */
 
 #ifndef	reg
 #define	reg		register
@@ -28,6 +29,16 @@
 #ifndef min
 #define	min(a, b)	((a) < (b) ? (a) : (b))
 #endif
+
+/*
+** The ANSI C isalnum(), etc. macros require that the argument be cast to
+** `unsigned char'; if you pass a signed char, the behaviour is undefined.
+** Hence we define `MR_' versions of these that do the cast -- you should
+** make sure to always use the `MR_' versions rather than the standard ones.
+*/
+#define	MR_isalnum(c)	isdigit((unsigned char) (c))
+#define	MR_isdigit(c)	isdigit((unsigned char) (c))
+#define	MR_isspace(c)	isspace((unsigned char) (c))
 
 #define streq(s1, s2)		(strcmp(s1, s2) == 0)
 #define strdiff(s1, s2)		(strcmp(s1, s2) != 0)
Index: runtime/mercury_trace_internal.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_trace_internal.c,v
retrieving revision 1.10
diff -u -r1.10 mercury_trace_internal.c
--- mercury_trace_internal.c	1998/07/23 06:36:31	1.10
+++ mercury_trace_internal.c	1998/07/27 17:41:25
@@ -14,11 +14,8 @@
 #include "mercury_trace.h"
 #include "mercury_trace_internal.h"
 #include "mercury_trace_util.h"
+#include "mercury_std.h"
 #include <stdio.h>
-#include <ctype.h>
-
-#define	MR_isdigit(c)	isdigit((unsigned char) (c))
-#define	MR_isspace(c)	isspace((unsigned char) (c))
 
 #define	MR_NAME_LEN		80
 #define	MR_MAX_SPY_POINTS	100
Index: runtime/mercury_wrapper.c
===================================================================
RCS file: /home/mercury1/repository/mercury/runtime/mercury_wrapper.c,v
retrieving revision 1.19
diff -u -r1.19 mercury_wrapper.c
--- mercury_wrapper.c	1998/07/22 07:53:35	1.19
+++ mercury_wrapper.c	1998/07/27 17:42:23
@@ -31,7 +31,6 @@
 #include	"mercury_imp.h"
 
 #include	<stdio.h>
-#include	<ctype.h>
 #include	<string.h>
 
 #include	"mercury_timing.h"
@@ -340,7 +339,7 @@
 
 	for (;;) {
 		/* skip leading whitespace */
-		while(isspace((unsigned char)*s)) {
+		while(MR_isspace(*s)) {
 			s++;
 		}
 
@@ -369,7 +368,7 @@
 			s++;
 		} else {
 			/* ordinary white-space delimited arg */
-			while(*s != '\0' && !isspace((unsigned char)*s)) {
+			while(*s != '\0' && !MR_isspace(*s)) {
 				if (*s == '\\')
 					s++;
 				args_len++; s++;
@@ -392,7 +391,7 @@
 	d = args;
 	for(i = 0; i < argc; i++) {
 		/* skip leading whitespace */
-		while(isspace((unsigned char)*s)) {
+		while(MR_isspace(*s)) {
 			s++;
 		}
 
@@ -416,7 +415,7 @@
 			s++;
 		} else {
 			/* ordinary white-space delimited arg */
-			while(*s != '\0' && !isspace((unsigned char)*s)) {
+			while(*s != '\0' && !MR_isspace(*s)) {
 				if (*s == '\\')
 					s++;
 				*d++ = *s++;
Index: util/mdemangle.c
===================================================================
RCS file: /home/mercury1/repository/mercury/util/mdemangle.c,v
retrieving revision 1.30
diff -u -r1.30 mdemangle.c
--- mdemangle.c	1998/04/27 04:05:12	1.30
+++ mdemangle.c	1998/07/27 17:38:02
@@ -67,7 +67,7 @@
 		*/
 		for (;;) {
 			char buf[1000];
-			int len = 0;
+			size_t len = 0;
 			int c = getchar();
 			while (c != EOF && (isalnum(c) || c == '_')) {
 				if (len >= sizeof(buf) - 1)
@@ -335,7 +335,7 @@
 			goto wrong_format;
 		}
 		lambda_line = 0;
-		while (start < end && isdigit(*start)) {
+		while (start < end && MR_isdigit(*start)) {
 			lambda_line = lambda_line * 10 + (*start - '0');
 			start++;
 		}
@@ -343,7 +343,7 @@
 			goto wrong_format;
 		}
 		lambda_seq_number = 0;
-		while (start < end && isdigit(*start)) {
+		while (start < end && MR_isdigit(*start)) {
 			lambda_seq_number = lambda_seq_number * 10 +
 				(*start - '0');
 			start++;
Index: util/mkinit.c
===================================================================
RCS file: /home/mercury1/repository/mercury/util/mkinit.c,v
retrieving revision 1.37
diff -u -r1.37 mkinit.c
--- mkinit.c	1998/07/27 15:25:42	1.37
+++ mkinit.c	1998/07/27 17:37:52
@@ -455,8 +455,9 @@
 		if (strncmp(line, init_str, init_strlen) == 0) {
 			int	j;
 
-			for (j = init_strlen; isalnum(line[j]) ||
-						line[j] == '_'; j++)
+			for (j = init_strlen;
+			     MR_isalnum(line[j]) || line[j] == '_';
+			     j++)
 			{
 				/* VOID */
 			}

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>  |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3        |     -- the last words of T. S. Garp.



More information about the developers mailing list