[m-rev.] for review: fix bugs in mdb breakpoint procedure specification

Simon Taylor stayl at cs.mu.OZ.AU
Mon Feb 11 15:47:31 AEDT 2002


On 11-Feb-2002, Fergus Henderson <fjh at cs.mu.OZ.AU> wrote:
> On 11-Feb-2002, Simon Taylor <stayl at cs.mu.OZ.AU> wrote:
> > +static bool
> > +MR_parse_trailing_number(char *start, char **end, int *n)

> This routine is dangerous: it requires `start'
> to point to at least one character *past* the start of the
> string.  Otherwise, if the string contains only digits,
> then this routine will set `end' to `start - 1', which
> in standard C will result in undefined behaviour
> if `start' points to the start of the string.

--- mercury_trace_tables.c	2002/02/11 03:32:41	1.1
+++ mercury_trace_tables.c	2002/02/11 04:39:32
@@ -39,7 +39,7 @@
 			*file_layout, int line,
 			MR_file_line_callback callback_func, int callback_arg);
 
-static bool MR_parse_trailing_number(char *str, char **s, int *n);
+static bool MR_parse_trailing_number(char *start, char **end, int *number);
 
 void
 MR_register_all_modules_and_procs(FILE *fp, bool verbose)
@@ -241,10 +241,11 @@
 	*/
 	end = str + len - 1;
 	if (MR_parse_trailing_number(str, &end, &n)) {
-		if (end < str) {
+		if (end == str) {
 			/* the string contains only a number */
 			return FALSE;
 		}
+		end--;
 		if (*end == ':') {
 			/* filename:linenumber */
 			return FALSE;
@@ -259,10 +260,11 @@
 
 			end--;
 			if (MR_parse_trailing_number(str, &end, &n)) {
-				if (end < str) {
+				if (end == str) {
 					/* the string contains only a number */
 					return FALSE;
 				}
+				end--;
 				if (*end == '/') {
 					*end = '\0';
 					spec->MR_proc_arity = n;
@@ -285,8 +287,11 @@
 		str += 5;
 	}
 
-	/* Search backwards for the end of the final module qualifier. */
-	while (end >= str) {
+	/*
+	** Search backwards for the end of the final module qualifier.
+	** There must be at least one character before the qualifier.
+	*/
+	while (end > str) {
 		if (*end == ':' || (*end == '_' && *(end + 1) == '_')) {
 			if (*end  == ':') {
 				spec->MR_proc_name = end + 1;
@@ -322,18 +327,31 @@
 	return TRUE;
 }
 
+/* 
+** Go backwards over a string starting at `end', stopping at `start',
+** parsing the trailing integer and storing it in `*n'.
+** On return, `*end' points to the start of the trailing number.
+** If no number was found, `*end' is unchanged. 
+*/
 static bool
-MR_parse_trailing_number(char *start, char **end, int *n)
+MR_parse_trailing_number(char *start, char **end, int *number)
 {
 	bool found_digit = FALSE;
 	int power_of_10 = 1;	
+	char c;
+	char *tmp_end;
 
-	*n = 0;
-	while (*end >= start && MR_isdigit(**end)) {
+	*number = 0;
+
+	tmp_end = *end + 1;
+	while (tmp_end > start && MR_isdigit(*(tmp_end - 1))) {
 		found_digit = TRUE;
-		*n += power_of_10 * (**end - '0');
+		*number += power_of_10 * (*(tmp_end - 1) - '0');
 		power_of_10 *= 10;
-		(*end)--;
+		tmp_end--;
+	}
+	if (found_digit) {
+		*end = tmp_end;
 	}
 	return found_digit;
 }
--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list