[m-rev.] diff: fix bugs with use of MR_trace_getline()

Fergus Henderson fjh at cs.mu.OZ.AU
Wed Oct 8 16:45:05 AEST 2003


Estimated hours taken: 1
Branches: main, release

trace/mercury_trace_internal.c:
	Fix several bugs:

	- When getting a continuation line, deal with the case where
	  MR_trace_getline() returns NULL.  This fixes a bug where mdb
	  would crash if you typed control-D (EOF) on a continuation line.

	- When getting a continuation line, call MR_free to deallocate
	  the memory for the continuation line after we have copied it.
	  This fixes a bug where mdb would leak memory when reading
	  continuation lines.

	- Make sure that we use MR_free() rather than free() to
	  deallocate the return value from MR_trace_getline(),
	  to match the documentation for MR_trace_getline().

Workspace: /home/ceres/fjh/mercury
Index: trace/mercury_trace_internal.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_internal.c,v
retrieving revision 1.158
diff -u -d -r1.158 mercury_trace_internal.c
--- trace/mercury_trace_internal.c	12 Jun 2003 15:38:36 -0000	1.158
+++ trace/mercury_trace_internal.c	8 Oct 2003 06:26:02 -0000
@@ -5898,11 +5898,21 @@
 		** allocating more space if necessary.
 		*/
 		line = MR_trace_getline("> ", mdb_in, mdb_out);
+		if (line == NULL) {
+			/*
+			** We got an EOF... we need to stop processing
+			** the input, even though it is not syntactically
+			** correct, otherwise we might get into an infinite
+			** loop if we keep getting EOF.
+			*/
+			break;
+		}
 		extra_len = strlen(line);
 		/* cmd_char_max is always > 0 */
 		MR_ensure_big_enough(len + extra_len + 1, cmd_char, char, 0);
 		ptr = cmd_chars + len;
 		strcpy(ptr, line);
+		MR_free(line);
 		len = len + extra_len;
 	}
 
@@ -6084,7 +6094,7 @@
 						break;
 
 					case 'q':
-						free(buf);
+						MR_free(buf);
 						return MR_trace_event_internal(
 								cmd, MR_TRUE,
 								event_info);
@@ -6094,12 +6104,12 @@
 						fprintf(MR_mdb_err,
 							"unknown command, "
 							"try again\n");
-						free(buf);
+						MR_free(buf);
 						goto try_again;
 				}
 			}
 
-			free(buf);
+			MR_free(buf);
 		}
 
 		MR_scroll_next = 0;

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
The University of Melbourne         |  of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh>  |     -- the last words of T. S. Garp.
--------------------------------------------------------------------------
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