[m-rev.] diff: fix bug in mdb `document' command

Fergus Henderson fjh at cs.mu.OZ.AU
Fri Mar 30 15:54:56 AEST 2001


Estimated hours taken: 0.75
Branches: main, release

Fix a bug where mdb's document command was sometimes
crashing with a seg fault:
	bash$ mmc --debug hello.m
	bash$ mdb ./hello
	mdb> document
	cat> end
	*** Mercury runtime: caught segmentation violation ***

trace/mercury_trace_internal.c:
	Ensure that there's enough memory allocated to hold
	the terminating '\0' character.
	Also add some comments.

Workspace: /home/venus/fjh/ws-venus2/mercury
Index: trace/mercury_trace_internal.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_internal.c,v
retrieving revision 1.98
diff -u -d -r1.98 mercury_trace_internal.c
--- trace/mercury_trace_internal.c	2001/02/23 04:15:24	1.98
+++ trace/mercury_trace_internal.c	2001/03/30 05:53:06
@@ -2543,6 +2543,8 @@
 /*
 ** Read lines until we find one that contains only "end".
 ** Return the lines concatenated together.
+** The memory returned is allocated with MR_malloc();
+** it is the caller's responsibility to MR_free() it when appropriate.
 */
 
 static const char *
@@ -2557,7 +2559,8 @@
 
 	next_char_slot = 0;
 	while ((text = MR_trace_getline("cat> ", MR_mdb_in, MR_mdb_out))
-			!= NULL) {
+			!= NULL)
+	{
 		if (streq(text, "end")) {
 			MR_free(text);
 			break;
@@ -2576,6 +2579,8 @@
 		MR_free(text);
 	}
 
+	MR_ensure_big_enough(next_char_slot, doc_char, char,
+			MR_INIT_DOC_CHARS);
 	doc_chars[next_char_slot] = '\0';
 	return doc_chars;
 }
@@ -2592,6 +2597,9 @@
 ** *words_max strings. The number of strings (words) filled in will be
 ** given by *word_count.
 **
+** The space for the *words array is allocated with MR_malloc().
+** It is the caller's responsibility to free it when appropriate.
+** The elements of the *words array point to memory from the line array.
 ** The lifetime of the elements of the *words array expires when
 ** the line array is MR_free()'d or further modified or when
 ** MR_trace_parse_line is called again, whichever comes first.
@@ -2674,7 +2682,8 @@
 **
 ** On return *words will point to an array of strings, with space for
 ** *words_max strings. The number of strings filled in will be given by
-** the return value.
+** the return value.  The memory for *words is allocated with MR_malloc(),
+** and it is the responsibility of the caller to MR_free() it when appropriate.
 */
 
 static int
@@ -2834,6 +2843,11 @@
 ** newline).
 ** If EOF occurs on a nonempty line, treat the EOF as a newline; if EOF
 ** occurs on an empty line, return NULL.
+**
+** Whether the line is read from the queue or from mdb_in, if this function
+** returns a non-NULL value, then the memory for the line returned will have
+** been allocated with MR_malloc(), and it is the caller's resposibility
+** to MR_free() it when appropriate.
 */
 
 char *
@@ -2863,6 +2877,9 @@
 
 /*
 ** If there any lines waiting in the queue, return the first of these.
+** The memory for the line will have been allocated with MR_malloc(),
+** and it is the caller's resposibility to MR_free() it when appropriate.
+** If there are no lines in the queue, this function returns NULL.
 */
 
 static char *

-- 
Fergus Henderson <fjh at cs.mu.oz.au>  |  "I have always known that the pursuit
                                    |  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