diff: bug fix for mdemangle.c

Fergus Henderson fjh at cs.mu.oz.au
Wed May 14 20:11:58 AEST 1997


util/mdemangle.c:
	Fix some bugs that caused it to get a segmentation violation
	when trying to demangling certain symbols.  Please be careful
	with NULL pointer, folks!

cvs diff: Diffing .
Index: mdemangle.c
===================================================================
RCS file: /home/staff/zs/imp/mercury/util/mdemangle.c,v
retrieving revision 1.16
diff -u -r1.16 mdemangle.c
--- mdemangle.c	1997/03/13 18:23:23	1.16
+++ mdemangle.c	1997/05/14 07:35:55
@@ -25,7 +25,7 @@
 static bool check_for_suffix(char *start, char *position, const char *suffix,
 		int sizeof_suffix, int *mode_num2);
 static char *fix_mangled_ascii(char *str, char **end);
-static char *cut_at_double_underscore(char *str, char *end);
+static bool cut_at_double_underscore(char **str, char *end);
 static bool cut_trailing_integer(char *str, char **end, int *num);
 static bool cut_trailing_underscore_integer(char *str, char **end, int *num);
 static bool strip_prefix(char **str, const char *prefix);
@@ -100,7 +100,7 @@
 	static const char common[] = "common";
 
 	char *start = name;
-	char *module = NULL;	/* module name of type for special pred */
+	const char *type_module = ""; /* module name of type for special pred */
 	char *end = name + strlen(name);
 	char *position;		/* current position in string */
 	int mode_num;
@@ -263,8 +263,10 @@
 	** generated predicates.
 	*/
 	if (category != ORDINARY) {
-		module = start++;
-		start = cut_at_double_underscore(start, end);
+		type_module = start;
+		if (!cut_at_double_underscore(&start, end)) {
+			type_module = "";
+		}
 	}
 
 	/*
@@ -285,15 +287,15 @@
 	switch(category) {
 	case UNIFY:
 		printf("unification predicate for type '%s:%s'/%d mode %d",
-			module, start, arity, mode_num);
+			type_module, start, arity, mode_num);
 		break;
 	case COMPARE:
 		printf("compare/3 predicate for type '%s:%s'/%d",
-			module, start, arity);
+			type_module, start, arity);
 		break;
 	case INDEX:
-		printf("index/2 predicate for type '%s:%s'/%d", module,
-			start, arity);
+		printf("index/2 predicate for type '%s:%s'/%d",
+			type_module, start, arity);
 		break;
 	default:
 		printf("%s '%s'/%d mode %d",
@@ -322,8 +324,10 @@
 	if (!strip_prefix(&start, mercury_data)) {
 		goto wrong_format;
 	}
-	module = start;
-	start = cut_at_double_underscore(start, end);
+	type_module = start;
+	if (!cut_at_double_underscore(&start, end)) {
+		type_module = "";
+	}
 
 	if (strip_prefix(&start, base_type_info)) {
 		data_category = INFO;
@@ -349,26 +353,26 @@
 	switch (data_category) {
 
 	case INFO:
-		if (*module == '\0') {
+		if (*type_module == '\0') {
 			printf("<base type_info for type '%s'/%d>",
 				start, arity);
 		} else {
 			printf("<base type_info for type '%s:%s'/%d>",
-				module, start, arity);
+				type_module, start, arity);
 		}
 		break;
 	case LAYOUT:
-		if (*module == '\0') {
+		if (*type_module == '\0') {
 			printf("<type layout for type '%s'/%d>",
 				start, arity);
 		} else {
 			printf("<type layout for type '%s:%s'/%d>",
-				module, start, arity);
+				type_module, start, arity);
 		}
 		break;
 	case COMMON:
 		printf("<shared constant number %d for module %s>",
-			arity, module);
+			arity, type_module);
 		break;
 
 	default:
@@ -457,22 +461,24 @@
 	/*
 	** Scan for `__' and cut the string at there (replace first
 	** `_' with `\0', return the part of the string after the `__').
-	** Returns NULL if there is no `__' in the string before the
-	** supplied end.
+	** Returns TRUE if `__' was found, FALSE otherwise.
 	*/
 
-static char *
-cut_at_double_underscore(char *str, char *end) 
+static bool
+cut_at_double_underscore(char **start, char *end) 
 {
+	char *str = *start;
+
 	while (*str != '_' || *(str + 1) != '_') {
 		if (str == end) {
-			return NULL;
+			return FALSE;
 		}
 		str++;
 	}
 
 	*str = '\0';
-	return str + 2;
+	*start = str + 2;
+	return TRUE;
 }
 
 	/*

-- 
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